diff --git a/client/client.go b/client/client.go index 88f6d73..92d12de 100644 --- a/client/client.go +++ b/client/client.go @@ -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) diff --git a/client/product.go b/client/product.go index 20f14cb..b08e7b4 100644 --- a/client/product.go +++ b/client/product.go @@ -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{} diff --git a/client/product_test.go b/client/product_test.go index 4b267ed..be48312 100644 --- a/client/product_test.go +++ b/client/product_test.go @@ -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) + } +}