-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient_options.go
85 lines (64 loc) · 2.33 KB
/
client_options.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
package hop
import (
"context"
"io"
"strings"
)
// ClientOption is used to define am option that the client will consume when it is ran.
// You should not and cannot implement this interface. This is only designed as a signature for other options.
type ClientOption interface {
youCannotImplementClientOption()
}
type baseClientOption struct{}
func (baseClientOption) youCannotImplementClientOption() {}
type projectIdOption struct {
baseClientOption
projectId string
}
// WithProjectID Is used to return a ClientOption for calling the API with a specific project ID.
func WithProjectID(projectId string) ClientOption {
return projectIdOption{projectId: projectId}
}
type apiUrlOption struct {
baseClientOption
apiBase string
}
// WithCustomAPIURL Is used to return a ClientOption for calling the API with a specific API URL.
// By default, this is https://api.hop.io/v1.
func WithCustomAPIURL(apiBase string) ClientOption {
if !strings.HasPrefix(apiBase, "http://") && !strings.HasPrefix(apiBase, "https://") {
apiBase = "https://" + apiBase
}
apiBase = strings.TrimSuffix(apiBase, "/")
return apiUrlOption{apiBase: apiBase}
}
type curlWriterOption struct {
baseClientOption
w io.Writer
}
// WithCurlDebugWriter is used to write what the curl command for the request specified would be and a new line to an
// io.Writer.
func WithCurlDebugWriter(w io.Writer) ClientOption {
return curlWriterOption{w: w}
}
type customHandlerOption struct {
baseClientOption
fn CustomHandler
}
// CustomHandler is used to define a custom handler for Hop requests.
type CustomHandler func(ctx context.Context, a ClientArgs, opts ProcessedClientOpts) error
// WithCustomHandler is used to define a custom Hop request handler.
func WithCustomHandler(fn CustomHandler) ClientOption {
return customHandlerOption{fn: fn}
}
// ProcessedClientOpts is the result of all the client options that were passed in.
type ProcessedClientOpts struct {
// ProjectID is the project iD this is relating to. Blank if not set.
ProjectID string
// CustomAPIURL is the last API URL that was swt. Blank if not set.
CustomAPIURL string
// CurlDebugWriters are writers that the curl command and new line of a request should be sent to.
CurlDebugWriters []io.Writer
// CustomHandler is used to define the custom handler for Hop requests. Nil if not set.
CustomHandler CustomHandler
}