Skip to content

Commit

Permalink
Initial version of API.Post function.
Browse files Browse the repository at this point in the history
  • Loading branch information
adlio committed Aug 23, 2017
1 parent 6ea59b7 commit 451b625
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package harvest

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -62,3 +63,48 @@ func (a *API) Get(path string, args Arguments, target interface{}) error {

return nil
}

func (a *API) Post(path string, args Arguments, postData interface{}, target interface{}) error {
url := fmt.Sprintf("%s%s", a.BaseURL, path)
urlWithParams := fmt.Sprintf("%s?%s", url, args.ToURLValues().Encode())

buffer := new(bytes.Buffer)
if postData != nil {
json.NewEncoder(buffer).Encode(postData)
fmt.Printf("%v", buffer)
}

req, err := http.NewRequest("POST", urlWithParams, buffer)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
if a.User != "" && a.Password != "" {
req.SetBasicAuth(a.User, a.Password)
}
if err != nil {
return errors.Wrapf(err, "Invalid POST request %s", url)
}

resp, err := a.client.Do(req)
if err != nil {
return errors.Wrapf(err, "HTTP request failure on %s", url)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
var body []byte
body, err = ioutil.ReadAll(resp.Body)
return errors.Wrapf(err, "HTTP request failure on %s: %s %s", url, string(body), err)
}

decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(target)
if err != nil {
body, _ := ioutil.ReadAll(resp.Body)
if len(body) == 0 {
return nil
} else {
return errors.Wrapf(err, "JSON decode failed on %s: %s %s", url, string(body), err)
}
}

return nil
}

0 comments on commit 451b625

Please sign in to comment.