File: requestdata.go

package info (click to toggle)
golang-github-koofr-go-httpclient 0.0~git20190818.e0dc8fd-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 160 kB
  • sloc: makefile: 2
file content (98 lines) | stat: -rw-r--r-- 1,739 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package httpclient

import (
	"context"
	"io"
	"net/http"
	"net/url"
)

type Encoding string

const (
	EncodingJSON = "JSON"
	EncodingXML  = "XML"
	EncodingForm = "Form"
)

type RequestData struct {
	Context          context.Context
	Method           string
	Path             string
	Params           url.Values
	FullURL          string // client.BaseURL + Path or FullURL
	Headers          http.Header
	ReqReader        io.Reader
	ReqEncoding      Encoding
	ReqValue         interface{}
	ReqContentLength int64
	ExpectedStatus   []int
	IgnoreRedirects  bool
	RespEncoding     Encoding
	RespValue        interface{}
	RespConsume      bool
}

func (r *RequestData) CanCopy() bool {
	if r.ReqReader != nil {
		return false
	}

	return true
}

func (r *RequestData) Copy() (ok bool, nr *RequestData) {
	if !r.CanCopy() {
		return false, nil
	}

	nr = &RequestData{
		Method:          r.Method,
		Path:            r.Path,
		FullURL:         r.FullURL,
		ReqEncoding:     r.ReqEncoding,
		ReqValue:        r.ReqValue,
		IgnoreRedirects: r.IgnoreRedirects,
		RespEncoding:    r.RespEncoding,
		RespValue:       r.RespValue,
		RespConsume:     r.RespConsume,
	}

	if r.Params != nil {
		nr.Params = make(url.Values)

		for k, vs := range r.Params {
			nvs := make([]string, len(vs))

			for i, v := range vs {
				nvs[i] = v
			}

			nr.Params[k] = nvs
		}
	}

	if r.Headers != nil {
		nr.Headers = make(http.Header)

		for k, vs := range r.Headers {
			nvs := make([]string, len(vs))

			for i, v := range vs {
				nvs[i] = v
			}

			nr.Headers[k] = nvs
		}
	}

	if r.ExpectedStatus != nil {
		nr.ExpectedStatus = make([]int, len(r.ExpectedStatus))

		for i, v := range r.ExpectedStatus {
			nr.ExpectedStatus[i] = v
		}
	}

	return true, nr
}