Skip to content

Commit

Permalink
Added Extensions to ApiManifest
Browse files Browse the repository at this point in the history
  • Loading branch information
darrelmiller committed May 30, 2023
1 parent 0f9f82b commit 9ef50fc
Show file tree
Hide file tree
Showing 7 changed files with 444 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/lib/ApiDependency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,25 @@ namespace Microsoft.OpenApi.ApiManifest;
public class ApiDependency
{
public string? ApiDescripionUrl { get; set; }
public string? ApiDescripionVersion { get; set; }
public Auth? Auth { get; set; }
public List<Request> Requests { get; set; } = new List<Request>();
public Extensions? Extensions { get; set; }

private const string ApiDescriptionUrlProperty = "apiDescripionUrl";
private const string ApiDescriptionVersionProperty = "apiDescripionVersion";
private const string AuthProperty = "auth";
private const string RequestsProperty = "requests";
private const string ExtensionsProperty = "extensions";

// Write method
public void Write(Utf8JsonWriter writer)
{
writer.WriteStartObject();

if (!String.IsNullOrWhiteSpace(ApiDescripionUrl)) writer.WriteString(ApiDescriptionUrlProperty, ApiDescripionUrl);
if (!String.IsNullOrWhiteSpace(ApiDescripionVersion)) writer.WriteString(ApiDescriptionVersionProperty, ApiDescripionVersion);

if (Auth != null) {
writer.WritePropertyName(AuthProperty);
Auth?.Write(writer);
Expand All @@ -32,14 +38,21 @@ public void Write(Utf8JsonWriter writer)
writer.WriteEndArray();
}

if (Extensions != null) {
writer.WritePropertyName(ExtensionsProperty);
Extensions?.Write(writer);
}

writer.WriteEndObject();
}
// Fixed fieldmap for ApiDependency
private static FixedFieldMap<ApiDependency> handlers = new()
{
{ ApiDescriptionUrlProperty, (o,v) => {o.ApiDescripionUrl = v.GetString(); } },
{ ApiDescriptionVersionProperty, (o,v) => {o.ApiDescripionVersion = v.GetString(); } },
{ AuthProperty, (o,v) => {o.Auth = Auth.Load(v); } },
{ RequestsProperty, (o,v) => {o.Requests = ParsingHelpers.GetList(v, Request.Load); } },
{ ExtensionsProperty, (o,v) => {o.Extensions = Extensions.Load(v); } }
};

// Load Method
Expand Down
8 changes: 8 additions & 0 deletions src/lib/ApiManifestDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ public class ApiManifestDocument
{
public Publisher? Publisher { get; set; }
public ApiDependencies ApiDependencies { get; set; } = new ApiDependencies();
public Extensions Extensions { get; set; } = new Extensions();

private const string PublisherProperty = "publisher";
private const string ApiDependenciesProperty = "apiDependencies";
private const string ExtensionsProperty = "extensions";

// Write method
public void Write(Utf8JsonWriter writer)
Expand All @@ -27,6 +29,11 @@ public void Write(Utf8JsonWriter writer)
}
writer.WriteEndObject();

if (Extensions.Count > 0) {
writer.WritePropertyName(ExtensionsProperty);
Extensions.Write(writer);
}

writer.WriteEndObject();
}
// Load method
Expand All @@ -41,6 +48,7 @@ public static ApiManifestDocument Load(JsonElement value)
{
{ PublisherProperty, (o,v) => {o.Publisher = Publisher.Load(v); } },
{ ApiDependenciesProperty, (o,v) => {o.ApiDependencies = new ApiDependencies(ParsingHelpers.GetMap(v, ApiDependency.Load)); } },
{ ExtensionsProperty, (o,v) => {o.Extensions = Extensions.Load(v); } }
};
}

Expand Down
30 changes: 30 additions & 0 deletions src/lib/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Text.Json;
using System.Text.Json.Nodes;

namespace Microsoft.OpenApi.ApiManifest;

public class Extensions : Dictionary<string, JsonNode>
{
public static Extensions Load(JsonElement value)
{
var extensions = new Extensions();
foreach(var property in value.EnumerateObject())
{
if (property.Value.ValueKind != JsonValueKind.Null) {
extensions.Add(property.Name, JsonSerializer.Deserialize<JsonObject>(property.Value.GetRawText()));
}
}
return extensions;
}

public void Write(Utf8JsonWriter writer)
{
writer.WriteStartObject();
foreach(var extension in this)
{
writer.WritePropertyName(extension.Key);
writer.WriteRawValue(extension.Value.ToJsonString());
}
writer.WriteEndObject();
}
}
36 changes: 36 additions & 0 deletions src/lib/OpenAI/Api.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

