Skip to content

Commit f8e1383

Browse files
committed
code improvements, preparing for v1.0
1 parent 41c7684 commit f8e1383

File tree

4 files changed

+178
-170
lines changed

4 files changed

+178
-170
lines changed

client.go

Lines changed: 0 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,15 @@ import (
88
"bytes"
99
"crypto/tls"
1010
"crypto/x509"
11-
"encoding/json"
12-
"encoding/xml"
1311
"errors"
1412
"fmt"
1513
"io"
1614
"io/ioutil"
1715
"log"
18-
"mime/multipart"
1916
"net/http"
2017
"net/url"
21-
"os"
22-
"path/filepath"
2318
"reflect"
2419
"regexp"
25-
"runtime"
2620
"strings"
2721
"sync"
2822
"time"
@@ -649,13 +643,7 @@ func (c *Client) SetRootCertificate(pemFilePath string) *Client {
649643
// resty.SetOutputDirectory("/save/http/response/here")
650644
//
651645
func (c *Client) SetOutputDirectory(dirPath string) *Client {
652-
err := createDirectory(dirPath)
653-
if err != nil {
654-
c.Log.Printf("ERROR [%v]", err)
655-
}
656-
657646
c.outputDirectory = dirPath
658-
659647
return c
660648
}
661649

@@ -834,155 +822,3 @@ type File struct {
834822
func (f *File) String() string {
835823
return fmt.Sprintf("ParamName: %v; FileName: %v", f.ParamName, f.Name)
836824
}
837-
838-
//
839-
// Helper methods
840-
//
841-
842-
// IsStringEmpty method tells whether given string is empty or not
843-
func IsStringEmpty(str string) bool {
844-
return (len(strings.TrimSpace(str)) == 0)
845-
}
846-
847-
// DetectContentType method is used to figure out `Request.Body` content type for request header
848-
func DetectContentType(body interface{}) string {
849-
contentType := plainTextType
850-
kind := kindOf(body)
851-
switch kind {
852-
case reflect.Struct, reflect.Map:
853-
contentType = jsonContentType
854-
case reflect.String:
855-
contentType = plainTextType
856-
default:
857-
if b, ok := body.([]byte); ok {
858-
contentType = http.DetectContentType(b)
859-
} else if kind == reflect.Slice {
860-
contentType = jsonContentType
861-
}
862-
}
863-
864-
return contentType
865-
}
866-
867-
// IsJSONType method is to check JSON content type or not
868-
func IsJSONType(ct string) bool {
869-
return jsonCheck.MatchString(ct)
870-
}
871-
872-
// IsXMLType method is to check XML content type or not
873-
func IsXMLType(ct string) bool {
874-
return xmlCheck.MatchString(ct)
875-
}
876-
877-
// Unmarshal content into object from JSON or XML
878-
// Deprecated: kept for backward compatibility
879-
func Unmarshal(ct string, b []byte, d interface{}) (err error) {
880-
if IsJSONType(ct) {
881-
err = json.Unmarshal(b, d)
882-
} else if IsXMLType(ct) {
883-
err = xml.Unmarshal(b, d)
884-
}
885-
886-
return
887-
}
888-
889-
// Unmarshalc content into object from JSON or XML
890-
func Unmarshalc(c *Client, ct string, b []byte, d interface{}) (err error) {
891-
if IsJSONType(ct) {
892-
err = c.JSONUnmarshal(b, d)
893-
} else if IsXMLType(ct) {
894-
err = xml.Unmarshal(b, d)
895-
}
896-
897-
return
898-
}
899-
900-
func getLogger(w io.Writer) *log.Logger {
901-
return log.New(w, "RESTY ", log.LstdFlags)
902-
}
903-
904-
func addFile(w *multipart.Writer, fieldName, path string) error {
905-
file, err := os.Open(path)
906-
if err != nil {
907-
return err
908-
}
909-
defer func() {
910-
_ = file.Close()
911-
}()
912-
913-
part, err := w.CreateFormFile(fieldName, filepath.Base(path))
914-
if err != nil {
915-
return err
916-
}
917-
_, err = io.Copy(part, file)
918-
919-
return err
920-
}
921-
922-
func addFileReader(w *multipart.Writer, f *File) error {
923-
part, err := w.CreateFormFile(f.ParamName, f.Name)
924-
if err != nil {
925-
return err
926-
}
927-
_, err = io.Copy(part, f.Reader)
928-
929-
return err
930-
}
931-
932-
func getPointer(v interface{}) interface{} {
933-
vv := valueOf(v)
934-
if vv.Kind() == reflect.Ptr {
935-
return v
936-
}
937-
return reflect.New(vv.Type()).Interface()
938-
}
939-
940-
func isPayloadSupported(m string, allowMethodGet bool) bool {
941-
return (m == MethodPost || m == MethodPut || m == MethodDelete || m == MethodPatch || (allowMethodGet && m == MethodGet))
942-
}
943-
944-
func typeOf(i interface{}) reflect.Type {
945-
return indirect(valueOf(i)).Type()
946-
}
947-
948-
func valueOf(i interface{}) reflect.Value {
949-
return reflect.ValueOf(i)
950-
}
951-
952-
func indirect(v reflect.Value) reflect.Value {
953-
return reflect.Indirect(v)
954-
}
955-
956-
func kindOf(v interface{}) reflect.Kind {
957-
return typeOf(v).Kind()
958-
}
959-
960-
func createDirectory(dir string) (err error) {
961-
if _, err = os.Stat(dir); err != nil {
962-
if os.IsNotExist(err) {
963-
if err = os.MkdirAll(dir, 0755); err != nil {
964-
return
965-
}
966-
}
967-
}
968-
return
969-
}
970-
971-
func canJSONMarshal(contentType string, kind reflect.Kind) bool {
972-
return IsJSONType(contentType) && (kind == reflect.Struct || kind == reflect.Map)
973-
}
974-
975-
func functionName(i interface{}) string {
976-
return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
977-
}
978-
979-
func acquireBuffer() *bytes.Buffer {
980-
return bufPool.Get().(*bytes.Buffer)
981-
}
982-
983-
func releaseBuffer(buf *bytes.Buffer) {
984-
if buf != nil {
985-
buf.Reset()
986-
bufPool.Put(buf)
987-
}
988-
}

middleware.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ import (
1717
"path/filepath"
1818
"reflect"
1919
"strings"
20+
"time"
2021
)
2122

22-
//
23+
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
2324
// Request Middleware(s)
24-
//
25+
//___________________________________
2526

2627
func parseRequestURL(c *Client, r *Request) error {
2728
// Parsing request URL
@@ -70,10 +71,10 @@ func parseRequestURL(c *Client, r *Request) error {
7071
func parseRequestHeader(c *Client, r *Request) error {
7172
hdr := http.Header{}
7273
for k := range c.Header {
73-
hdr.Set(k, c.Header.Get(k))
74+
hdr[k] = append(hdr[k], c.Header[k]...)
7475
}
7576
for k := range r.Header {
76-
hdr.Set(k, r.Header.Get(k))
77+
hdr[k] = append(hdr[k], r.Header[k]...)
7778
}
7879

7980
if IsStringEmpty(hdr.Get(hdrUserAgentKey)) {
@@ -217,7 +218,7 @@ func responseLogger(c *Client, res *Response) error {
217218
c.disableLogPrefix()
218219
c.Log.Println("---------------------- RESPONSE LOG -----------------------")
219220
c.Log.Printf("STATUS : %s", res.Status())
220-
c.Log.Printf("RECEIVED AT : %v", res.ReceivedAt())
221+
c.Log.Printf("RECEIVED AT : %v", res.ReceivedAt().Format(time.RFC3339Nano))
221222
c.Log.Printf("RESPONSE TIME : %v", res.Time())
222223
c.Log.Println("HEADERS:")
223224
for h, v := range res.Header() {

request.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ func (r *Request) Execute(method, url string) (*Response, error) {
456456
}
457457

458458
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
459-
// Unexported methods
459+
// Request Unexported methods
460460
//___________________________________
461461

462462
func (r *Request) fmtBodyString() (body string) {

0 commit comments

Comments
 (0)