Skip to content

Commit

Permalink
api调整
Browse files Browse the repository at this point in the history
  • Loading branch information
lxzan committed Jul 15, 2019
1 parent 9dbe901 commit ef32544
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 32 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ hasaki.
Set(hasaki.Form{
"X-Access-Token": token,
"X-Running-Env": env,
})
}).
Send(nil)

// Advanced
opt := &RequestOption{
Expand Down
50 changes: 25 additions & 25 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,28 +93,28 @@ func Delete(link string, opt *RequestOption) *Request {
return NewRequest("delete", link, opt)
}

func (this *Request) Type(contentType string) *Request {
this.contentType = contentType
return this
func (c *Request) Type(contentType string) *Request {
c.contentType = contentType
return c
}

func (this *Request) Set(header Form) *Request {
this.header = header
return this
func (c *Request) Set(header Form) *Request {
c.header = header
return c
}

func (this *Request) Send(param JSON) (*Response, error) {
if this.err != nil {
return nil, this.err
func (c *Request) Send(param Any) (*Response, error) {
if c.err != nil {
return nil, c.err
}

if param == nil {
param = JSON{}
param = Any{}
}

var r io.Reader
if this.method == "GET" {
var query = this.url.Query()
if c.method == "GET" {
var query = c.url.Query()
var qs = ""
if len(query) > 0 || len(param) > 0 {
for k, item := range query {
Expand All @@ -126,34 +126,34 @@ func (this *Request) Send(param JSON) (*Response, error) {
}
qs = "?" + FormEncode(param)
}
this.link = fmt.Sprintf("%s://%s%s%s", this.url.Scheme, this.url.Host, this.url.Path, qs)
c.link = fmt.Sprintf("%s://%s%s%s", c.url.Scheme, c.url.Host, c.url.Path, qs)
} else {
if this.contentType == FormType {
if c.contentType == FormType {
r = strings.NewReader(FormEncode(param))
} else if this.contentType == JsonType {
} else if c.contentType == JsonType {
b, _ := json.Marshal(param)
r = bytes.NewReader(b)
}
}
return this.Raw(r)
return c.Raw(r)
}

func (this *Request) Raw(r io.Reader) (*Response, error) {
if this.err != nil {
return nil, this.err
func (c *Request) Raw(r io.Reader) (*Response, error) {
if c.err != nil {
return nil, c.err
}

var req, err = http.NewRequest(this.method, this.link, r)
var req, err = http.NewRequest(c.method, c.link, r)
if err != nil {
return nil, err
}

req.Header.Set("Content-Type", this.contentType)
for k, v := range this.header {
req.Header.Set("Content-Type", c.contentType)
for k, v := range c.header {
req.Header.Set(k, v)
}

var res, requestError = this.client.Do(req)
var res, requestError = c.client.Do(req)
if requestError != nil {
return nil, requestError
}
Expand All @@ -163,7 +163,7 @@ func (this *Request) Raw(r io.Reader) (*Response, error) {
return nil, readError
}
return &Response{
HttpResponse: res,
Body: body,
Response: res,
responseBody: body,
}, nil
}
2 changes: 1 addition & 1 deletion request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import (

func TestGet(t *testing.T) {
res, _ := Post("https://api.github.com/", nil).Send(nil)
println(string(res.Body))
println(string(res.GetBody()))
}
12 changes: 9 additions & 3 deletions response.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package hasaki

import "net/http"
import (
"net/http"
)

type Response struct {
HttpResponse *http.Response
Body []byte
*http.Response
responseBody []byte
}

func (c *Response) GetBody() []byte {
return c.responseBody
}
4 changes: 2 additions & 2 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ const (
FormType = "application/x-www-form-urlencoded"
)

type JSON map[string]interface{}
type Any map[string]interface{}

type Form map[string]string

func FormEncode(data JSON) string {
func FormEncode(data Any) string {
var form = url.Values{}
for k, item := range data {
if v, ok := item.(string); ok {
Expand Down

0 comments on commit ef32544

Please sign in to comment.