using System.Text.Json;

namespace Microsoft.OpenApi.ApiManifest.OpenAI;

public class Api {
public string? Type { get; set; }
public string? Url { get; set; }
public bool? IsUserAuthenticated { get; set; }

public static Api Load(JsonElement value)
{
var api = new Api();
ParsingHelpers.ParseMap(value, api, handlers);
return api;
}

// Create handlers FixedFieldMap for Api
private static FixedFieldMap<Api> handlers = new()
{
{ "type", (o,v) => {o.Type = v.GetString(); } },
{ "url", (o,v) => {o.Url = v.GetString(); } },
{ "is_user_authenticated", (o,v) => {o.IsUserAuthenticated = v.GetBoolean(); }},
};

public void Write(Utf8JsonWriter writer)
{
writer.WriteStartObject();
writer.WriteString("type", Type);
writer.WriteString("url", Url);
writer.WriteBoolean("is_user_authenticated", IsUserAuthenticated ?? false);
writer.WriteEndObject();
}
}


140 changes: 140 additions & 0 deletions src/lib/OpenAI/BaseManifestAuth.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@

using System.Text.Json;

namespace Microsoft.OpenApi.ApiManifest.OpenAI;

public abstract class BaseManifestAuth
{
public string? Type { get; set; }
public string? Instructions { get; set; }

public static BaseManifestAuth? Load(JsonElement value)
{
BaseManifestAuth? auth = null;

switch(value.GetProperty("type").GetString()) {
case "none":
auth = new ManifestNoAuth();
ParsingHelpers.ParseMap<ManifestNoAuth>(value, (ManifestNoAuth)auth, ManifestNoAuth.handlers);
break;
case "user_http":
auth = new ManifestUserHttpAuth();
ParsingHelpers.ParseMap<ManifestUserHttpAuth>(value, (ManifestUserHttpAuth)auth, ManifestUserHttpAuth.handlers);
break;
case "service_http":
auth = new ManifestServiceHttpAuth();
ParsingHelpers.ParseMap<ManifestServiceHttpAuth>(value, (ManifestServiceHttpAuth)auth, ManifestServiceHttpAuth.handlers);
break;
case "oauth":
auth = new ManifestOAuthAuth();
ParsingHelpers.ParseMap<ManifestOAuthAuth>(value, (ManifestOAuthAuth)auth, ManifestOAuthAuth.handlers);
break;
}

return auth;
}

// Create handlers FixedFieldMap for ManifestAuth

public virtual void Write(Utf8JsonWriter writer) {}

}

public class ManifestNoAuth : BaseManifestAuth
{
public ManifestNoAuth()
{
Type = "none";
}

internal static FixedFieldMap<ManifestNoAuth> handlers = new()
{
{ "type", (o,v) => {o.Type = v.GetString(); } },
{ "instructions", (o,v) => {o.Instructions = v.GetString(); } },
};

public override void Write(Utf8JsonWriter writer) {
writer.WriteStartObject();
writer.WriteString("type", Type);
if(Instructions != null) writer.WriteString("instructions", Instructions);
writer.WriteEndObject();
}
}

public class ManifestOAuthAuth : BaseManifestAuth
{
public string? ClientUrl { get; set; }
public string? Scope { get; set; }
public string? AuthorizationUrl { get; set; }
public string? AuthorizationContentType { get; set; }
public Dictionary<string, string>? VerificationTokens { get; set; }

public ManifestOAuthAuth()
{
Type = "oauth";
}
internal static FixedFieldMap<ManifestOAuthAuth> handlers = new()
{
{ "type", (o,v) => {o.Type = v.GetString(); } },
{ "instructions", (o,v) => {o.Instructions = v.GetString(); } },
{ "client_url", (o,v) => {o.ClientUrl = v.GetString(); } },
{ "scope", (o,v) => {o.Scope = v.GetString(); } },
{ "authorization_url", (o,v) => {o.AuthorizationUrl = v.GetString(); } },
{ "authorization_content_type", (o,v) => {o.AuthorizationContentType = v.GetString(); } },
{ "verification_tokens", (o,v) => { o.VerificationTokens = ParsingHelpers.GetMap<string>(v,(e) => e.GetString() ); } },
};

public override void Write(Utf8JsonWriter writer) {
writer.WriteStartObject();
writer.WriteString("type", Type);

if(Instructions != null) writer.WriteString("instructions", Instructions);
if(ClientUrl != null) writer.WriteString("client_url", ClientUrl);
if(Scope != null) writer.WriteString("scope", Scope);
if(AuthorizationUrl != null) writer.WriteString("authorization_url", AuthorizationUrl);
if(AuthorizationContentType != null) writer.WriteString("authorization_content_type", AuthorizationContentType);
writer.WriteEndObject();
}
}

