Skip to content

Commit

Permalink
Merge pull request #1733 from openmeterio/tsp-go-sdk
Browse files Browse the repository at this point in the history
Generate Go SDK from new OpenAPI
  • Loading branch information
sagikazarmark authored Oct 22, 2024
2 parents 6a50601 + 1587c27 commit b73e03c
Show file tree
Hide file tree
Showing 15 changed files with 20,869 additions and 8,712 deletions.
1,401 changes: 771 additions & 630 deletions api/api.gen.go

Large diffs are not rendered by default.

28,036 changes: 20,013 additions & 8,023 deletions api/client/go/client.gen.go

Large diffs are not rendered by default.

56 changes: 30 additions & 26 deletions api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2162,11 +2162,7 @@ paths:
content:
application/json:
schema:
anyOf:
- type: array
items:
$ref: '#/components/schemas/Entitlement'
- $ref: '#/components/schemas/EntitlementPaginatedResponse'
$ref: '#/components/schemas/ListEntitlementsResult'
'400':
description: The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).
content:
Expand Down Expand Up @@ -2500,11 +2496,7 @@ paths:
content:
application/json:
schema:
anyOf:
- type: array
items:
$ref: '#/components/schemas/Feature'
- $ref: '#/components/schemas/FeaturePaginatedResponse'
$ref: '#/components/schemas/ListFeaturesResult'
'400':
description: The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).
content:
Expand Down Expand Up @@ -2892,7 +2884,7 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/CreateStripeCheckoutSessionResponse'
$ref: '#/components/schemas/CreateStripeCheckoutSessionResult'
'400':
description: The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).
content:
Expand Down Expand Up @@ -3004,7 +2996,7 @@ paths:
in: path
required: true
schema:
$ref: '#/components/schemas/OpenMeter.App.Type'
$ref: '#/components/schemas/AppType'
responses:
'200':
description: The request has succeeded.
Expand Down Expand Up @@ -3132,7 +3124,7 @@ paths:
in: path
required: true
schema:
$ref: '#/components/schemas/OpenMeter.App.Type'
$ref: '#/components/schemas/AppType'
responses:
'200':
description: The request has succeeded.
Expand Down Expand Up @@ -6881,14 +6873,14 @@ components:
required: true
description: The type of the app to install.
schema:
$ref: '#/components/schemas/OpenMeter.App.Type'
$ref: '#/components/schemas/AppType'
MarketplaceOAuth2InstallAuthorizeRequest.type:
name: type
in: path
required: true
description: The type of the app to install.
schema:
$ref: '#/components/schemas/OpenMeter.App.Type'
$ref: '#/components/schemas/AppType'
MeterQuery.filterGroupBy:
name: filterGroupBy
in: query
Expand Down Expand Up @@ -7339,6 +7331,11 @@ components:
- ready
- unauthorized
description: App installed status.
AppType:
type: string
enum:
- stripe
description: Type of the app.
BadRequestProblemResponse:
type: object
allOf:
Expand Down Expand Up @@ -8546,7 +8543,7 @@ components:
title: The ID of the app, if not specified the type is used to find the default app
type:
allOf:
- $ref: '#/components/schemas/OpenMeter.App.Type'
- $ref: '#/components/schemas/AppType'
title: The type of the app, if specified the default app is used
description: AppReference can be used to reference an app during creation only.
BillingWorkflowCollectionAlignment:
Expand Down Expand Up @@ -8737,7 +8734,7 @@ components:
items:
type: string
description: Create Stripe checkout session options
CreateStripeCheckoutSessionResponse:
CreateStripeCheckoutSessionResult:
type: object
required:
- customerId
Expand Down Expand Up @@ -9266,7 +9263,7 @@ components:
description: The amount to grant. Should be a positive number.
priority:
type: integer
format: int8
format: uint8
example: 1
minimum: 1
maximum: 255
Expand Down Expand Up @@ -9355,7 +9352,7 @@ components:
description: The amount to grant. Should be a positive number.
priority:
type: integer
format: int8
format: uint8
example: 1
minimum: 1
maximum: 255
Expand Down Expand Up @@ -10301,6 +10298,18 @@ components:
$ref: '#/components/schemas/BillingInvoiceSummary'
description: The items in the current page.
description: Paginated response
ListEntitlementsResult:
oneOf:
- type: array
items:
$ref: '#/components/schemas/Entitlement'
- $ref: '#/components/schemas/EntitlementPaginatedResponse'
ListFeaturesResult:
oneOf:
- type: array
items:
$ref: '#/components/schemas/Feature'
- $ref: '#/components/schemas/FeaturePaginatedResponse'
MarketplaceListing:
type: object
required:
Expand All @@ -10312,7 +10321,7 @@ components:
properties:
type:
allOf:
- $ref: '#/components/schemas/OpenMeter.App.Type'
- $ref: '#/components/schemas/AppType'
description: The app's type
name:
type: string
Expand Down Expand Up @@ -11286,11 +11295,6 @@ components:
- server_error
- temporarily_unavailable
description: OAuth2 authorization code grant error types.
OpenMeter.App.Type:
type: string
enum:
- stripe
description: Type of the app.
Percentage:
type: string
pattern: ^\-?[0-9]+(\.[0-9]+)?%$
Expand Down Expand Up @@ -12754,7 +12758,7 @@ components:
Additional properties specific to the problem type may be present.
x-go-type-import:
path: github.com/openmeterio/openmeter/pkg/models
x-go-type: models.Problem
x-go-type: models.StatusProblem
UnitPrice:
type: object
required:
Expand Down
1 change: 1 addition & 0 deletions api/spec/src/app/app.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ model ListAppsRequest extends PaginatedQuery {}
/**
* Type of the app.
*/
@friendlyName("AppType")
enum Type {
Stripe: "stripe",
}
Expand Down
6 changes: 3 additions & 3 deletions api/spec/src/app/stripe.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ interface AppStripe {
@operationId("createStripeCheckoutSession")
createCheckoutSession(@body body: CreateStripeCheckoutSessionRequest): {
@statusCode _: 201;
@body body: CreateStripeCheckoutSessionResponse;
@body body: CreateStripeCheckoutSessionResult;
} | NotFoundError | CommonErrors;
}

