Skip to content

Commit

Permalink
Merge pull request #871 from openmeterio/refactor/rename-openapi-methods
Browse files Browse the repository at this point in the history
refactor: rename openapi methods
  • Loading branch information
turip authored May 8, 2024
2 parents 8eb04d2 + 7c3e55a commit 0ee168f
Show file tree
Hide file tree
Showing 9 changed files with 781 additions and 782 deletions.
546 changes: 273 additions & 273 deletions api/api.gen.go

Large diffs are not rendered by default.

750 changes: 375 additions & 375 deletions api/client/go/client.gen.go

Large diffs are not rendered by default.

168 changes: 84 additions & 84 deletions api/openapi.yaml

Large diffs are not rendered by default.

49 changes: 24 additions & 25 deletions e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,36 +571,36 @@ func TestCredit(t *testing.T) {
featureId := features[0].ID

// Create grant
resp, err := client.CreateCreditGrantWithResponse(context.Background(), ledgerID, api.CreateCreditGrantRequest{
resp, err := client.CreateLedgerGrantWithResponse(context.Background(), ledgerID, api.CreateLedgerGrantRequest{
Type: credit.GrantTypeUsage,
LedgerID: ledgerID,
FeatureID: featureId,
Amount: 100,
Priority: 1,
EffectiveAt: effectiveAt,
Rollover: &api.CreditGrantRollover{
Rollover: &api.LedgerGrantRollover{
Type: credit.GrantRolloverTypeRemainingAmount,
},
Expiration: api.CreditExpirationPeriod{
Expiration: api.LedgerGrantExpirationPeriod{
Duration: "DAY",
Count: 1,
},
})
require.NoError(t, err)
require.Equal(t, http.StatusCreated, resp.StatusCode(), "Invalid status code [response_body=%s]", resp.Body)

expected := &api.CreditGrantResponse{
expected := &api.LedgerGrantBalance{
ID: resp.JSON201.ID,
LedgerID: ledgerID,
Type: credit.GrantTypeUsage,
FeatureID: featureId,
Amount: 100,
Priority: 1,
EffectiveAt: effectiveAt,
Rollover: &api.CreditGrantRollover{
Rollover: &api.LedgerGrantRollover{
Type: credit.GrantRolloverTypeRemainingAmount,
},
Expiration: api.CreditExpirationPeriod{
Expiration: api.LedgerGrantExpirationPeriod{
Duration: "DAY",
Count: 1,
},
Expand All @@ -611,7 +611,7 @@ func TestCredit(t *testing.T) {

t.Run("Balance", func(t *testing.T) {
// Get grants
grantListResp, err := client.ListCreditGrantsWithResponse(context.Background(), &api.ListCreditGrantsParams{})
grantListResp, err := client.ListLedgerGrantsWithResponse(context.Background(), &api.ListLedgerGrantsParams{})
require.NoError(t, err)
require.Equal(t, http.StatusOK, grantListResp.StatusCode())
require.NotNil(t, grantListResp.JSON200)
Expand All @@ -628,11 +628,11 @@ func TestCredit(t *testing.T) {
features := *featureListResp.JSON200
feature := features[0]

resp, err := client.GetCreditBalanceWithResponse(context.Background(), ledgerID, nil)
resp, err := client.GetLedgerBalanceWithResponse(context.Background(), ledgerID, nil)
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode())

expected := &api.CreditBalance{
expected := &api.LedgerBalance{
LedgerID: ledgerID,
Subject: subject,
Metadata: ledgerMeta,
Expand All @@ -657,7 +657,7 @@ func TestCredit(t *testing.T) {
effectiveAt, _ := time.ParseInLocation(time.RFC3339, "2024-01-01T00:02:00Z", time.UTC)

// Get grants
parentGrantListResp, err := client.ListCreditGrantsWithResponse(context.Background(), &api.ListCreditGrantsParams{})
parentGrantListResp, err := client.ListLedgerGrantsWithResponse(context.Background(), &api.ListLedgerGrantsParams{})
require.NoError(t, err)
require.Equal(t, http.StatusOK, parentGrantListResp.StatusCode())
require.NotNil(t, parentGrantListResp.JSON200)
Expand All @@ -675,45 +675,44 @@ func TestCredit(t *testing.T) {
featureId := features[0].ID

// Reset credit
resetResp, err := client.ResetCreditWithResponse(context.Background(), ledgerID, api.CreditReset{
resetResp, err := client.ResetLedgerWithResponse(context.Background(), ledgerID, api.LedgerReset{
EffectiveAt: effectiveAt,
})

require.NoError(t, err)
require.Equal(t, http.StatusCreated, resetResp.StatusCode())

reset := resetResp.JSON201
expectedReset := &api.CreditReset{
expectedReset := &api.LedgerReset{
ID: reset.ID,
LedgerID: ledgerID,
EffectiveAt: effectiveAt,
}
assert.Equal(t, expectedReset, resetResp.JSON201)

// List grants
resp, err := client.ListCreditGrantsWithResponse(context.Background(), &api.ListCreditGrantsParams{
resp, err := client.ListLedgerGrantsWithResponse(context.Background(), &api.ListLedgerGrantsParams{
LedgerID: &ledgerID,
})

require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode(), "response body: %s", string(resp.Body))

grants := *resp.JSON200
expected := &[]api.CreditGrantResponse{
expected := &[]api.LedgerGrantResponse{
{
ID: grants[0].ID,
ParentID: parentGrant.ID,
LedgerID: ledgerID,
Type: credit.GrantTypeUsage,
FeatureID: featureId,
Amount: 99,
Priority: 1,
// TODO: this should be equal to `effectiveAt` but we run into timezone issues
EffectiveAt: grants[0].EffectiveAt,
Rollover: &api.CreditGrantRollover{
ID: grants[0].ID,
ParentID: parentGrant.ID,
LedgerID: ledgerID,
Type: credit.GrantTypeUsage,
FeatureID: featureId,
Amount: 99,
Priority: 1,
EffectiveAt: effectiveAt,
Rollover: &api.LedgerGrantRollover{
Type: credit.GrantRolloverTypeRemainingAmount,
},
Expiration: api.CreditExpirationPeriod{
Expiration: api.LedgerGrantExpirationPeriod{
Duration: "DAY",
Count: 1,
},
Expand Down
4 changes: 2 additions & 2 deletions internal/server/router/credit_balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
)

// Get credit balance, GET /api/v1/ledgers/{ledgerID}/balance
func (a *Router) GetCreditBalance(w http.ResponseWriter, r *http.Request, ledgerID ulid.ULID, params api.GetCreditBalanceParams) {
ctx := contextx.WithAttr(r.Context(), "operation", "getCreditBalance")
func (a *Router) GetLedgerBalance(w http.ResponseWriter, r *http.Request, ledgerID ulid.ULID, params api.GetLedgerBalanceParams) {
ctx := contextx.WithAttr(r.Context(), "operation", "getLedgerBalance")
namespace := a.config.NamespaceManager.GetDefaultNamespace()
cutline := time.Now()

Expand Down
26 changes: 13 additions & 13 deletions internal/server/router/credit_grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
)

// List credit grants, GET /api/v1/ledgers/grants
func (a *Router) ListCreditGrants(w http.ResponseWriter, r *http.Request, params api.ListCreditGrantsParams) {
ctx := contextx.WithAttr(r.Context(), "operation", "listCreditGrants")
func (a *Router) ListLedgerGrants(w http.ResponseWriter, r *http.Request, params api.ListLedgerGrantsParams) {
ctx := contextx.WithAttr(r.Context(), "operation", "listLedgerGrants")
namespace := a.config.NamespaceManager.GetDefaultNamespace()

ledgerIDs := []ulid.ULID{}
Expand All @@ -31,7 +31,7 @@ func (a *Router) ListCreditGrants(w http.ResponseWriter, r *http.Request, params
LedgerIDs: ledgerIDs,
FromHighWatermark: true,
IncludeVoid: true,
Limit: defaultx.WithDefault(params.Limit, DefaultCreditsQueryLimit),
Limit: defaultx.WithDefault(params.Limit, DefaultLedgerQueryLimit),
})
if err != nil {
a.config.ErrorHandler.HandleContext(ctx, err)
Expand All @@ -47,16 +47,16 @@ func (a *Router) ListCreditGrants(w http.ResponseWriter, r *http.Request, params
}

// List credit grants, GET /api/v1/ledgers/{ledgerID}/grants
func (a *Router) ListCreditGrantsByLedger(w http.ResponseWriter, r *http.Request, ledgerID api.LedgerID, params api.ListCreditGrantsByLedgerParams) {
ctx := contextx.WithAttr(r.Context(), "operation", "listCreditGrants")
func (a *Router) ListLedgerGrantsByLedger(w http.ResponseWriter, r *http.Request, ledgerID api.LedgerID, params api.ListLedgerGrantsByLedgerParams) {
ctx := contextx.WithAttr(r.Context(), "operation", "listLedgerGrantsByLedger")
namespace := a.config.NamespaceManager.GetDefaultNamespace()

// Get grants
grants, err := a.config.CreditConnector.ListGrants(ctx, namespace, credit.ListGrantsParams{
LedgerIDs: []ulid.ULID{ledgerID},
FromHighWatermark: true,
IncludeVoid: true,
Limit: defaultx.WithDefault(params.Limit, DefaultCreditsQueryLimit),
Limit: defaultx.WithDefault(params.Limit, DefaultLedgerQueryLimit),
})
if err != nil {
a.config.ErrorHandler.HandleContext(ctx, err)
Expand All @@ -72,12 +72,12 @@ func (a *Router) ListCreditGrantsByLedger(w http.ResponseWriter, r *http.Request
}

// Create credit grant, POST /api/v1/ledgers/{creditSubjectId}/grants
func (a *Router) CreateCreditGrant(w http.ResponseWriter, r *http.Request, ledgerID api.LedgerID) {
ctx := contextx.WithAttr(r.Context(), "operation", "createCreditGrant")
func (a *Router) CreateLedgerGrant(w http.ResponseWriter, r *http.Request, ledgerID api.LedgerID) {
ctx := contextx.WithAttr(r.Context(), "operation", "createLedgerGrant")
namespace := a.config.NamespaceManager.GetDefaultNamespace()

// Parse request body
grant := &api.CreateCreditGrantJSONRequestBody{}
grant := &api.CreateLedgerGrantJSONRequestBody{}
if err := render.DecodeJSON(r.Body, grant); err != nil {
err := fmt.Errorf("decode json: %w", err)

Expand Down Expand Up @@ -137,8 +137,8 @@ func (a *Router) CreateCreditGrant(w http.ResponseWriter, r *http.Request, ledge
}

// Void credit grant, DELETE /api/v1/ledgers/{ledgerID}/grants/{creditGrantID}
func (a *Router) VoidCreditGrant(w http.ResponseWriter, r *http.Request, ledgerID api.LedgerID, creditGrantId api.CreditGrantID) {
ctx := contextx.WithAttr(r.Context(), "operation", "voidCreditGrant")
func (a *Router) VoidLedgerGrant(w http.ResponseWriter, r *http.Request, ledgerID api.LedgerID, creditGrantId api.LedgerGrantID) {
ctx := contextx.WithAttr(r.Context(), "operation", "voidLedgerGrant")
namespace := a.config.NamespaceManager.GetDefaultNamespace()

// Get grant
Expand Down Expand Up @@ -212,8 +212,8 @@ func (a *Router) VoidCreditGrant(w http.ResponseWriter, r *http.Request, ledgerI
}

// Get credit, GET /api/v1/ledgers/{ledgerID}/grants/{creditGrantId}
func (a *Router) GetCreditGrant(w http.ResponseWriter, r *http.Request, ledgerID api.LedgerID, creditGrantId api.CreditGrantID) {
ctx := contextx.WithAttr(r.Context(), "operation", "getCreditGrant")
func (a *Router) GetLedgerGrant(w http.ResponseWriter, r *http.Request, ledgerID api.LedgerID, creditGrantId api.LedgerGrantID) {
ctx := contextx.WithAttr(r.Context(), "operation", "getLedgerGrant")
namespace := a.config.NamespaceManager.GetDefaultNamespace()

// Get grant
Expand Down
10 changes: 5 additions & 5 deletions internal/server/router/credit_ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ import (
)

// Get credit balance, GET /api/v1/ledgers/{ledgerID}/history
func (a *Router) GetCreditHistory(w http.ResponseWriter, r *http.Request, ledgerID api.LedgerID, params api.GetCreditHistoryParams) {
ctx := contextx.WithAttr(r.Context(), "operation", "getCreditLedger")
func (a *Router) GetLedgerHistory(w http.ResponseWriter, r *http.Request, ledgerID api.LedgerID, params api.GetLedgerHistoryParams) {
ctx := contextx.WithAttr(r.Context(), "operation", "getLedgerHistory")
namespace := a.config.NamespaceManager.GetDefaultNamespace()

// Get Ledger
ledgerEntries, err := a.config.CreditConnector.GetHistory(
ctx, namespace, ledgerID,
params.From,
defaultx.WithDefault(params.To, time.Now()),
defaultx.WithDefault(params.Limit, DefaultCreditsQueryLimit),
defaultx.WithDefault(params.Limit, DefaultLedgerQueryLimit),
)
if err != nil {
a.config.ErrorHandler.HandleContext(ctx, err)
Expand All @@ -39,7 +39,7 @@ func (a *Router) GetCreditHistory(w http.ResponseWriter, r *http.Request, ledger

// CreateLedger POS /api/v1/ledgers
func (a *Router) CreateLedger(w http.ResponseWriter, r *http.Request) {
ctx := contextx.WithAttr(r.Context(), "operation", "createCreditLedger")
ctx := contextx.WithAttr(r.Context(), "operation", "createLedger")
namespace := a.config.NamespaceManager.GetDefaultNamespace()

// Parse request body
Expand Down Expand Up @@ -93,7 +93,7 @@ func (a *Router) ListLedgers(w http.ResponseWriter, r *http.Request, params api.
credit.ListLedgersParams{
Subjects: defaultx.WithDefault(params.Subject, nil),
Offset: defaultx.WithDefault(params.Offset, 0),
Limit: defaultx.WithDefault(params.Limit, DefaultCreditsQueryLimit),
Limit: defaultx.WithDefault(params.Limit, DefaultLedgerQueryLimit),
})
if err != nil && !db.IsNotFound(err) {
a.config.ErrorHandler.HandleContext(ctx, err)
Expand Down
4 changes: 2 additions & 2 deletions internal/server/router/credit_limits.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package router

const (
// DefaultCreditQueryLimit specifies how many entries to return by default for credit related queries
DefaultCreditsQueryLimit = 1000
// DefaultLedgerQueryLimit specifies how many entries to return by default for credit related queries
DefaultLedgerQueryLimit = 1000
)
6 changes: 3 additions & 3 deletions internal/server/router/credit_reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import (
)

// Resets the credit POST /api/v1/ledgers/{ledgerID}/reset
func (a *Router) ResetCredit(w http.ResponseWriter, r *http.Request, ledgerID api.LedgerID) {
ctx := contextx.WithAttr(r.Context(), "operation", "resetCredit")
func (a *Router) ResetLedger(w http.ResponseWriter, r *http.Request, ledgerID api.LedgerID) {
ctx := contextx.WithAttr(r.Context(), "operation", "resetLedger")
namespace := a.config.NamespaceManager.GetDefaultNamespace()

// Parse request body
resetIn := &api.ResetCreditJSONRequestBody{}
resetIn := &api.ResetLedgerJSONRequestBody{}
if err := render.DecodeJSON(r.Body, resetIn); err != nil {
err := fmt.Errorf("decode json: %w", err)

Expand Down

0 comments on commit 0ee168f

Please sign in to comment.