Last active
July 3, 2020 12:29
-
-
Save bykof/ea1f02b9b014b907ca0edc1c39f0a160 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package infrastructure | |
import ( | |
"encoding/json" | |
"example.com/dingo_example/domain" | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
) | |
type ConcreteProductAPI struct { | |
ApiUrl string | |
) | |
func (cpa *ConcreteProductAPI) Get(endpoint string) ([]byte, error) { | |
resp, err := http.Get(fmt.Sprintf("%s%s", cpa.apiUrl, endpoint)) | |
if err != nil { | |
return nil, err | |
} | |
defer resp.Body.Close() | |
body, _ := ioutil.ReadAll(resp.Body) | |
return body, nil | |
} | |
func (cpa *ConcreteProductAPI) ProductList() (domain.ProductList, error) { | |
var err error | |
var productList domain.ProductList | |
body, err := cpa.Get("/products") | |
if err != nil { | |
return productList, err | |
} | |
err = json.Unmarshal(body, &productList) | |
if err != nil { | |
return productList, err | |
} | |
return productList, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment