github.com/akamai/AkamaiOPEN-edgegrid-golang@v1.2.2/api-endpoints-v2/endpoints.go (about)

     1  package apiendpoints
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  
     7  	"github.com/akamai/AkamaiOPEN-edgegrid-golang/client-v1"
     8  	"github.com/google/go-querystring/query"
     9  )
    10  
    11  type Endpoints []Endpoint
    12  
    13  type Endpoint struct {
    14  	APICategoryIds             []int                 `json:"apiCategoryIds,omitempty"`
    15  	APIEndPointHosts           []string              `json:"apiEndPointHosts"`
    16  	APIEndPointID              int                   `json:"apiEndPointId,omitempty"`
    17  	APIEndPointLocked          bool                  `json:"apiEndPointLocked,omitempty"`
    18  	APIEndPointName            string                `json:"apiEndPointName"`
    19  	APIEndPointScheme          string                `json:"apiEndPointScheme,omitempty"`
    20  	APIResourceBaseInfo        []*ResourceBaseInfo   `json:"apiResourceBaseInfo,omitempty"`
    21  	BasePath                   string                `json:"basePath,omitempty"`
    22  	ClonedFromVersion          *int                  `json:"clonedFromVersion,omitempty"`
    23  	ConsumeType                string                `json:"consumeType,omitempty"`
    24  	ContractID                 string                `json:"contractId,omitempty"`
    25  	CreateDate                 string                `json:"createDate,omitempty"`
    26  	CreatedBy                  string                `json:"createdBy,omitempty"`
    27  	Description                string                `json:"description,omitempty"`
    28  	GroupID                    int                   `json:"groupId,omitempty"`
    29  	ProductionVersion          *VersionSummary       `json:"productionVersion,omitempty"`
    30  	ProductionStatus           string                `json:"productionStatus,omitempty"`
    31  	ProtectedByAPIKey          bool                  `json:"protectedByApiKey,omitempty"`
    32  	StagingStatus              string                `json:"stagingStatus,omitempty"`
    33  	StagingVersion             *VersionSummary       `json:"stagingVersion,omitempty"`
    34  	UpdateDate                 string                `json:"updateDate,omitempty"`
    35  	UpdatedBy                  string                `json:"updatedBy,omitempty"`
    36  	VersionNumber              int                   `json:"versionNumber,omitempty"`
    37  	SecurityScheme             *SecurityScheme       `json:"securityScheme,omitempty"`
    38  	AkamaiSecurityRestrictions *SecurityRestrictions `json:"akamaiSecurityRestrictions,omitempty"`
    39  	APIResources               *Resources            `json:"apiResources,omitempty"`
    40  }
    41  
    42  type SecurityScheme struct {
    43  	SecuritySchemeType   string                `json:"securitySchemeType,omitempty"`
    44  	SecuritySchemeDetail *SecuritySchemeDetail `json:"securitySchemeDetail,omitempty"`
    45  }
    46  
    47  type SecuritySchemeDetail struct {
    48  	APIKeyLocation string `json:"apiKeyLocation,omitempty"`
    49  	APIKeyName     string `json:"apiKeyName,omitempty"`
    50  }
    51  
    52  type SecurityRestrictions struct {
    53  	MaxJsonxmlElement       int `json:"MAX_JSONXML_ELEMENT,omitempty"`
    54  	MaxElementNameLength    int `json:"MAX_ELEMENT_NAME_LENGTH,omitempty"`
    55  	MaxDocDepth             int `json:"MAX_DOC_DEPTH,omitempty"`
    56  	PositiveSecurityEnabled int `json:"POSITIVE_SECURITY_ENABLED,omitempty"`
    57  	MaxStringLength         int `json:"MAX_STRING_LENGTH,omitempty"`
    58  	MaxBodySize             int `json:"MAX_BODY_SIZE,omitempty"`
    59  	MaxIntegerValue         int `json:"MAX_INTEGER_VALUE,omitempty"`
    60  }
    61  
    62  type CreateEndpointOptions struct {
    63  	ContractId string   `json:"contractId,omitempty"`
    64  	GroupId    int      `json:"groupId,omitempty"`
    65  	Name       string   `json:"apiEndPointName,omitempty"`
    66  	BasePath   string   `json:"basePath,omitempty"`
    67  	Hostnames  []string `json:"apiEndPointHosts,omitempty"`
    68  }
    69  
    70  func CreateEndpoint(options *CreateEndpointOptions) (*Endpoint, error) {
    71  	req, err := client.NewJSONRequest(
    72  		Config,
    73  		"POST",
    74  		"/api-definitions/v2/endpoints",
    75  		options,
    76  	)
    77  
    78  	return call(req, err)
    79  }
    80  
    81  type CreateEndpointFromFileOptions struct {
    82  	File       string
    83  	Format     string
    84  	ContractId string
    85  	GroupId    int
    86  }
    87  
    88  func CreateEndpointFromFile(options *CreateEndpointFromFileOptions) (*Endpoint, error) {
    89  	req, err := client.NewMultiPartFormDataRequest(
    90  		Config,
    91  		"/api-definitions/v2/endpoints/files",
    92  		options.File,
    93  		map[string]string{
    94  			"contractId":       options.ContractId,
    95  			"groupId":          strconv.Itoa(options.GroupId),
    96  			"importFileFormat": options.Format,
    97  		},
    98  	)
    99  
   100  	return call(req, err)
   101  }
   102  
   103  type UpdateEndpointFromFileOptions struct {
   104  	EndpointId int
   105  	Version    int
   106  	File       string
   107  	Format     string
   108  }
   109  
   110  func UpdateEndpointFromFile(options *UpdateEndpointFromFileOptions) (*Endpoint, error) {
   111  	url := fmt.Sprintf(
   112  		"/api-definitions/v2/endpoints/%d/versions/%d/file",
   113  		options.EndpointId,
   114  		options.Version,
   115  	)
   116  
   117  	req, err := client.NewMultiPartFormDataRequest(
   118  		Config,
   119  		url,
   120  		options.File,
   121  		map[string]string{
   122  			"importFileFormat": options.Format,
   123  		},
   124  	)
   125  
   126  	return call(req, err)
   127  }
   128  
   129  type ListEndpointOptions struct {
   130  	ContractId        string `url:"contractId,omitempty"`
   131  	GroupId           int    `url:"groupId,omitempty"`
   132  	Category          string `url:"category,omitempty"`
   133  	Contains          string `url:"contains,omitempty"`
   134  	Page              int    `url:"page,omitempty"`
   135  	PageSize          int    `url:"pageSize,omitempty"`
   136  	Show              string `url:show,omitempty`
   137  	SortBy            string `url:"sortBy,omitempty"`
   138  	SortOrder         string `url:"sortOrder,omitempty"`
   139  	VersionPreference string `url:"versionPreference,omitempty"`
   140  }
   141  
   142  type EndpointList struct {
   143  	APIEndPoints Endpoints `json:"apiEndPoints"`
   144  	Links        Links     `json:"links"`
   145  	Page         int       `json:"page"`
   146  	PageSize     int       `json:"pageSize"`
   147  	TotalSize    int       `json:"totalSize"`
   148  }
   149  
   150  func (list *EndpointList) ListEndpoints(options *ListEndpointOptions) error {
   151  	q, err := query.Values(options)
   152  	if err != nil {
   153  		return err
   154  	}
   155  
   156  	url := fmt.Sprintf(
   157  		"/api-definitions/v2/endpoints?%s",
   158  		q.Encode(),
   159  	)
   160  
   161  	req, err := client.NewJSONRequest(Config, "GET", url, nil)
   162  	if err != nil {
   163  		return err
   164  	}
   165  
   166  	res, err := client.Do(Config, req)
   167  	if err != nil {
   168  		return err
   169  	}
   170  
   171  	if client.IsError(res) {
   172  		return client.NewAPIError(res)
   173  	}
   174  
   175  	if err = client.BodyJSON(res, list); err != nil {
   176  		return err
   177  	}
   178  
   179  	return nil
   180  }
   181  
   182  func RemoveEndpoint(endpointId int) (*Endpoint, error) {
   183  	req, err := client.NewJSONRequest(
   184  		Config,
   185  		"DELETE",
   186  		fmt.Sprintf(
   187  			"/api-definitions/v2/endpoints/%d",
   188  			endpointId,
   189  		),
   190  		nil,
   191  	)
   192  
   193  	if err != nil {
   194  		return nil, err
   195  	}
   196  
   197  	res, err := client.Do(Config, req)
   198  
   199  	if client.IsError(res) {
   200  		return nil, client.NewAPIError(res)
   201  	}
   202  
   203  	rep := &Endpoint{}
   204  	return rep, nil
   205  }