|
| 1 | +package grequests |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | +) |
| 8 | + |
| 9 | +// RequestOptions is the location that of where the data |
| 10 | +type RequestOptions struct { |
| 11 | + // Data is a map of key values that will eventually convert into the query string of a GET request or the |
| 12 | + // body of a POST request. Items can be passed in as an interface (which makes the map easier to construct) |
| 13 | + Data map[string]interface{} |
| 14 | + // VerifySSL is a flag that specifies if we should validate the server's TLS certificate. It should be noted that |
| 15 | + // Go's TLS verify mechanism doesn't validate if a certificate has been revoked |
| 16 | + VerifyTLS bool |
| 17 | + UserAgent string |
| 18 | +} |
| 19 | + |
| 20 | +type Response struct { |
| 21 | + // Ok is a boolean flag that validates that the server returned a 2xx code |
| 22 | + Ok bool |
| 23 | + // This is the Go error flag – if something went wrong within the request, this flag will be set. |
| 24 | + Error error |
| 25 | + // We want to abstract (at least at the moment) the Go http.Response object away. So we are going to make use of it |
| 26 | + // internal but not give the user access |
| 27 | + resp *http.Response |
| 28 | +} |
| 29 | + |
| 30 | +func buildResponse(resp *http.Response, err error) *Response { |
| 31 | + // If the connection didn't succeed we just return a blank response |
| 32 | + if err != nil { |
| 33 | + return &Response{Error: err} |
| 34 | + } |
| 35 | + |
| 36 | + resp.Close = true // This will close the connection |
| 37 | + |
| 38 | + return &Response{ |
| 39 | + Ok: resp.StatusCode <= 200 && resp.StatusCode > 300, |
| 40 | + Error: nil, |
| 41 | + resp: resp, |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func (r *Response) JSON(userStruct interface{}) error { |
| 46 | + jsonDecoder := json.NewDecoder(r.resp.Body) |
| 47 | + defer r.resp.Body.Close() |
| 48 | + |
| 49 | + if err := jsonDecoder.Decode(&userStruct); err != nil && err != io.EOF { |
| 50 | + return err |
| 51 | + } |
| 52 | + |
| 53 | + return nil |
| 54 | +} |
| 55 | + |
| 56 | +// GET takes 2 parameters and returns a Response channel. These two options are: |
| 57 | +// 1. A URL |
| 58 | +// 2. A RequestOptions struct |
| 59 | +func Get(url string, ro *RequestOptions) chan *Response { |
| 60 | + responseChan := make(chan *Response) |
| 61 | + go func() { |
| 62 | + if ro == nil { |
| 63 | + responseChan <- buildResponse(http.Get(url)) |
| 64 | + } |
| 65 | + |
| 66 | + }() |
| 67 | + return responseChan |
| 68 | +} |
| 69 | + |
| 70 | +func Put(url string, ro *RequestOptions) chan *Response { return nil } |
| 71 | +func Post(url string, ro *RequestOptions) chan *Response { return nil } |
| 72 | +func Delete(url string, ro *RequestOptions) chan *Response { return nil } |
| 73 | +func Head(url string, ro *RequestOptions) chan *Response { return nil } |
| 74 | +func Options(url string, ro *RequestOptions) chan *Response { return nil } |
0 commit comments