Expand Down Expand Up @@ -111,8 +111,8 @@ model CreateStripeCheckoutSessionRequestOptions {
/**
* Create Stripe Checkout Session response.
*/
@friendlyName("CreateStripeCheckoutSessionResponse")
model CreateStripeCheckoutSessionResponse {
@friendlyName("CreateStripeCheckoutSessionResult")
model CreateStripeCheckoutSessionResult {
/**
* The OpenMeter customer ID.
*/
Expand Down
11 changes: 10 additions & 1 deletion api/spec/src/entitlements/entitlements.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ interface Entitlements {
...OpenMeter.QueryPagination,
...OpenMeter.QueryLimitOffset,
...OpenMeter.QueryOrdering<EntitlementOrderBy>,
): Entitlement[] | OpenMeter.PaginatedResponse<Entitlement> | OpenMeter.CommonErrors;
): ListEntitlementsResult | OpenMeter.CommonErrors;

/**
* Get entitlement by id.
Expand All @@ -72,6 +72,15 @@ interface Entitlements {
): Entitlement | OpenMeter.NotFoundError | OpenMeter.CommonErrors;
}

// TODO: Remove in v2

@oneOf
@friendlyName("ListEntitlementsResult")
union ListEntitlementsResult {
Entitlement[],
OpenMeter.PaginatedResponse<Entitlement>,
}

/**
* Type of the entitlement.
*/
Expand Down
11 changes: 10 additions & 1 deletion api/spec/src/entitlements/feature.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ interface Features {
...OpenMeter.QueryPagination,
...OpenMeter.QueryLimitOffset,
...OpenMeter.QueryOrdering<FeatureOrderBy>,
): Feature[] | OpenMeter.PaginatedResponse<Feature> | OpenMeter.CommonErrors;
): ListFeaturesResult | OpenMeter.CommonErrors;

/**
* Features are either metered or static. A feature is metered if meterSlug is provided at creation.
Expand Down Expand Up @@ -120,6 +120,15 @@ model FeatureCreateInputs {
meterGroupByFilters?: Record<string>;
}

// TODO: Remove in v2

@oneOf
@friendlyName("ListFeaturesResult")
union ListFeaturesResult {
Feature[],
OpenMeter.PaginatedResponse<Feature>,
}

/**
* Represents a feature that can be enabled or disabled for a plan.
* Used both for product catalog and entitlements.
Expand Down
2 changes: 1 addition & 1 deletion api/spec/src/entitlements/grant.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ model GrantCreateInput {
@minValue(1)
@maxValue(255)
@example(1)
priority?: int8;
priority?: uint8;

/**
* Effective date for grants and anchor for recurring grants. Provided value will be ceiled to metering windowSize (minute).
Expand Down
2 changes: 1 addition & 1 deletion api/spec/src/errors.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace OpenMeter;
* Additional properties specific to the problem type may be present.
*/
@error
@extension("x-go-type", "models.Problem")
@extension("x-go-type", "models.StatusProblem")
@extension(
"x-go-type-import",
{
Expand Down
25 changes: 13 additions & 12 deletions e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ func TestCredit(t *testing.T) {
client := initClient(t)
subject := "customer-1"
meterSlug := "credit_test_meter"
var featureId *api.FeatureId
var featureId *string
var featureKey string

const waitTime = time.Second * 30
Expand Down Expand Up @@ -554,8 +554,9 @@ func TestCredit(t *testing.T) {
require.NoError(t, err)
require.Equal(t, http.StatusConflict, resp.StatusCode(), "Invalid status code [response_body=%s]", string(resp.Body))

require.NotEmpty(t, resp.ApplicationproblemJSON409.Extensions.ConflictingEntityId)
require.Equal(t, *entitlementId, resp.ApplicationproblemJSON409.Extensions.ConflictingEntityId)
require.NotNil(t, resp.ApplicationproblemJSON409)
require.NotEmpty(t, resp.ApplicationproblemJSON409.Extensions["conflictingEntityId"])
require.Equal(t, *entitlementId, resp.ApplicationproblemJSON409.Extensions["conflictingEntityId"])
})

t.Run("Create a Entitlement With Default Grants", func(t *testing.T) {
Expand All @@ -573,7 +574,7 @@ func TestCredit(t *testing.T) {
},
MeasureUsageFrom: muf,
IssueAfterReset: convert.ToPointer(100.0),
IssueAfterResetPriority: convert.ToPointer(6),
IssueAfterResetPriority: convert.ToPointer[uint8](6),
}
body := &api.CreateEntitlementJSONRequestBody{}
err = body.FromEntitlementMeteredCreateInputs(meteredEntitlement)
Expand All @@ -587,7 +588,7 @@ func TestCredit(t *testing.T) {
require.NoError(t, err)

require.Equal(t, randSubject, metered.SubjectKey)
require.Equal(t, measureUsageFrom, metered.MeasureUsageFrom)
require.Equal(t, &measureUsageFrom, metered.MeasureUsageFrom)

// fetch grants for entitlement
grantListResp, err := client.ListEntitlementGrantsWithResponse(context.Background(), randSubject, *metered.Id, nil)
Expand All @@ -607,7 +608,7 @@ func TestCredit(t *testing.T) {
randSubject := ulid.Make().String()
periodAnchor := time.Now().Truncate(time.Minute).Add(-time.Hour).In(time.UTC)
muf := &api.MeasureUsageFrom{}
err := muf.FromMeasureUsageFromEnum(api.CURRENTPERIODSTART)
err := muf.FromMeasureUsageFromPreset(api.CurrentPeriodStart)
require.NoError(t, err)
meteredEntitlement := api.EntitlementMeteredCreateInputs{
Type: "metered",
Expand All @@ -630,13 +631,13 @@ func TestCredit(t *testing.T) {
require.NoError(t, err)

require.Equal(t, randSubject, metered.SubjectKey)
require.Equal(t, periodAnchor, metered.MeasureUsageFrom)
require.Equal(t, &periodAnchor, metered.MeasureUsageFrom)
})

t.Run("Create Grant", func(t *testing.T) {
effectiveAt := time.Now().Truncate(time.Minute)

priority := 1
priority := uint8(1)
maxRolloverAmount := 100.0
minRolloverAmount := 0.0

Expand Down Expand Up @@ -768,7 +769,7 @@ func TestCredit(t *testing.T) {
require.NoError(t, err)
require.Equal(t, http.StatusOK, featureListResp.StatusCode())
require.NotNil(t, featureListResp.JSON200)
features, err := featureListResp.JSON200.AsListFeatureResponse0()
features, err := featureListResp.JSON200.AsListFeaturesResult0()
require.NoError(t, err)
require.Len(t, features, 1)

Expand Down Expand Up @@ -805,7 +806,7 @@ func TestCredit(t *testing.T) {
require.NoError(t, err)
require.Equal(t, http.StatusOK, featureListResp.StatusCode())
require.NotNil(t, featureListResp.JSON200)
features, err := featureListResp.JSON200.AsListFeatureResponse0()
features, err := featureListResp.JSON200.AsListFeaturesResult0()
require.NoError(t, err)
require.Len(t, features, 1)

Expand Down Expand Up @@ -908,7 +909,7 @@ func TestCredit(t *testing.T) {
require.Equal(t, http.StatusOK, resp.StatusCode())
require.NotNil(t, resp.JSON200)
// should be a paginated response
resBody, err := resp.JSON200.AsListEntitlementPaginatedResponse()
resBody, err := resp.JSON200.AsEntitlementPaginatedResponse()
require.NoError(t, err)

require.Equal(t, 1, resBody.Page)
Expand All @@ -927,7 +928,7 @@ func TestCredit(t *testing.T) {
require.Equal(t, http.StatusOK, resp.StatusCode())
require.NotNil(t, resp.JSON200)
// should be a paginated response
resBody, err = resp.JSON200.AsListEntitlementPaginatedResponse()
resBody, err = resp.JSON200.AsEntitlementPaginatedResponse()
require.NoError(t, err)

require.Equal(t, 1, resBody.Page)
Expand Down
6 changes: 4 additions & 2 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ default:

generate:
dagger call generate server export --path api
GOWORK=off go generate ./cmd/... # TODO: move this to dagger?
# TODO: generate clients

# TODO: move this to dagger?
GOWORK=off go generate ./cmd/...
go generate ./api/client/go

# tag and release a new version
release:
Expand Down
Loading

0 comments on commit b73e03c

Please sign in to comment.