-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add endpoint for retrieving customer credits
- Loading branch information
Showing
8 changed files
with
189 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package billing | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/porter-dev/porter/api/server/handlers" | ||
"github.com/porter-dev/porter/api/server/shared" | ||
"github.com/porter-dev/porter/api/server/shared/apierrors" | ||
"github.com/porter-dev/porter/api/server/shared/config" | ||
"github.com/porter-dev/porter/api/types" | ||
"github.com/porter-dev/porter/internal/models" | ||
"github.com/porter-dev/porter/internal/telemetry" | ||
) | ||
|
||
// GetCreditsHandler is a handler for getting available credits | ||
type GetCreditsHandler struct { | ||
handlers.PorterHandlerWriter | ||
} | ||
|
||
// NewGetCreditsHandler will create a new GetCreditsHandler | ||
func NewGetCreditsHandler( | ||
config *config.Config, | ||
writer shared.ResultWriter, | ||
) *GetCreditsHandler { | ||
return &GetCreditsHandler{ | ||
PorterHandlerWriter: handlers.NewDefaultPorterHandler(config, nil, writer), | ||
} | ||
} | ||
|
||
func (c *GetCreditsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
ctx, span := telemetry.NewSpan(r.Context(), "get-credits-endpoint") | ||
defer span.End() | ||
|
||
proj, _ := ctx.Value(types.ProjectScope).(*models.Project) | ||
|
||
credits, err := c.Config().BillingManager.MetronomeClient.GetCustomerCredits(proj.UsageID) | ||
if err != nil { | ||
err := telemetry.Error(ctx, span, err, "error getting credits") | ||
c.HandleAPIError(w, r, apierrors.NewErrInternal(err)) | ||
return | ||
} | ||
|
||
c.WriteResult(w, r, credits) | ||
} |
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