Commit 11933054acf481aff93c2216d5d233aad635b0f6

Authored by Marko Tikvić
1 parent 46b2215ebd
Exists in master

Added Get, Post and Put methods

Showing 2 changed files with 76 additions and 7 deletions   Show diff stats
1 1 package webutility
2 2  
3 3 import (
  4 + "bytes"
4 5 "encoding/json"
5 6 "fmt"
  7 + "io"
6 8 "net/http"
  9 + "net/url"
7 10 )
8 11  
9 12 // StatusRecorder ...
... ... @@ -150,3 +153,76 @@ func InternalServerError(w http.ResponseWriter, r *http.Request, err string) {
150 153 Error(w, r, http.StatusInternalServerError, err)
151 154 }
152 155  
  156 +// DecodeJSON decodes JSON data from r to v.
  157 +// Returns an error if it fails.
  158 +func DecodeJSON(r io.Reader, v interface{}) error {
  159 + return json.NewDecoder(r).Decode(v)
  160 +}
  161 +
  162 +func GetJSON(url string, v interface{}, params url.Values, headers http.Header) (status int, err error) {
  163 + p := params.Encode()
  164 + if p != "" {
  165 + url += "?" + p
  166 + }
  167 +
  168 + req, err := http.NewRequest(http.MethodGet, url, nil)
  169 + if err != nil {
  170 + return 0, err
  171 + }
  172 +
  173 + if headers != nil {
  174 + for k, head := range headers {
  175 + for i, h := range head {
  176 + if i == 0 {
  177 + req.Header.Set(k, h)
  178 + } else {
  179 + req.Header.Add(k, h)
  180 + }
  181 + }
  182 + }
  183 + }
  184 +
  185 + resp, err := http.DefaultClient.Do(req)
  186 + if err != nil {
  187 + return 0, err
  188 + }
  189 + status = resp.StatusCode
  190 +
  191 + return status, DecodeJSON(resp.Body, v)
  192 +}
  193 +
  194 +func PostJSON(url string, v, r interface{}, params url.Values, headers http.Header) (status int, err error) {
  195 + buffer := bytes.NewBuffer(make([]byte, 0))
  196 + json.NewEncoder(buffer).Encode(v)
  197 +
  198 + p := params.Encode()
  199 + if p != "" {
  200 + url += "?" + p
  201 + }
  202 +
  203 + req, err := http.NewRequest(http.MethodPost, url, buffer)
  204 + if err != nil {
  205 + return 0, err
  206 + }
  207 +
  208 + if headers != nil {
  209 + for k, head := range headers {
  210 + for i, h := range head {
  211 + if i == 0 {
  212 + req.Header.Set(k, h)
  213 + } else {
  214 + req.Header.Add(k, h)
  215 + }
  216 + }
  217 + }
  218 + }
  219 + req.Header.Set("Content-Type", "application/json")
  220 +
  221 + resp, err := http.DefaultClient.Do(req)
  222 + if err != nil {
  223 + return 0, err
  224 + }
  225 + status = resp.StatusCode
  226 +
  227 + return status, DecodeJSON(resp.Body, r)
  228 +}
... ...
... ... @@ -5,7 +5,6 @@ import (
5 5 "encoding/json"
6 6 "errors"
7 7 "fmt"
8   - "io"
9 8 "net/http"
10 9 "strings"
11 10 "sync"
... ... @@ -122,12 +121,6 @@ func NewPayload(r *http.Request, key string) Payload {
122 121 return p
123 122 }
124 123  
125   -// DecodeJSON decodes JSON data from r to v.
126   -// Returns an error if it fails.
127   -func DecodeJSON(r io.Reader, v interface{}) error {
128   - return json.NewDecoder(r).Decode(v)
129   -}
130   -
131 124 // InitPayloadsMetadata loads all payloads' information into 'metadata' variable.
132 125 func InitPayloadsMetadata(drv string, db *sql.DB, project string) error {
133 126 var err error
... ...