Skip to content

Commit 3dcd106

Browse files
committed
Improvements, test case and read update
1 parent f09e94b commit 3dcd106

18 files changed

+399
-73
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
# resty [![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
1+
# resty [![Build Status](https://travis-ci.org/go-resty/resty.svg?branch=master)](https://travis-ci.org/go-resty/resty) [![GoCover](http://gocover.io/_badge/github.com/go-resty/resty)](http://gocover.io/github.com/go-resty/resty) [![GoDoc](https://godoc.org/github.com/go-resty/resty?status.svg)](https://godoc.org/github.com/go-resty/resty) [![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
22

33
Simple HTTP and REST client for Go inspired by Ruby rest-client
44

5-
**Development is in progress.**
5+
**Documentation is in progress.**
66

77
### Usage
8-
<pre>Development is in progress</pre>
8+
<pre>Documentation is in progress</pre>
99

1010
### Versioning
1111
* resty release version according to [Semantic Versioning](http://semver.org)
1212

1313
### License
14-
resty released under MIT license, refer LICENSE file.
14+
resty released under MIT license, refer [LICENSE](LICENSE) file.

client.go

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,25 @@ import (
2828
)
2929

3030
const (
31-
GET = "GET"
32-
POST = "POST"
33-
PUT = "PUT"
34-
DELETE = "DELETE"
35-
PATCH = "PATCH"
36-
HEAD = "HEAD"
31+
// HTTP method GET
32+
GET = "GET"
33+
34+
// HTTP method POST
35+
POST = "POST"
36+
37+
// HTTP method PUT
38+
PUT = "PUT"
39+
40+
// HTTP method DELETE
41+
DELETE = "DELETE"
42+
43+
// HTTP method PATCH
44+
PATCH = "PATCH"
45+
46+
// HTTP method HEAD
47+
HEAD = "HEAD"
48+
49+
// HTTP method OPTIONS
3750
OPTIONS = "OPTIONS"
3851
)
3952

@@ -58,7 +71,7 @@ var (
5871
// Client type is used for HTTP/RESTful global values
5972
// for all request raised from the client
6073
type Client struct {
61-
HostUrl string
74+
HostURL string
6275
QueryParam url.Values
6376
FormData url.Values
6477
Header http.Header
@@ -77,15 +90,15 @@ type Client struct {
7790
afterResponse []func(*Client, *Response) error
7891
}
7992

80-
// Type User is hold a username and password information
93+
// User type is to hold an username and password information
8194
type User struct {
8295
Username, Password string
8396
}
8497

8598
// SetHostUrl method is to set Host URL in the client instance. It will be used with request
8699
// raised from this client with relative URL
87-
func (c *Client) SetHostUrl(url string) *Client {
88-
c.HostUrl = strings.TrimRight(url, "/")
100+
func (c *Client) SetHostURL(url string) *Client {
101+
c.HostURL = strings.TrimRight(url, "/")
89102
return c
90103
}
91104

@@ -133,7 +146,7 @@ func (c *Client) SetHeaders(headers map[string]string) *Client {
133146
// Domain: "sample.com",
134147
// MaxAge: 36000,
135148
// HttpOnly: true,
136-
// Secure: false, // baseds on https or http
149+
// Secure: false,
137150
// })
138151
//
139152
func (c *Client) SetCookie(hc *http.Cookie) *Client {
@@ -254,7 +267,7 @@ func (c *Client) SetAuthToken(token string) *Client {
254267
// R method creates a request instance, its used for Get, Post, Put, Delete, Patch, Head and Options.
255268
func (c *Client) R() *Request {
256269
r := &Request{
257-
Url: "",
270+
URL: "",
258271
Method: "",
259272
QueryParam: url.Values{},
260273
FormData: url.Values{},
@@ -501,7 +514,7 @@ func (c *Client) disableLogPrefix() {
501514
// and also you can add more options for that particular request
502515
//
503516
type Request struct {
504-
Url string
517+
URL string
505518
Method string
506519
QueryParam url.Values
507520
FormData url.Values
@@ -786,7 +799,7 @@ func (r *Request) execute(method, url string) (*Response, error) {
786799
}
787800

788801
r.Method = method
789-
r.Url = url
802+
r.URL = url
790803

791804
return r.client.execute(r)
792805
}
@@ -900,21 +913,21 @@ func DetectContentType(body interface{}) string {
900913
return contentType
901914
}
902915

903-
// IsJsonType method is to check JSON content type or not
904-
func IsJsonType(ct string) bool {
916+
// IsJSONType method is to check JSON content type or not
917+
func IsJSONType(ct string) bool {
905918
return jsonCheck.MatchString(ct)
906919
}
907920

908-
// IsXmlType method is to check XML content type or not
909-
func IsXmlType(ct string) bool {
921+
// IsXMLType method is to check XML content type or not
922+
func IsXMLType(ct string) bool {
910923
return xmlCheck.MatchString(ct)
911924
}
912925

913926
// Unmarshal content into object from JSON or XML
914927
func Unmarshal(ct string, b []byte, d interface{}) (err error) {
915-
if IsJsonType(ct) {
928+
if IsJSONType(ct) {
916929
err = json.Unmarshal(b, d)
917-
} else if IsXmlType(ct) {
930+
} else if IsXMLType(ct) {
918931
err = xml.Unmarshal(b, d)
919932
}
920933

@@ -964,17 +977,20 @@ func getRequestBodyString(r *Request) (body string) {
964977
var prtBodyBytes []byte
965978
var err error
966979
kind := reflect.ValueOf(r.Body).Kind()
967-
if IsJsonType(contentType) && (kind == reflect.Struct || kind == reflect.Map) {
980+
if IsJSONType(contentType) && (kind == reflect.Struct || kind == reflect.Map) {
968981
prtBodyBytes, err = json.MarshalIndent(&r.Body, "", " ")
969-
} else if IsXmlType(contentType) && (kind == reflect.Struct) {
982+
} else if IsXMLType(contentType) && (kind == reflect.Struct) {
970983
prtBodyBytes, err = xml.MarshalIndent(&r.Body, "", " ")
971984
} else if b, ok := r.Body.(string); ok {
972-
if IsJsonType(contentType) {
985+
if IsJSONType(contentType) {
973986
bodyBytes := []byte(b)
974987
var out bytes.Buffer
975988
if err = json.Indent(&out, bodyBytes, "", " "); err == nil {
976989
prtBodyBytes = out.Bytes()
977990
}
991+
} else {
992+
body = b
993+
return
978994
}
979995
} else if b, ok := r.Body.([]byte); ok {
980996
body = base64.StdEncoding.EncodeToString(b)
@@ -994,7 +1010,7 @@ func getResponseBodyString(res *Response) string {
9941010
bodyStr := "***** NO CONTENT *****"
9951011
if res.Body != nil {
9961012
ct := res.Header().Get(hdrContentTypeKey)
997-
if IsJsonType(ct) {
1013+
if IsJSONType(ct) {
9981014
var out bytes.Buffer
9991015
if err := json.Indent(&out, res.Body, "", " "); err == nil {
10001016
bodyStr = string(out.Bytes())

default.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func New() *Client {
2828
cookieJar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
2929

3030
c := &Client{
31-
HostUrl: "",
31+
HostURL: "",
3232
QueryParam: url.Values{},
3333
FormData: url.Values{},
3434
Header: http.Header{},
@@ -43,10 +43,10 @@ func New() *Client {
4343

4444
// default before request middlewares
4545
c.beforeRequest = []func(*Client, *Request) error{
46-
parseRequestUrl,
46+
parseRequestURL,
4747
parseRequestHeader,
4848
parseRequestBody,
49-
createHttpRequest,
49+
createHTTPRequest,
5050
addCredentials,
5151
requestLogger,
5252
}

middleware.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,37 +21,37 @@ import (
2121
// Request Middleware(s)
2222
//
2323

24-
func parseRequestUrl(c *Client, r *Request) error {
24+
func parseRequestURL(c *Client, r *Request) error {
2525
// Parsing request URL
26-
reqUrl, err := url.Parse(r.Url)
26+
reqURL, err := url.Parse(r.URL)
2727
if err != nil {
2828
return err
2929
}
3030

3131
// If Request.Url is relative path then added c.HostUrl into
3232
// the request URL otherwise Request.Url will be used as-is
33-
if !reqUrl.IsAbs() {
34-
if !strings.HasPrefix(r.Url, "/") {
35-
r.Url = "/" + r.Url
33+
if !reqURL.IsAbs() {
34+
if !strings.HasPrefix(r.URL, "/") {
35+
r.URL = "/" + r.URL
3636
}
3737

38-
reqUrl, err = url.Parse(c.HostUrl + r.Url)
38+
reqURL, err = url.Parse(c.HostURL + r.URL)
3939
if err != nil {
4040
return err
4141
}
4242
}
4343

4444
// Adding Query Param
45-
query := reqUrl.Query()
45+
query := reqURL.Query()
4646
for k := range c.QueryParam {
4747
query.Set(k, c.QueryParam.Get(k))
4848
}
4949
for k := range r.QueryParam {
5050
query.Set(k, r.QueryParam.Get(k))
5151
}
5252

53-
reqUrl.RawQuery = query.Encode()
54-
r.Url = reqUrl.String()
53+
reqURL.RawQuery = query.Encode()
54+
r.URL = reqURL.String()
5555

5656
return nil
5757
}
@@ -137,9 +137,9 @@ func parseRequestBody(c *Client, r *Request) (err error) {
137137

138138
var bodyBytes []byte
139139
kind := reflect.ValueOf(r.Body).Kind()
140-
if IsJsonType(contentType) && (kind == reflect.Struct || kind == reflect.Map) {
140+
if IsJSONType(contentType) && (kind == reflect.Struct || kind == reflect.Map) {
141141
bodyBytes, err = json.Marshal(&r.Body)
142-
} else if IsXmlType(contentType) && (kind == reflect.Struct) {
142+
} else if IsXMLType(contentType) && (kind == reflect.Struct) {
143143
bodyBytes, err = xml.Marshal(&r.Body)
144144
} else if b, ok := r.Body.(string); ok {
145145
bodyBytes = []byte(b)
@@ -149,13 +149,13 @@ func parseRequestBody(c *Client, r *Request) (err error) {
149149

150150
if err != nil {
151151
return
152+
} else if bodyBytes == nil {
153+
err = errors.New("Unsupported 'Body' type/value")
154+
return
152155
}
153156

154157
// []byte into Buffer
155-
if bodyBytes == nil {
156-
err = errors.New("Unsupported 'Body' type/value")
157-
return
158-
} else {
158+
if bodyBytes != nil {
159159
r.bodyBuf = bytes.NewBuffer(bodyBytes)
160160
}
161161
}
@@ -169,11 +169,11 @@ CL:
169169
return
170170
}
171171

172-
func createHttpRequest(c *Client, r *Request) (err error) {
172+
func createHTTPRequest(c *Client, r *Request) (err error) {
173173
if r.bodyBuf == nil {
174-
r.RawRequest, err = http.NewRequest(r.Method, r.Url, nil)
174+
r.RawRequest, err = http.NewRequest(r.Method, r.URL, nil)
175175
} else {
176-
r.RawRequest, err = http.NewRequest(r.Method, r.Url, r.bodyBuf)
176+
r.RawRequest, err = http.NewRequest(r.Method, r.URL, r.bodyBuf)
177177
}
178178

179179
// Add headers into http request
@@ -197,7 +197,7 @@ func addCredentials(c *Client, r *Request) error {
197197
r.RawRequest.SetBasicAuth(c.UserInfo.Username, c.UserInfo.Password)
198198
isBasicAuth = true
199199
}
200-
if isBasicAuth && !strings.HasPrefix(r.Url, "https") {
200+
if isBasicAuth && !strings.HasPrefix(r.URL, "https") {
201201
c.Log.Println("WARNING - Using Basic Auth in HTTP mode is not secure.")
202202
}
203203

@@ -257,7 +257,7 @@ func responseLogger(c *Client, res *Response) error {
257257
func parseResponseBody(c *Client, res *Response) (err error) {
258258
// Handles only JSON or XML content type
259259
ct := res.Header().Get(hdrContentTypeKey)
260-
if IsJsonType(ct) || IsXmlType(ct) {
260+
if IsJSONType(ct) || IsXMLType(ct) {
261261
// Considered as Result
262262
if res.StatusCode() > 199 && res.StatusCode() < 300 {
263263
if res.Request.Result != nil {

resty.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ Package resty provides simple HTTP and REST client for Go inspired by Ruby rest-
88
*/
99
package resty
1010

11+
// go-resty version no
1112
var Version = "0.1"

0 commit comments

Comments
 (0)