Skip to content

Commit

Permalink
up: update some std methods, update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jan 23, 2022
1 parent 52d845e commit b50ef61
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 37 deletions.
33 changes: 32 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

**HReq** A simple http client request builder and sender

> `hreq` inspired from https://github.com/dghubble/sling and more projects, please see refers.
> `hreq` inspired from [dghubble/sling][1] and more projects, please see refers.
## Features

Expand Down Expand Up @@ -75,6 +75,36 @@ map[string]interface {} { #len=4
},
```

## Request headers

```go
hreq.New("some.host/api").
SetHeader("req-id", "a string")
```

Set multi at once:

```go
hreq.New("some.host/api").
SetHeaders(map[string]string{
"req-id": "a string",
})
```

### Set content type

```go
hreq.New("some.host/api").
ContentType("text/html")
```

Built in `JSONType()` `FromType()` `XMLType()`

```go
hreq.New("some.host/api").JSONType()
```


## Use middleware

```go
Expand Down Expand Up @@ -216,3 +246,4 @@ Server: gunicorn/19.9.0
- https://github.com/go-resty/resty
- https://github.com/monaco-io/request

[1]: https://github.com/dghubble/sling
9 changes: 9 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# TODO

- allow upload file
- [ ] can send benchmark requests
- [ ] request to CURL command
- [ ] allow sending raw text http contents
- batch send requests, wait one ok OR wait all ok
- retry send request
- backup url on request error or custom condition
1 change: 1 addition & 0 deletions ext.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package hreq
20 changes: 19 additions & 1 deletion hreq.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,11 @@ func (h *HReq) ContentType(value string) *HReq {
return h.SetHeader(httpctype.Key, value)
}

// XMLType with xml Content-Type header
func (h *HReq) XMLType() *HReq {
return h.SetHeader(httpctype.Key, httpctype.XML)
}

