Skip to content

Commit b126dd6

Browse files
committed
Init
Signed-off-by: Levi Gross <[email protected]>
1 parent aebccf3 commit b126dd6

File tree

4 files changed

+136
-0
lines changed

4 files changed

+136
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@ _testmain.go
2222
*.exe
2323
*.test
2424
*.prof
25+
26+
.idea/
27+
*.iml

base.go

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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 }

base_test.go

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package grequests
2+
3+
import (
4+
"testing"
5+
)
6+
7+
type BasicGetResponse struct {
8+
Args struct{} `json:"args"`
9+
Headers struct {
10+
Accept string `json:"Accept"`
11+
Accept_Encoding string `json:"Accept-Encoding"`
12+
Accept_Language string `json:"Accept-Language"`
13+
Dnt string `json:"Dst"`
14+
Host string `json:"Host"`
15+
User_Agent string `json:"User-Agent"`
16+
} `json:"headers"`
17+
Origin string `json:"origin"`
18+
URL string `json:"url"`
19+
}
20+
21+
func TestGetNoOptions(t *testing.T) {
22+
resp := <-Get("http://httpbin.org/get", nil)
23+
if resp.Error != nil {
24+
t.Error("Unable to make request", resp.Error)
25+
}
26+
myJsonStruct := &BasicGetResponse{}
27+
28+
err := resp.JSON(myJsonStruct)
29+
if err != nil {
30+
t.Error("Unable to coerce to JSON", err)
31+
}
32+
33+
if myJsonStruct.URL != "http://httpbin.org/get" {
34+
t.Error("For some reason the URL isn't the same", myJsonStruct.URL)
35+
}
36+
}
37+
38+
func TestGetNoOptionsChannel(t *testing.T) {
39+
respChan := Get("http://httpbin.org/get", nil)
40+
select {
41+
case resp := <-respChan:
42+
if resp.Error != nil {
43+
t.Error("Unable to make request", resp.Error)
44+
}
45+
myJsonStruct := &BasicGetResponse{}
46+
47+
err := resp.JSON(myJsonStruct)
48+
if err != nil {
49+
t.Error("Unable to coerce to JSON", err)
50+
}
51+
52+
if myJsonStruct.URL != "http://httpbin.org/get" {
53+
t.Error("For some reason the URL isn't the same", myJsonStruct.URL)
54+
}
55+
56+
}
57+
58+
}

utils.go

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package grequests

0 commit comments

Comments
 (0)