-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
927 additions
and
645 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
namespace OpenMeter.Static; | ||
|
||
using TypeSpec.Http; | ||
using TypeSpec.OpenAPI; | ||
|
||
/** | ||
* Currency describes a currency supported by OpenMeter. | ||
*/ | ||
@friendlyName("Currency") | ||
model Currency { | ||
/** | ||
* The currency ISO code. | ||
*/ | ||
code: CurrencyCode; | ||
|
||
/** | ||
* The currency name. | ||
*/ | ||
name: string; | ||
|
||
/** | ||
* The currency symbol. | ||
*/ | ||
symbol: string; | ||
|
||
/** | ||
* Subunit of the currency. | ||
*/ | ||
subunits: uint32; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import "@typespec/http"; | ||
import "@typespec/openapi"; | ||
import "@typespec/openapi3"; | ||
|
||
import ".."; | ||
|
||
// Package Contents | ||
import "./currencies.tsp"; | ||
import "./routes.tsp"; | ||
|
||
namespace OpenMeter.Static; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import "../rest.tsp"; | ||
|
||
namespace OpenMeter.Static; | ||
|
||
using TypeSpec.Http; | ||
using TypeSpec.OpenAPI; | ||
|
||
@route("/api/v1/static") | ||
@tag("Static Data") | ||
interface Currencies { | ||
/** | ||
* List all supported currencies. | ||
*/ | ||
@get | ||
@operationId("listCurrencies") | ||
@route("/currencies") | ||
@summary("List supported currencies") | ||
listCurrencies(): Currency[] | OpenMeter.CommonErrors; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package router | ||
|
||
import "net/http" | ||
|
||
// List supported currencies | ||
// (GET /api/v1/static/currencies) | ||
func (a *Router) ListCurrencies(w http.ResponseWriter, r *http.Request) { | ||
a.staticHandler.ListCurrencies().ServeHTTP(w, r) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package httpdriver | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/invopop/gobl/currency" | ||
"github.com/samber/lo" | ||
|
||
"github.com/openmeterio/openmeter/api" | ||
"github.com/openmeterio/openmeter/pkg/framework/commonhttp" | ||
"github.com/openmeterio/openmeter/pkg/framework/transport/httptransport" | ||
) | ||
|
||
type ( | ||
ListCurrenciesRequest struct{} | ||
ListCurrenciesResponse []api.Currency | ||
ListCurrenciesHandler httptransport.Handler[ListCurrenciesRequest, ListCurrenciesResponse] | ||
) | ||
|
||
func (h *handler) ListCurrencies() ListCurrenciesHandler { | ||
return httptransport.NewHandler( | ||
func(ctx context.Context, r *http.Request) (ListCurrenciesRequest, error) { | ||
return ListCurrenciesRequest{}, nil | ||
}, | ||
func(ctx context.Context, request ListCurrenciesRequest) (ListCurrenciesResponse, error) { | ||
defs := currency.Definitions() | ||
|
||
return lo.Map(defs, func(def *currency.Def, _ int) api.Currency { | ||
return api.Currency{ | ||
Code: api.CurrencyCode(def.ISOCode), | ||
Name: def.Name, | ||
Symbol: def.Symbol, | ||
Subunits: def.Subunits, | ||
} | ||
}), nil | ||
}, | ||
commonhttp.JSONResponseEncoderWithStatus[ListCurrenciesResponse](http.StatusOK), | ||
httptransport.AppendOptions( | ||
h.options, | ||
httptransport.WithOperationName("listCurrencies"), | ||
httptransport.WithErrorEncoder(errorEncoder()), | ||
)..., | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package httpdriver | ||
|
||
import "github.com/openmeterio/openmeter/pkg/framework/transport/httptransport" | ||
|
||
type Handler interface { | ||
ListCurrencies() ListCurrenciesHandler | ||
} | ||
|
||
type handler struct { | ||
options []httptransport.HandlerOption | ||
} | ||
|
||
func New(options ...httptransport.HandlerOption) Handler { | ||
return &handler{ | ||
options: options, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package httpdriver | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/openmeterio/openmeter/pkg/framework/commonhttp" | ||
"github.com/openmeterio/openmeter/pkg/framework/transport/httptransport" | ||
"github.com/openmeterio/openmeter/pkg/models" | ||
) | ||
|
||
func errorEncoder() httptransport.ErrorEncoder { | ||
return func(ctx context.Context, err error, w http.ResponseWriter, r *http.Request) bool { | ||
return commonhttp.HandleErrorIfTypeMatches[*models.GenericUserError](ctx, http.StatusBadRequest, err, w) | ||
} | ||
} |