| |
|
|
| package openmeter |
|
|
| import ( |
| "context" |
| "fmt" |
| "iter" |
| "net/http" |
| "net/url" |
| ) |
|
|
| type CustomersService struct { |
| client *Client |
| Billing *CustomersBillingService |
| Credits *CustomersCreditsService |
| Charges *CustomersChargesService |
| } |
|
|
| type CustomerFilter struct { |
| Key *StringFilter |
| Name *StringFilter |
| PrimaryEmail *StringFilter |
| UsageAttributionSubjectKey *StringFilter |
| PlanKey *StringFilter |
| BillingProfileID *StringExactFilter |
| } |
|
|
| type CustomerListParams struct { |
| Page *PageParams |
| Sort *Sort |
| Filter *CustomerFilter |
| } |
|
|
| func (p CustomerListParams) values() url.Values { |
| q := url.Values{} |
|
|
| addPageParams(q, p.Page) |
|
|
| addSort(q, "sort", p.Sort) |
|
|
| if p.Filter != nil { |
| addStringFilter(q, "filter[key]", p.Filter.Key) |
| addStringFilter(q, "filter[name]", p.Filter.Name) |
| addStringFilter(q, "filter[primary_email]", p.Filter.PrimaryEmail) |
| addStringFilter(q, "filter[usage_attribution_subject_key]", p.Filter.UsageAttributionSubjectKey) |
| addStringFilter(q, "filter[plan_key]", p.Filter.PlanKey) |
| addStringExactFilter(q, "filter[billing_profile_id]", p.Filter.BillingProfileID) |
| } |
|
|
| return q |
| } |
|
|
| func (s *CustomersService) Create(ctx context.Context, request CreateCustomerRequest) (*Customer, error) { |
| path := "/openmeter/customers" |
|
|
| req, err := s.client.newRequestWithContentType(ctx, http.MethodPost, path, nil, request, "application/json", "application/json") |
| if err != nil { |
| return nil, err |
| } |
|
|
| var out Customer |
| if err := s.client.doJSON(req, &out); err != nil { |
| return nil, err |
| } |
|
|
| return &out, nil |
| } |
|
|
| func (s *CustomersService) Get(ctx context.Context, customerID string) (*Customer, error) { |
| if customerID == "" { |
| return nil, fmt.Errorf("openmeter: %s must not be empty: %w", "customerID", ErrEmptyID) |
| } |
|
|
| path := "/openmeter/customers/{customerId}" |
|
|
| path = replacePathParam(path, "customerId", customerID) |
|
|
| req, err := s.client.newRequestWithContentType(ctx, http.MethodGet, path, nil, nil, "", "application/json") |
| if err != nil { |
| return nil, err |
| } |
|
|
| var out Customer |
| if err := s.client.doJSON(req, &out); err != nil { |
| return nil, err |
| } |
|
|
| return &out, nil |
| } |
|
|
| func (s *CustomersService) List(ctx context.Context, params CustomerListParams) (*CustomerPagePaginatedResponse, error) { |
| path := "/openmeter/customers" |
|
|
| req, err := s.client.newRequestWithContentType(ctx, http.MethodGet, path, params.values(), nil, "", "application/json") |
| if err != nil { |
| return nil, err |
| } |
|
|
| var out CustomerPagePaginatedResponse |
| if err := s.client.doJSON(req, &out); err != nil { |
| return nil, err |
| } |
|
|
| return &out, nil |
| } |
|
|
| |
| func (s *CustomersService) ListAll(ctx context.Context, params CustomerListParams) iter.Seq2[Customer, error] { |
| return paginate(params.Page, func(page, size int) ([]Customer, int, error) { |
| pageParams := params |
| pageParams.Page = &PageParams{Size: Int(size), Number: Int(page)} |
|
|
| resp, err := s.List(ctx, pageParams) |
| if err != nil { |
| return nil, 0, err |
| } |
|
|
| return resp.Data, resp.Meta.Page.Total, nil |
| }) |
| } |
|
|
| func (s *CustomersService) Upsert(ctx context.Context, customerID string, request UpsertCustomerRequest) (*Customer, error) { |
| if customerID == "" { |
| return nil, fmt.Errorf("openmeter: %s must not be empty: %w", "customerID", ErrEmptyID) |
| } |
|
|
| path := "/openmeter/customers/{customerId}" |
|
|
| path = replacePathParam(path, "customerId", customerID) |
|
|
| req, err := s.client.newRequestWithContentType(ctx, http.MethodPut, path, nil, request, "application/json", "application/json") |
| if err != nil { |
| return nil, err |
| } |
|
|
| var out Customer |
| if err := s.client.doJSON(req, &out); err != nil { |
| return nil, err |
| } |
|
|
| return &out, nil |
| } |
|
|
| func (s *CustomersService) Delete(ctx context.Context, customerID string) error { |
| if customerID == "" { |
| return fmt.Errorf("openmeter: %s must not be empty: %w", "customerID", ErrEmptyID) |
| } |
|
|
| path := "/openmeter/customers/{customerId}" |
|
|
| path = replacePathParam(path, "customerId", customerID) |
|
|
| req, err := s.client.newRequestWithContentType(ctx, http.MethodDelete, path, nil, nil, "", "") |
| if err != nil { |
| return err |
| } |
|
|
| _, err = s.client.doRaw(req) |
| return err |
| } |
|
|