mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-19 22:15:24 +00:00
add api package
This commit is contained in:
28
pkg/api/api.go
Normal file
28
pkg/api/api.go
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
// Package api is a helper for calling external third party apis
|
||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"encoding/json"
|
||||||
|
"io/ioutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Get(url string, rsp interface{}) error {
|
||||||
|
resp, err := http.Get(url)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
b, err := ioutil.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
return fmt.Errorf("Non 200 response %v: %v", resp.StatusCode, string(b))
|
||||||
|
}
|
||||||
|
|
||||||
|
return json.Unmarshal(b, rsp)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user