1// +build go1.7 go1.8 2 3// Copyright (c) 2015-2019 Jeevanandam M (jeeva@myjeeva.com) 4// 2016 Andrew Grigorev (https://github.com/ei-grad) 5// All rights reserved. 6// resty source code and usage is governed by a MIT style 7// license that can be found in the LICENSE file. 8 9package resty 10 11import ( 12 "bytes" 13 "context" 14 "encoding/json" 15 "net/http" 16 "net/url" 17 "time" 18) 19 20// Request type is used to compose and send individual request from client 21// go-resty is provide option override client level settings such as 22// Auth Token, Basic Auth credentials, Header, Query Param, Form Data, Error object 23// and also you can add more options for that particular request 24type Request struct { 25 URL string 26 Method string 27 Token string 28 QueryParam url.Values 29 FormData url.Values 30 Header http.Header 31 Time time.Time 32 Body interface{} 33 Result interface{} 34 Error interface{} 35 RawRequest *http.Request 36 SRV *SRVRecord 37 UserInfo *User 38 39 isMultiPart bool 40 isFormData bool 41 setContentLength bool 42 isSaveResponse bool 43 notParseResponse bool 44 jsonEscapeHTML bool 45 outputFile string 46 fallbackContentType string 47 ctx context.Context 48 pathParams map[string]string 49 client *Client 50 bodyBuf *bytes.Buffer 51 multipartFiles []*File 52 multipartFields []*MultipartField 53} 54 55// Context method returns the Context if its already set in request 56// otherwise it creates new one using `context.Background()`. 57func (r *Request) Context() context.Context { 58 if r.ctx == nil { 59 return context.Background() 60 } 61 return r.ctx 62} 63 64// SetContext method sets the context.Context for current Request. It allows 65// to interrupt the request execution if ctx.Done() channel is closed. 66// See https://blog.golang.org/context article and the "context" package 67// documentation. 68func (r *Request) SetContext(ctx context.Context) *Request { 69 r.ctx = ctx 70 return r 71} 72 73func (r *Request) addContextIfAvailable() { 74 if r.ctx != nil { 75 r.RawRequest = r.RawRequest.WithContext(r.ctx) 76 } 77} 78 79func (r *Request) isContextCancelledIfAvailable() bool { 80 if r.ctx != nil { 81 if r.ctx.Err() != nil { 82 return true 83 } 84 } 85 return false 86} 87 88// for go1.7+ 89var noescapeJSONMarshal = func(v interface{}) ([]byte, error) { 90 buf := acquireBuffer() 91 defer releaseBuffer(buf) 92 encoder := json.NewEncoder(buf) 93 encoder.SetEscapeHTML(false) 94 err := encoder.Encode(v) 95 return buf.Bytes(), err 96} 97