Skip to content

Commit

Permalink
improvement: make sdk version optional and default to latest (#734)
Browse files Browse the repository at this point in the history
  • Loading branch information
armandobelardo authored May 1, 2024
1 parent 8fafd26 commit 9c40cde
Show file tree
Hide file tree
Showing 35 changed files with 528 additions and 66 deletions.
41 changes: 38 additions & 3 deletions fern/apis/fdr/definition/snippets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ service:
docs: |
If you have more than one API, you must specify its ID.
sdks:
type: optional<list<SDK>>
type: optional<list<SDKRequest>>
docs: |
The SDKs for which to load snippets. If unspecified,
snippets for the latest published SDKs will be returned.
Expand Down Expand Up @@ -116,7 +116,7 @@ service:
docs: |
If you have more than one API, you must specify its ID.
sdks:
type: optional<list<SDK>>
type: optional<list<SDKRequest>>
docs: |
The SDKs for which to load snippets. If unspecified,
snippets for the latest published SDKs will be returned.
Expand All @@ -140,7 +140,7 @@ service:
sdks:
- type: python
package: vellum-ai
version: 1.2.1
# version: 1.2.1
response:
body:
next: 2
Expand Down Expand Up @@ -206,6 +206,41 @@ service:
})
types:
SDKRequest:
base-properties:
version: optional<string>
union:
typescript: TypeScriptSDKRequest
python: PythonSDKRequest
go: GoSDKRequest
ruby: RubySDKRequest
java: JavaSDKRequest

TypeScriptSDKRequest:
properties:
package: string

PythonSDKRequest:
properties:
package: string

GoSDKRequest:
properties:
githubRepo: string

RubySDKRequest:
properties:
gem: string

JavaSDKRequest:
properties:
group:
type: string
docs: The maven repository group (e.g. `com.stripe.java`)
artifact:
type: string
docs: The artifact (e.g. `stripe-java`)

