Skip to content

Commit

Permalink
activedoc create, update, delete
Browse files Browse the repository at this point in the history
  • Loading branch information
eguzki committed Dec 1, 2020
1 parent b0c8292 commit 6e8a33a
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 11 deletions.
79 changes: 79 additions & 0 deletions client/activedocs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package client

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
)
Expand Down Expand Up @@ -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)
}
9 changes: 9 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 10 additions & 11 deletions client/types.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package client

import (
"encoding/json"
"encoding/xml"
"net/http"
"net/url"
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 6e8a33a

Please sign in to comment.