Skip to content

Commit

Permalink
Merge pull request imroc#50 from kazekim/master
Browse files Browse the repository at this point in the history
use HeaderFromStruct function to create header from struct to make we work easier
  • Loading branch information
imroc authored Jan 26, 2020
2 parents 6799276 + 472d7da commit 46bda37
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 14 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,25 @@ header.Set("Accept", "application/json")
req.Get("https://www.baidu.com", header)
```

#### Set Header From Struct
Use `HeaderFromStruct` func to parse your struct
``` go
type HeaderStruct struct {
UserAgent string `json:"User-Agent"`
Authorization string `json:"Authorization"`
}

h := HeaderStruct{
"V1.0.0",
"roc",
}

authHeader := req.HeaderFromStruct(h)
req.Get("https://www.baidu.com", authHeader, req.Header{"User-Agent": "V1.1"})
```

Note: Please add tag 'json' to your argument in struct to let you customize the key name of your header

## <a name="Set-Param">Set Param</a>
Use `req.Param` (it is actually a `map[string]interface{}`)
``` go
Expand Down
41 changes: 41 additions & 0 deletions header.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
GoLang code created by Jirawat Harnsiriwatanakit https://github.com/kazekim
*/

package req

import "encoding/json"

// Header represents http request header
type Header map[string]string

func (h Header) Clone() Header {
if h == nil {
return nil
}
hh := Header{}
for k, v := range h {
hh[k] = v
}
return hh
}

// ParseStruct parse struct into header
func ParseStruct(h Header, v interface{}) Header {
data, err := json.Marshal(v)
if err != nil {
return h
}

err = json.Unmarshal(data, &h)
return h
}

// HeaderFromStruct init header from struct
func HeaderFromStruct(v interface{}) Header {

var header Header
header = ParseStruct(header, v)
return header
}

47 changes: 47 additions & 0 deletions header_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
GoLang code created by Jirawat Harnsiriwatanakit https://github.com/kazekim
*/

package req

import "testing"

func TestParseStruct(t *testing.T) {

type HeaderStruct struct {
UserAgent string `json:"User-Agent"`
Authorization string `json:"Authorization"`
}

h := HeaderStruct{
"V1.0.0",
"roc",
}

var header Header
header = ParseStruct(header, h)

if header["User-Agent"] != h.UserAgent && header["Authorization"] != h.Authorization {
t.Fatal("struct parser for header is not working")
}

}

func TestHeaderFromStruct(t *testing.T) {

type HeaderStruct struct {
UserAgent string `json:"User-Agent"`
Authorization string `json:"Authorization"`
}

h := HeaderStruct{
"V1.0.0",
"roc",
}

header := HeaderFromStruct(h)

if header["User-Agent"] != h.UserAgent && header["Authorization"] != h.Authorization {
t.Fatal("struct parser for header is not working")
}
}
14 changes: 0 additions & 14 deletions req.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,6 @@ const (
LstdFlags = LreqHead | LreqBody | LrespHead | LrespBody
)

// Header represents http request header
type Header map[string]string

func (h Header) Clone() Header {
if h == nil {
return nil
}
hh := Header{}
for k, v := range h {
hh[k] = v
}
return hh
}

// Param represents http request param
type Param map[string]interface{}

Expand Down

0 comments on commit 46bda37

Please sign in to comment.