Skip to content

Instantly share code, notes, and snippets.

@bykof
Last active July 3, 2020 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bykof/ea1f02b9b014b907ca0edc1c39f0a160 to your computer and use it in GitHub Desktop.
Save bykof/ea1f02b9b014b907ca0edc1c39f0a160 to your computer and use it in GitHub Desktop.
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