diff --git a/client/activedocs.go b/client/activedocs.go index dd1b256..c4e82fb 100644 --- a/client/activedocs.go +++ b/client/activedocs.go @@ -1,6 +1,9 @@ package client import ( + "bytes" + "encoding/json" + "errors" "fmt" "net/http" ) @@ -47,3 +50,79 @@ func (c *ThreeScaleClient) ActiveDoc(id int64) (*ActiveDoc, error) { err = handleJsonResp(resp, http.StatusOK, activeDoc) return activeDoc, err } + +// CreateActiveDoc Create 3scale activedoc +func (c *ThreeScaleClient) CreateActiveDoc(activeDoc *ActiveDoc) (*ActiveDoc, error) { + bodyArr, err := json.Marshal(activeDoc.Element) + if err != nil { + return nil, err + } + body := bytes.NewReader(bodyArr) + + req, err := c.buildPostJSONReq(activeDocListEndpoint, body) + if err != nil { + return nil, err + } + + resp, err := c.httpClient.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + respObj := &ActiveDoc{} + err = handleJsonResp(resp, http.StatusCreated, respObj) + return respObj, err +} + +// UpdateActiveDoc Update existing activedoc +func (c *ThreeScaleClient) UpdateActiveDoc(activeDoc *ActiveDoc) (*ActiveDoc, error) { + if activeDoc == nil { + return nil, errors.New("UpdateActiveDoc needs not nil pointer") + } + + if activeDoc.Element.ID == nil { + return nil, errors.New("UpdateActiveDoc needs not nil ID") + } + + endpoint := fmt.Sprintf(activeDocEndpoint, *activeDoc.Element.ID) + + bodyArr, err := json.Marshal(activeDoc.Element) + if err != nil { + return nil, err + } + body := bytes.NewReader(bodyArr) + + req, err := c.buildUpdateJSONReq(endpoint, body) + if err != nil { + return nil, err + } + + resp, err := c.httpClient.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + respObj := &ActiveDoc{} + err = handleJsonResp(resp, http.StatusOK, respObj) + return respObj, err +} + +// DeleteActiveDoc Delete existing activedoc +func (c *ThreeScaleClient) DeleteActiveDoc(id int64) error { + endpoint := fmt.Sprintf(activeDocEndpoint, id) + + req, err := c.buildDeleteReq(endpoint, nil) + if err != nil { + return err + } + + resp, err := c.httpClient.Do(req) + if err != nil { + return err + } + defer resp.Body.Close() + + return handleJsonResp(resp, http.StatusOK, nil) +} diff --git a/client/client.go b/client/client.go index 44b205e..b8fb75b 100644 --- a/client/client.go +++ b/client/client.go @@ -114,6 +114,15 @@ func (c *ThreeScaleClient) buildPostReq(ep string, body io.Reader) (*http.Reques return req, err } +// Request builder for POST request to the provided endpoint +func (c *ThreeScaleClient) buildPostJSONReq(ep string, body io.Reader) (*http.Request, error) { + req, err := http.NewRequest("POST", c.adminPortal.rawURL+ep, body) + req.Header.Set("Accept", "application/json") + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Authorization", "Basic "+basicAuth("", c.credential)) + return req, err +} + // Request builder for PUT request to the provided endpoint func (c *ThreeScaleClient) buildUpdateReq(ep string, body io.Reader) (*http.Request, error) { req, err := http.NewRequest("PUT", c.adminPortal.rawURL+ep, body) diff --git a/client/types.go b/client/types.go index eb16dcd..a200639 100644 --- a/client/types.go +++ b/client/types.go @@ -1,7 +1,6 @@ package client import ( - "encoding/json" "encoding/xml" "net/http" "net/url" @@ -652,16 +651,16 @@ type OIDCConfiguration struct { } type ActiveDocItem struct { - ID *int64 `json:"id,omitempty"` - SystemName *string `json:"system_name,omitempty"` - Name *string `json:"name,omitempty"` - Description *string `json:"description,omitempty"` - Published *bool `json:"published,omitempty"` - SkipSwaggerValidations *bool `json:"skip_swagger_validations,omitempty"` - Body *json.RawMessage `json:"body,omitempty"` - ServiceID *int64 `json:"service_id,omitempty"` - CreatedAt *string `json:"created_at,omitempty"` - UpdatedAt *string `json:"updated_at,omitempty"` + ID *int64 `json:"id,omitempty"` + SystemName *string `json:"system_name,omitempty"` + Name *string `json:"name,omitempty"` + Description *string `json:"description,omitempty"` + Published *bool `json:"published,omitempty"` + SkipSwaggerValidations *bool `json:"skip_swagger_validations,omitempty"` + Body *string `json:"body,omitempty"` + ServiceID *int64 `json:"service_id,omitempty"` + CreatedAt *string `json:"created_at,omitempty"` + UpdatedAt *string `json:"updated_at,omitempty"` } type ActiveDoc struct {