add vehicle api (#221)

* add vehicle api

* Commit from GitHub Actions (Publish APIs & Clients)

Co-authored-by: asim <asim@users.noreply.github.com>
This commit is contained in:
Asim Aslam
2021-10-04 12:14:31 +01:00
committed by GitHub
parent ec9fc27f24
commit 9b6010372e
22 changed files with 884 additions and 1 deletions

View File

@@ -2,6 +2,7 @@
package api
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
@@ -45,3 +46,41 @@ func Get(url string, rsp interface{}) error {
return json.Unmarshal(b, rsp)
}
// Post a url and unmarshal a json body into the given value
func Post(url string, ureq, rsp interface{}) error {
b, err := json.Marshal(ureq)
if err != nil {
return err
}
req, err := http.NewRequest("POST", url, bytes.NewReader(b))
if err != nil {
return err
}
for k, v := range keys {
req.Header.Set(k, v)
}
if v := req.Header.Get("Content-Type"); len(v) == 0 {
req.Header.Set("Content-Type", "application/json")
}
resp, err := http.DefaultClient.Do(req)
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)
}