-
Notifications
You must be signed in to change notification settings - Fork 4
/
goaxios_struct.go
executable file
·106 lines (95 loc) · 3.24 KB
/
goaxios_struct.go
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
99
100
101
102
103
104
105
106
package goaxios
import (
"io"
"net/http"
"time"
)
type Interceptor struct {
Request func(req *GoAxios) *GoAxios
Response func(resp *http.Response) *http.Response
}
type GoAxios struct {
// Url is the url to send the request to.
Url string
// Method is the http method to use for the request. This can be GET, POST, PUT, PATCH, DELETE, etc.
Method string
// Body to pass to the request. This can be a string, []byte, or a struct
Body interface{}
// Form is only required when dealing with multi-part/formdata
Form *Form
/*Query represents the parameters to add to the url.
Example:
`/users?name=John&age=30` */
Query map[string]string
/*Params is the path parameters to replace in the url.
Example:
`/users/:name/:age` */
Params map[string]string
// BearerToken is the bearer token to use for the request. This will be added to the Authorization header in the form `Bearer <token>`
BearerToken string
// ResponseStruct is the struct to use for marshalling the response body. This is optional.
ResponseStruct interface{}
/*Headers is a map of headers to add to the request. A default
`Content-Type: application/json`*/
// is added if no headers are passed. To prevent this, pass an empty map.
Headers map[string]string
// Timeout is the timeout to use for the request. This is optional.
Timeout time.Duration
// IsMultiPart is a flag to indicate if the request is a multipart form.
IsMultiPart bool
// IsDownload is a flag to indicate if the request is a download request.
// if true, a download destination must be provided.
IsDownload bool
// DownloadDestination is the path/writer to write the download to.
// This is required if IsDownload is true.
DownloadDestination Destination
Interceptor Interceptor
// OnDownload is an handler that's fired for any batch of byte written during a download event
OnDownload func(Event)
}
type Destination struct {
Location string
Writer io.Writer
}
type Event struct {
Download DownloadEvent
}
type DownloadEvent struct {
TotalContentLength int64
CurrentContentLength int64
}
// Form is the struct used to pass parameters to request methods.
type Form struct {
// Files is a list of files to upload.
Files []FormFile
// Data is a list of data to upload along with the files.
Data []FormData
}
// FormData is the struct for uploading data along with files in a multipart request.
type FormData struct {
// Key is the key to use for the data.
Key string
// Value is the value to use for the data.
Value string
}
// FormFile is the struct for uploading a single file in a multipart request.
type FormFile struct {
// Name is the name of the file.
Name string
// Path is the path to the file. This has an xor relationship with Handle.
Path string
// Key is the key to use for the file.
Key string
// Handle is an io.ReadCloser to use instead of a file path. This has an xor relationship with Path. GoAxios will close this handle after reading it. If your handle does not implement io.Closer, then you can use io.NopCloser to wrap it.
Handle io.ReadCloser
}
type Response struct {
// The unfiltered *http.Respons object
Response *http.Response
// The response body as byte
Bytes []byte
// The unmarshalled response body
Body interface{}
// The error object (if any or nil)
Error error
}