Skip to content

Commit

Permalink
Merge pull request #19 from dstdfx/feature/get-restrictions-api
Browse files Browse the repository at this point in the history
Geo Restrictions API support
  • Loading branch information
dstdfx authored Apr 5, 2019
2 parents 5a0c78b + afee7bb commit 63ebe97
Show file tree
Hide file tree
Showing 3 changed files with 448 additions and 1 deletion.
4 changes: 3 additions & 1 deletion gcore/gcore.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ type CommonServices struct {

// ResellerServices represent specific account type features.
type ResellerServices struct {
Clients *ClientsService
Clients *ClientsService
GeoRestrictions *GeoRestrictionsService
}

// AuthOptions is G-Core account credentials.
Expand Down Expand Up @@ -186,6 +187,7 @@ func NewResellerClient(logger ...log.GenericLogger) *ResellerClient {

resellerServices := ResellerServices{}
resellerServices.Clients = (*ClientsService)(&c.common)
resellerServices.GeoRestrictions = (*GeoRestrictionsService)(&c.common)
resellClient := &ResellerClient{
Client: c,
ResellerServices: resellerServices,
Expand Down
102 changes: 102 additions & 0 deletions gcore/georestrictions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package gcore

import (
"context"
"fmt"
"net/http"
)

const (
geoRestrictionsBaseURL = "/admin"
geoRestrictionsListRegionsURL = geoRestrictionsBaseURL + "/billing_regions"
geoRestrictionsGetSetRestrictionsURL = geoRestrictionsBaseURL + "/clients/%d"
)

// GeoRestrictionsService handles communication with geo-restrictions related methods
// of the G-Core CDN API.
type GeoRestrictionsService service

// Region represents the region which is utilized in content delivery.
type Region struct {
// ID represents the region identifier.
ID int `json:"id"`

// Name represents region abbreviation.
Name string `json:"name"`

// Description represents the region full name.
Description string `json:"description"`

// Required flag indicates if the region is might be excluded from the content delivery.
Required bool `json:"required"`
}

// GeoRestrictions represents the list of regions that are utilized
// in content delivery for a client.
type GeoRestrictions struct {
// IsIn flag shows if regions are utilized in the content delivery for a client or not:
// true — only regions from the list are utilized in the content delivery for the client.
// false — regions from the list are not participating in the content delivery for the client
IsIn bool `json:"is_in"`

// RegionList represents the list of regions IDs that are utilized
// in the content delivery for a client.
// Numbers 1-7 represent a region.
RegionList []int `json:"region_list"`
}

// ListRegions method returns the regions that are available for content delivery.
func (s *GeoRestrictionsService) ListRegions(ctx context.Context) ([]*Region, *http.Response, error) {
req, err := s.client.NewRequest(ctx, http.MethodGet, geoRestrictionsListRegionsURL, nil)
if err != nil {
return nil, nil, err
}

regions := make([]*Region, 0)

resp, err := s.client.Do(req, &regions)
if err != nil {
return nil, resp, err
}
return regions, resp, nil
}

// GetRestrictions method returns current client's geo restrictions.
// Returns 404 response code if restrictions haven't been set before.
func (s *GeoRestrictionsService) GetRestrictions(ctx context.Context,
clientID int) (*GeoRestrictions, *http.Response, error) {

req, err := s.client.NewRequest(ctx,
http.MethodGet,
fmt.Sprintf(geoRestrictionsGetSetRestrictionsURL, clientID), nil)
if err != nil {
return nil, nil, err
}

restrict := &GeoRestrictions{}

resp, err := s.client.Do(req, restrict)
if err != nil {
return nil, resp, err
}
return restrict, resp, nil
}

// SetRestrictions method limits the regions for the content delivery for a client.
// In case of success it returns 204 response body.
func (s *GeoRestrictionsService) SetRestrictions(ctx context.Context,
clientID int, restrictions *GeoRestrictions) (*http.Response, error) {

req, err := s.client.NewRequest(ctx,
http.MethodPost,
fmt.Sprintf(geoRestrictionsGetSetRestrictionsURL, clientID), restrictions)
if err != nil {
return nil, err
}

resp, err := s.client.Do(req, nil)
if err != nil {
return resp, err
}
return resp, nil
}
Loading

0 comments on commit 63ebe97

Please sign in to comment.