Skip to content

Commit

Permalink
product read endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
eguzki committed Nov 20, 2020
1 parent cf7bdea commit 5d5b63b
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
8 changes: 8 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ func (c *ThreeScaleClient) buildGetReq(ep string) (*http.Request, error) {
return req, err
}

// Request builder for GET request to the provided endpoint for json payloads
func (c *ThreeScaleClient) buildGetJSONReq(ep string) (*http.Request, error) {
req, err := http.NewRequest("GET", c.adminPortal.rawURL+ep, nil)
req.Header.Set("Accept", "application/json")
req.Header.Set("Authorization", "Basic "+basicAuth("", c.credential))
return req, err
}

// Request builder for POST request to the provided endpoint
func (c *ThreeScaleClient) buildPostReq(ep string, body io.Reader) (*http.Request, error) {
req, err := http.NewRequest("POST", c.adminPortal.rawURL+ep, body)
Expand Down
20 changes: 20 additions & 0 deletions client/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,26 @@ const (
productProxyDeployResourceEndpoint = "/admin/api/services/%d/proxy/deploy.json"
)

// BackendApi Read 3scale Backend
func (c *ThreeScaleClient) Product(id int64) (*Product, error) {
endpoint := fmt.Sprintf(productResourceEndpoint, id)

req, err := c.buildGetJSONReq(endpoint)
if err != nil {
return nil, err
}

resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

product := &Product{}
err = handleJsonResp(resp, http.StatusOK, product)
return product, err
}

// CreateProduct Create 3scale Product
func (c *ThreeScaleClient) CreateProduct(name string, params Params) (*Product, error) {
values := url.Values{}
Expand Down
49 changes: 49 additions & 0 deletions client/product_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -955,3 +955,52 @@ func TestDeployProductProxy(t *testing.T) {
t.Fatalf("Endpoint does not match. Expected [%s]; got [%s]", productionEndpoint, obj.Element.Endpoint)
}
}

func TestReadProduct(t *testing.T) {
var (
productID int64 = 98765
endpoint = fmt.Sprintf(productResourceEndpoint, productID)
product = &Product{
Element: ProductItem{
ID: productID,
Name: "myProduct",
},
}
)

httpClient := NewTestClient(func(req *http.Request) *http.Response {
if req.URL.Path != endpoint {
t.Fatalf("Path does not match. Expected [%s]; got [%s]", endpoint, req.URL.Path)
}

if req.Method != http.MethodGet {
t.Fatalf("Method does not match. Expected [%s]; got [%s]", http.MethodGet, req.Method)
}

responseBodyBytes, err := json.Marshal(*product)
if err != nil {
t.Fatal(err)
}

return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewBuffer(responseBodyBytes)),
Header: make(http.Header),
}
})

credential := "someAccessToken"
c := NewThreeScale(NewTestAdminPortal(t), credential, httpClient)
obj, err := c.Product(productID)
if err != nil {
t.Fatal(err)
}

if obj == nil {
t.Fatal("backendapi returned nil")
}

if *obj != *product {
t.Fatalf("Expected %v; got %v", *product, *obj)
}
}

0 comments on commit 5d5b63b

Please sign in to comment.