public class ManifestUserHttpAuth : BaseManifestAuth
{
public ManifestUserHttpAuth()
{
Type = "user_http";
}
internal static FixedFieldMap<ManifestUserHttpAuth> handlers = new()
{
{ "type", (o,v) => {o.Type = v.GetString(); } },
{ "instructions", (o,v) => {o.Instructions = v.GetString(); } },
};
public override void Write(Utf8JsonWriter writer) {
writer.WriteStartObject();
writer.WriteString("type", Type);
writer.WriteString("instructions", Instructions);
writer.WriteEndObject();
}
}

public class ManifestServiceHttpAuth : BaseManifestAuth
{
public ManifestServiceHttpAuth()
{
Type = "service_http";
}
internal static FixedFieldMap<ManifestServiceHttpAuth> handlers = new()
{
{ "type", (o,v) => {o.Type = v.GetString(); } },
{ "instructions", (o,v) => {o.Instructions = v.GetString(); } },
};
public override void Write(Utf8JsonWriter writer) {
writer.WriteStartObject();
writer.WriteString("type", Type);
writer.WriteString("instructions", Instructions);
writer.WriteEndObject();
}
}




66 changes: 66 additions & 0 deletions src/lib/OpenAI/OpenAIPluginManifest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@

using System.Text.Json;

namespace Microsoft.OpenApi.ApiManifest.OpenAI;

public class OpenAIPluginManifest
{
public string? SchemaVersion { get; set; }
public string? NameForHuman { get; set; }
public string? NameForModel { get; set; }
public string? DescriptionForHuman { get; set; }
public string? DescriptionForModel { get; set; }
public BaseManifestAuth? Auth { get; set; }
public Api? Api { get; set; }
public string? LogoUrl { get; set; }
public string? ContactEmail { get; set; }
public string? LegalInfoUrl { get; set; }


public static OpenAIPluginManifest Load(JsonElement value)
{
var manifest = new OpenAIPluginManifest();
ParsingHelpers.ParseMap(value, manifest, handlers);
return manifest;
}

// Create handlers FixedFieldMap for OpenAIPluginManifest
private static FixedFieldMap<OpenAIPluginManifest> handlers = new()
{
{ "schema_version", (o,v) => {o.SchemaVersion = v.GetString(); } },
{ "name_for_human", (o,v) => {o.NameForHuman = v.GetString(); } },
{ "name_for_model", (o,v) => {o.NameForModel = v.GetString(); } },
{ "description_for_human", (o,v) => {o.DescriptionForHuman = v.GetString(); } },
{ "description_for_model", (o,v) => {o.DescriptionForModel = v.GetString(); } },
{ "auth", (o,v) => {o.Auth = BaseManifestAuth.Load(v); } },
{ "api", (o,v) => {o.Api = Api.Load(v); } },
{ "logo_url", (o,v) => {o.LogoUrl = v.GetString(); } },
{ "contact_email", (o,v) => {o.ContactEmail = v.GetString(); } },
{ "legal_info_url", (o,v) => {o.LegalInfoUrl = v.GetString(); } },
};

//Write method
public void Write(Utf8JsonWriter writer)
{
writer.WriteStartObject();
writer.WriteString("schema_version", SchemaVersion);
writer.WriteString("name_for_human", NameForHuman);
writer.WriteString("name_for_model", NameForModel);
writer.WriteString("description_for_human", DescriptionForHuman);
writer.WriteString("description_for_model", DescriptionForModel);
if (Auth != null) {
writer.WritePropertyName("auth");
Auth.Write(writer);
}
if (Api != null) {
writer.WritePropertyName("api");
Api?.Write(writer);
}
if (LogoUrl != null)writer.WriteString("logo_url", LogoUrl);
if (ContactEmail != null) writer.WriteString("contact_email", ContactEmail);
if (LegalInfoUrl != null) writer.WriteString("legal_info_url", LegalInfoUrl);
writer.WriteEndObject();
}
}


Loading

0 comments on commit 9ef50fc

Please sign in to comment.