github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/cloudwrapper/locations.go (about)

     1  package cloudwrapper
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  	"net/http"
     8  )
     9  
    10  type (
    11  	// Locations is the cloudwrapper location API interface
    12  	Locations interface {
    13  		// ListLocations returns a list of locations available to distribute Cloud Wrapper capacity
    14  		//
    15  		// See: https://techdocs.akamai.com/cloud-wrapper/reference/get-locations
    16  		ListLocations(context.Context) (*ListLocationResponse, error)
    17  	}
    18  
    19  	// ListLocationResponse represents a response object returned by ListLocations
    20  	ListLocationResponse struct {
    21  		Locations []Location `json:"locations"`
    22  	}
    23  
    24  	// Location represents a Location object
    25  	Location struct {
    26  		LocationID         int               `json:"locationId"`
    27  		LocationName       string            `json:"locationName"`
    28  		MultiCDNLocationID string            `json:"multiCdnLocationId"`
    29  		TrafficTypes       []TrafficTypeItem `json:"trafficTypes"`
    30  	}
    31  
    32  	// TrafficTypeItem represents a TrafficType object for the location
    33  	TrafficTypeItem struct {
    34  		TrafficTypeID int    `json:"trafficTypeId"`
    35  		TrafficType   string `json:"trafficType"`
    36  		MapName       string `json:"mapName"`
    37  	}
    38  )
    39  
    40  var (
    41  	// ErrListLocations is returned when ListLocations fails
    42  	ErrListLocations = errors.New("list locations")
    43  )
    44  
    45  func (c *cloudwrapper) ListLocations(ctx context.Context) (*ListLocationResponse, error) {
    46  	url := "/cloud-wrapper/v1/locations"
    47  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
    48  	if err != nil {
    49  		return nil, fmt.Errorf("%w: failed to create request:\n%s", ErrListLocations, err)
    50  	}
    51  
    52  	var locations ListLocationResponse
    53  	resp, err := c.Exec(req, &locations)
    54  	if err != nil {
    55  		return nil, fmt.Errorf("%w: request failed:\n%s", ErrListLocations, err)
    56  	}
    57  
    58  	if resp.StatusCode != http.StatusOK {
    59  		return nil, fmt.Errorf("%s: %w", ErrListLocations, c.Error(resp))
    60  	}
    61  
    62  	return &locations, nil
    63  }