// JSONType with json Content-Type header
func (h *HReq) JSONType() *HReq {
return h.SetHeader(httpctype.Key, httpctype.JSON)
Expand Down Expand Up @@ -388,12 +393,25 @@ func (h *HReq) BasicAuth(username, password string) *HReq {
return h.SetHeader("Authorization", httpreq.BuildBasicAuth(username, password))
}

// SetCookies to request
func (h *HReq) SetCookies(hcs ...*http.Cookie) *HReq {
var b strings.Builder
for i, hc := range hcs {
if i > 0 {
b.WriteByte(';')
}
b.WriteString(hc.String())
}

return h.SetCookieString(b.String())
}

// SetCookieString set cookie header value.
//
// Usage:
// h.New().
// SetCookieString("name=inhere;age=30").
// Get("/some/api")
// GetDo("/some/api")
func (h *HReq) SetCookieString(value string) *HReq {
// return h.AddHeader("Set-Cookie", value)
return h.AddHeader("Cookie", value)
Expand Down
35 changes: 17 additions & 18 deletions std.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ func BaseURL(baseURL string) *HReq {
return std.BaseURL(baseURL)
}

// Get sets the method to GET and sets the given pathURL, then send request and return response.
func Get(pathURL string) (*Response, error) {
// GetDo sets the method to GET and sets the given pathURL, then send request and return response.
func GetDo(pathURL string) (*Response, error) {
return reset().Send(pathURL, http.MethodGet)
}

// Post sets the method to POST and sets the given pathURL,
// PostDo sets the method to POST and sets the given pathURL,
// then send request and return http response.
func Post(pathURL string, body ...interface{}) (*Response, error) {
func PostDo(pathURL string, body ...interface{}) (*Response, error) {
if len(body) > 0 {
std.Body(body[0])
} else {
Expand All @@ -49,9 +49,9 @@ func Post(pathURL string, body ...interface{}) (*Response, error) {
return std.Send(pathURL, http.MethodPost)
}

// Put sets the method to PUT and sets the given pathURL,
// PutDo sets the method to PUT and sets the given pathURL,
// then send request and return http response.
func Put(pathURL string, body ...interface{}) (*Response, error) {
func PutDo(pathURL string, body ...interface{}) (*Response, error) {
if len(body) > 0 {
std.Body(body[0])
} else {
Expand All @@ -61,9 +61,9 @@ func Put(pathURL string, body ...interface{}) (*Response, error) {
return std.Send(pathURL, http.MethodPut)
}

// Patch sets the method to PATCH and sets the given pathURL,
// PatchDo sets the method to PATCH and sets the given pathURL,
// then send request and return http response.
func Patch(pathURL string, body ...interface{}) (*Response, error) {
func PatchDo(pathURL string, body ...interface{}) (*Response, error) {
if len(body) > 0 {
std.Body(body[0])
} else {
Expand All @@ -73,32 +73,31 @@ func Patch(pathURL string, body ...interface{}) (*Response, error) {
return std.Send(pathURL, http.MethodPatch)
}

// Delete sets the method to DELETE and sets the given pathURL,
// DeleteDo sets the method to DELETE and sets the given pathURL,
// then send request and return http response.
func Delete(pathURL string) (*Response, error) {
reset()
return std.Send(pathURL, http.MethodDelete)
func DeleteDo(pathURL string) (*Response, error) {
return reset().Send(pathURL, http.MethodDelete)
}

// Head sets the method to HEAD and request the pathURL, then send request and return response.
func Head(pathURL string) (*Response, error) {
return reset().Send(pathURL, http.MethodHead)
}

// Trace sets the method to TRACE and sets the given pathURL,
// TraceDo sets the method to TRACE and sets the given pathURL,
// then send request and return http response.
func Trace(pathURL string) (*Response, error) {
func TraceDo(pathURL string) (*Response, error) {
return reset().Send(pathURL, http.MethodTrace)
}

// Options sets the method to OPTIONS and request the pathURL,
// OptionsDo sets the method to OPTIONS and request the pathURL,
// then send request and return response.
func Options(pathURL string) (*Response, error) {
func OptionsDo(pathURL string) (*Response, error) {
return reset().Send(pathURL, http.MethodOptions)
}

// Connect sets the method to CONNECT and sets the given pathURL,
// ConnectDo sets the method to CONNECT and sets the given pathURL,
// then send request and return http response.
func Connect(pathURL string) (*Response, error) {
func ConnectDo(pathURL string) (*Response, error) {
return reset().Send(pathURL, http.MethodConnect)
}
34 changes: 17 additions & 17 deletions std_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ func init() {
hreq.BaseURL(testBaseURL)
}

func TestGet(t *testing.T) {
resp, err := hreq.Get("/get")
func TestGetDo(t *testing.T) {
resp, err := hreq.GetDo("/get")

assert.NoError(t, err)
assert.True(t, resp.IsOK())
Expand Down Expand Up @@ -45,8 +45,8 @@ func TestGetDo_with_QueryParams(t *testing.T) {
dump.P(retMp)
}

func TestPost(t *testing.T) {
resp, err := hreq.Post("/post")
func TestPostDo(t *testing.T) {
resp, err := hreq.PostDo("/post")

assert.NoError(t, err)
assert.True(t, resp.IsOK())
Expand All @@ -58,8 +58,8 @@ func TestPost(t *testing.T) {
dump.P(retMp)
}

func TestPut(t *testing.T) {
resp, err := hreq.Put("/put")
func TestPutDo(t *testing.T) {
resp, err := hreq.PutDo("/put")

assert.NoError(t, err)
assert.True(t, resp.IsOK())
Expand All @@ -71,8 +71,8 @@ func TestPut(t *testing.T) {
dump.P(retMp)
}

func TestPatch(t *testing.T) {
resp, err := hreq.Patch("/patch")
func TestPatchDo(t *testing.T) {
resp, err := hreq.PatchDo("/patch")

assert.NoError(t, err)
assert.True(t, resp.IsOK())
Expand All @@ -84,8 +84,8 @@ func TestPatch(t *testing.T) {
dump.P(retMp)
}

func TestDelete(t *testing.T) {
resp, err := hreq.Delete("/delete")
func TestDeleteDo(t *testing.T) {
resp, err := hreq.DeleteDo("/delete")

assert.NoError(t, err)
assert.True(t, resp.IsOK())
Expand All @@ -97,7 +97,7 @@ func TestDelete(t *testing.T) {
dump.P(retMp)
}

func TestHead(t *testing.T) {
func TestHeadDo(t *testing.T) {
resp, err := hreq.Reset().HeadDo("/")
fmt.Println(resp.String())

Expand All @@ -109,8 +109,8 @@ func TestHead(t *testing.T) {
assert.Empty(t, resp.BodyString())
}

func TestOptions(t *testing.T) {
resp, err := hreq.Options("/")
func TestOptionsDo(t *testing.T) {
resp, err := hreq.OptionsDo("/")
fmt.Println(resp.String())

assert.NoError(t, err)
Expand All @@ -122,16 +122,16 @@ func TestOptions(t *testing.T) {
assert.NotEmpty(t, resp.HeaderString())
}

func TestTrace(t *testing.T) {
resp, err := hreq.Trace("/")
func TestTraceDo(t *testing.T) {
resp, err := hreq.TraceDo("/")
fmt.Println(resp.String())

assert.NoError(t, err)
// assert.True(t, resp.IsNoBody())
}

func TestConnect(t *testing.T) {
resp, err := hreq.Connect("/")
func TestConnectDo(t *testing.T) {
resp, err := hreq.ConnectDo("/")
fmt.Println(resp.String())

assert.NoError(t, err)
Expand Down

0 comments on commit b50ef61

Please sign in to comment.