Skip to content

Commit

Permalink
registry: Add a method to get your registry subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwg committed Nov 2, 2020
1 parent 0e940b8 commit f9ae7dd
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
27 changes: 27 additions & 0 deletions registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type RegistryService interface {
ListGarbageCollections(context.Context, string, *ListOptions) ([]*GarbageCollection, *Response, error)
UpdateGarbageCollection(context.Context, string, string, *UpdateGarbageCollectionRequest) (*GarbageCollection, *Response, error)
GetOptions(context.Context) (*RegistryOptions, *Response, error)
GetSubscription(context.Context) (*RegistrySubscription, *Response, error)
}

var _ RegistryService = &RegistryServiceOp{}
Expand Down Expand Up @@ -146,6 +147,17 @@ type RegistrySubscriptionTier struct {
EligibilityReasons []string `json:"eligibility_reasons,omitempty"`
}

// RegistrySubscription is a user's subscription.
type RegistrySubscription struct {
Tier *RegistrySubscriptionTier `json:"tier"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}

type registrySubscriptionRoot struct {
Subscription *RegistrySubscription `json:"subscription"`
}

// Get retrieves the details of a Registry.
func (svc *RegistryServiceOp) Get(ctx context.Context) (*Registry, *Response, error) {
req, err := svc.client.NewRequest(ctx, http.MethodGet, registryPath, nil)
Expand Down Expand Up @@ -414,3 +426,18 @@ func (svc *RegistryServiceOp) GetOptions(ctx context.Context) (*RegistryOptions,

return root.Options, resp, nil
}

// GetSubscription retrieves the user's subscription.
func (svc *RegistryServiceOp) GetSubscription(ctx context.Context) (*RegistrySubscription, *Response, error) {
path := fmt.Sprintf("%s/subscription", registryPath)
req, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(registrySubscriptionRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Subscription, resp, nil
}
45 changes: 45 additions & 0 deletions registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -594,3 +594,48 @@ func TestRegistry_GetOptions(t *testing.T) {
require.NoError(t, err)
require.Equal(t, want, got)
}

func TestRegistry_GetSubscription(t *testing.T) {
setup()
defer teardown()

want := &RegistrySubscription{
Tier: &RegistrySubscriptionTier{
Name: "Basic",
Slug: "basic",
IncludedRepositories: 5,
IncludedStorageBytes: 5368709120,
AllowStorageOverage: true,
IncludedBandwidthBytes: 5368709120,
MonthlyPriceInCents: 500,
},
CreatedAt: testTime,
UpdatedAt: testTime,
}

getResponseJSON := `
{
"subscription": {
"tier": {
"name": "Basic",
"slug": "basic",
"included_repositories": 5,
"included_storage_bytes": 5368709120,
"allow_storage_overage": true,
"included_bandwidth_bytes": 5368709120,
"monthly_price_in_cents": 500
},
"created_at": "` + testTimeString + `",
"updated_at": "` + testTimeString + `"
}
}
`

mux.HandleFunc("/v2/registry/subscription", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, getResponseJSON)
})
got, _, err := client.Registry.GetSubscription(ctx)
require.NoError(t, err)
require.Equal(t, want, got)
}

0 comments on commit f9ae7dd

Please sign in to comment.