-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #900 from openmeterio/refactor/use-http-framework
refactor: use http framework for features
- Loading branch information
Showing
64 changed files
with
1,681 additions
and
927 deletions.
There are no files selected for viewing
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
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,63 @@ | ||
package creditdriver | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/openmeterio/openmeter/api" | ||
"github.com/openmeterio/openmeter/internal/credit" | ||
"github.com/openmeterio/openmeter/pkg/defaultx" | ||
"github.com/openmeterio/openmeter/pkg/framework/commonhttp" | ||
"github.com/openmeterio/openmeter/pkg/framework/transport/httptransport" | ||
) | ||
|
||
type GetLedgerBalaceHandlerParams struct { | ||
LedgerID api.LedgerID | ||
QueryParams api.GetLedgerBalanceParams | ||
} | ||
|
||
type GetLedgerBalanceRequest struct { | ||
LedgerID credit.NamespacedID | ||
Cutline time.Time | ||
} | ||
|
||
type GetLedgerBalanceHandler httptransport.HandlerWithArgs[GetLedgerBalanceRequest, credit.Balance, GetLedgerBalaceHandlerParams] | ||
|
||
func (b *builder) GetLedgerBalance() GetLedgerBalanceHandler { | ||
return httptransport.NewHandlerWithArgs[GetLedgerBalanceRequest, credit.Balance, GetLedgerBalaceHandlerParams]( | ||
func(ctx context.Context, r *http.Request, queryIn GetLedgerBalaceHandlerParams) (GetLedgerBalanceRequest, error) { | ||
ns, err := b.resolveNamespace(ctx) | ||
if err != nil { | ||
return GetLedgerBalanceRequest{}, err | ||
} | ||
|
||
return GetLedgerBalanceRequest{ | ||
LedgerID: credit.NewNamespacedID(ns, queryIn.LedgerID), | ||
Cutline: defaultx.WithDefault(queryIn.QueryParams.Time, time.Now()), | ||
}, nil | ||
}, | ||
func(ctx context.Context, request GetLedgerBalanceRequest) (credit.Balance, error) { | ||
return b.CreditConnector.GetBalance(ctx, request.LedgerID, request.Cutline) | ||
}, | ||
commonhttp.JSONResponseEncoder[credit.Balance], | ||
httptransport.AppendOptions( | ||
b.Options, | ||
httptransport.WithOperationName("getLedgerBalance"), | ||
httptransport.WithErrorEncoder(func(ctx context.Context, err error, w http.ResponseWriter) bool { | ||
if _, ok := err.(*credit.HighWatermarBeforeError); ok { | ||
commonhttp.NewHTTPError(http.StatusBadRequest, err).EncodeError(ctx, w) | ||
return true | ||
} | ||
if _, ok := err.(*credit.LedgerNotFoundError); ok { | ||
commonhttp.NewHTTPError(http.StatusNotFound, err).EncodeError(ctx, w) | ||
return true | ||
} | ||
if _, ok := err.(*credit.LockErrNotObtainedError); ok { | ||
commonhttp.NewHTTPError(http.StatusConflict, err).EncodeError(ctx, w) | ||
return true | ||
} | ||
return false | ||
}))..., | ||
) | ||
} |
2 changes: 1 addition & 1 deletion
2
internal/server/router/credit_limits.go → internal/credit/creditdriver/const.go
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
Oops, something went wrong.