SDK:
union:
typescript: TypeScriptSDK
Expand Down
2 changes: 1 addition & 1 deletion fern/apis/fdr/definition/templates.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ service:
type: commons.ApiId
docs: |
The API name.
sdk: snippets.SDK
sdk: snippets.SDKRequest
endpointId: commons.EndpointIdentifier
response: EndpointSnippetTemplate

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export interface GetSnippetRequest {
* snippets for the latest published SDKs will be returned.
*
*/
sdks?: FernRegistry.Sdk[];
sdks?: FernRegistry.SdkRequest[];
endpoint: FernRegistry.EndpointIdentifier;
/**
* The JSON payload to be used as the input for the code snippet. This should just be thought of as the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import * as FernRegistry from "../../../..";
* apiId: "vellum-ai",
* sdks: [{
* type: "python",
* package: "vellum-ai",
* version: "1.2.1"
* package: "vellum-ai"
* }]
* }
*/
Expand All @@ -35,5 +34,5 @@ export interface ListSnippetsRequest {
* snippets for the latest published SDKs will be returned.
*
*/
sdks?: FernRegistry.Sdk[];
sdks?: FernRegistry.SdkRequest[];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* This file was auto-generated by Fern from our API Definition.
*/

export interface GoSdkRequest {
githubRepo: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* This file was auto-generated by Fern from our API Definition.
*/

export interface JavaSdkRequest {
/** The maven repository group (e.g. `com.stripe.java`) */
group: string;
/** The artifact (e.g. `stripe-java`) */
artifact: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* This file was auto-generated by Fern from our API Definition.
*/

export interface PythonSdkRequest {
package: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* This file was auto-generated by Fern from our API Definition.
*/

export interface RubySdkRequest {
gem: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* This file was auto-generated by Fern from our API Definition.
*/

import * as FernRegistry from "../../..";

export type SdkRequest =
| FernRegistry.SdkRequest.Typescript
| FernRegistry.SdkRequest.Python
| FernRegistry.SdkRequest.Go
| FernRegistry.SdkRequest.Ruby
| FernRegistry.SdkRequest.Java;

export declare namespace SdkRequest {
interface Typescript extends FernRegistry.TypeScriptSdkRequest, _Base {
type: "typescript";
}

interface Python extends FernRegistry.PythonSdkRequest, _Base {
type: "python";
}

interface Go extends FernRegistry.GoSdkRequest, _Base {
type: "go";
}

interface Ruby extends FernRegistry.RubySdkRequest, _Base {
type: "ruby";
}

interface Java extends FernRegistry.JavaSdkRequest, _Base {
type: "java";
}

interface _Base {
version?: string;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* This file was auto-generated by Fern from our API Definition.
*/

export interface TypeScriptSdkRequest {
package: string;
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export * from "./SdkRequest";
export * from "./TypeScriptSdkRequest";
export * from "./PythonSdkRequest";
export * from "./GoSdkRequest";
export * from "./RubySdkRequest";
export * from "./JavaSdkRequest";
export * from "./Sdk";
export * from "./TypeScriptSdk";
export * from "./PythonSdk";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ export interface GetSnippetTemplate {
*
*/
apiId: FernRegistry.ApiId;
sdk: FernRegistry.Sdk;
sdk: FernRegistry.SdkRequest;
endpointId: FernRegistry.EndpointIdentifier;
}
74 changes: 70 additions & 4 deletions servers/fdr/src/__test__/local/services/snippetTemplates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FdrAPI } from "@fern-api/fdr-sdk";
import { inject } from "vitest";
import { FernRegistry } from "../../../api/generated";
import { CHAT_COMPLETION_PAYLOAD, CHAT_COMPLETION_SNIPPET } from "../../octo";
import { getClient } from "../util";
import { getAPIResponse, getClient } from "../util";

const ENDPOINT: FdrAPI.EndpointIdentifier = {
path: "/users/v1",
Expand Down Expand Up @@ -71,23 +71,89 @@ it("generate example from snippet template", async () => {
orgId,
apiId,
apiDefinitionId: "....",
snippet: CHAT_COMPLETION_SNIPPET,
snippet: CHAT_COMPLETION_SNIPPET("0.0.5"),
});
// create snippets
await fdr.templates.get({
orgId,
apiId,
endpointId: CHAT_COMPLETION_SNIPPET.endpointId,
endpointId: CHAT_COMPLETION_SNIPPET("0.0.5").endpointId,
sdk,
});

const response = await fdr.snippets.get({
orgId,
apiId,
endpoint: CHAT_COMPLETION_SNIPPET.endpointId,
endpoint: CHAT_COMPLETION_SNIPPET("0.0.5").endpointId,
sdks: [sdk],
payload: CHAT_COMPLETION_PAYLOAD,
});
expect(response.ok).toBe(true);
console.log(JSON.stringify(response, null, 2));
});

it("fallback to version", async () => {
const unauthedFdr = getClient({ authed: false, url: inject("url") });
const fdr = getClient({ authed: true, url: inject("url") });

const orgId = "octoai";
const apiId = "api";
const sdk: FernRegistry.Sdk = {
type: "python",
package: "octoai",
version: "0.0.6",
};
const genericRequest: FernRegistry.SdkRequest = {
type: "python",
package: "octoai",
};

// register API definition for acme org
const reg = await unauthedFdr.templates.register({
orgId,
apiId,
apiDefinitionId: "....",
snippet: CHAT_COMPLETION_SNIPPET("0.0.6"),
});
expect(reg.ok).toBe(true);
// create snippets
const template = getAPIResponse(
await fdr.templates.get({
orgId,
apiId,
endpointId: CHAT_COMPLETION_SNIPPET("0.0.6").endpointId,
sdk: genericRequest,
}),
);
expect(template.sdk.version).toBe("0.0.6");

// register API definition for acme org
const regAgain = await unauthedFdr.templates.register({
orgId,
apiId,
apiDefinitionId: "....",
snippet: CHAT_COMPLETION_SNIPPET("0.0.122"),
});
expect(regAgain.ok).toBe(true);
// create snippets
const templateAgain = getAPIResponse(
await fdr.templates.get({
orgId,
apiId,
endpointId: CHAT_COMPLETION_SNIPPET("0.0.122").endpointId,
sdk: genericRequest,
}),
);
expect(templateAgain.sdk.version).toBe("0.0.122");

const templateSpecify = await fdr.templates.get({
orgId,
apiId,
endpointId: CHAT_COMPLETION_SNIPPET("0.0.6").endpointId,
sdk,
});
expect(templateSpecify.ok).toBe(true);
if (templateSpecify.ok) {
expect(templateSpecify.body.sdk.version).toBe("0.0.6");
}
});
6 changes: 3 additions & 3 deletions servers/fdr/src/__test__/octo.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { FdrAPI } from "@fern-api/fdr-sdk";

export const CHAT_COMPLETION_SNIPPET: FdrAPI.EndpointSnippetTemplate = {
export const CHAT_COMPLETION_SNIPPET = (version: string): FdrAPI.EndpointSnippetTemplate => ({
endpointId: {
path: "/v1/chat/completions",
method: "POST",
},
sdk: {
type: "python",
package: "octoai",
version: "0.0.5",
version,
},
snippetTemplate: {
type: "v1",
Expand Down Expand Up @@ -624,7 +624,7 @@ export const CHAT_COMPLETION_SNIPPET: FdrAPI.EndpointSnippetTemplate = {
clientInstantiation:
'from octoai.client import AsyncOctoAI\n\nclient = AsyncOctoAI(\n api_key="YOUR_API_KEY",\n)\n',
},
};
});

export const CHAT_COMPLETION_PAYLOAD: FdrAPI.CustomSnippetPayload = {
headers: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as FernRegistry from "../../../..";
export interface GetSnippetRequest {
orgId?: FernRegistry.OrgId;
apiId?: FernRegistry.ApiId;
sdks?: FernRegistry.Sdk[];
sdks?: FernRegistry.SdkRequest[];
endpoint: FernRegistry.EndpointIdentifier;
payload?: FernRegistry.CustomSnippetPayload;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import * as FernRegistry from "../../../..";
export interface ListSnippetsRequest {
orgId?: FernRegistry.OrgId;
apiId?: FernRegistry.ApiId;
sdks?: FernRegistry.Sdk[];
sdks?: FernRegistry.SdkRequest[];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* This file was auto-generated by Fern from our API Definition.
*/
export interface GoSdkRequest {
githubRepo: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* This file was auto-generated by Fern from our API Definition.
*/
export {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* This file was auto-generated by Fern from our API Definition.
*/
export interface JavaSdkRequest {
/** The maven repository group (e.g. `com.stripe.java`) */
group: string;
/** The artifact (e.g. `stripe-java`) */
artifact: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* This file was auto-generated by Fern from our API Definition.
*/
export {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* This file was auto-generated by Fern from our API Definition.
*/
export interface PythonSdkRequest {
package: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* This file was auto-generated by Fern from our API Definition.
*/
export {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* This file was auto-generated by Fern from our API Definition.
*/
export interface RubySdkRequest {
gem: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* This file was auto-generated by Fern from our API Definition.
*/
export {};
Loading

0 comments on commit 9c40cde

Please sign in to comment.