@@ -7,13 +7,13 @@ import (
77 "io"
88 "math/rand/v2"
99 "net/http"
10- "os"
1110 "time"
1211)
1312
1413const (
15- maxRetries = 2
16- baseDelay = 500 * time .Millisecond
14+ DefaultBaseURL = "https://app.loops.so/api/v1"
15+ maxRetries = 2
16+ baseDelay = 500 * time .Millisecond
1717)
1818
1919var sleep = time .Sleep
@@ -35,22 +35,27 @@ type Client struct {
3535 baseURL string
3636 apiKey string
3737 httpClient * http.Client
38- debug bool
38+ logger io. Writer
3939 userAgent string
4040}
4141
42- func NewClient (baseURL , apiKey string , debug bool ) * Client {
43- return & Client {
44- baseURL : baseURL ,
42+ type Option func (* Client )
43+
44+ func WithBaseURL (u string ) Option { return func (c * Client ) { c .baseURL = u } }
45+ func WithUserAgent (ua string ) Option { return func (c * Client ) { c .userAgent = ua } }
46+ func WithLogger (w io.Writer ) Option { return func (c * Client ) { c .logger = w } }
47+ func WithHTTPClient (h * http.Client ) Option { return func (c * Client ) { c .httpClient = h } }
48+
49+ func NewClient (apiKey string , opts ... Option ) * Client {
50+ c := & Client {
51+ baseURL : DefaultBaseURL ,
4552 apiKey : apiKey ,
4653 httpClient : & http.Client {Timeout : 5 * time .Second },
47- debug : debug ,
4854 userAgent : "loops-go/dev" ,
4955 }
50- }
51-
52- func (c * Client ) WithUserAgent (ua string ) * Client {
53- c .userAgent = ua
56+ for _ , opt := range opts {
57+ opt (c )
58+ }
5459 return c
5560}
5661
@@ -73,20 +78,20 @@ func errorFromResponse(resp *http.Response) *APIError {
7378func (c * Client ) logResponse (resp * http.Response ) {
7479 raw , err := io .ReadAll (resp .Body )
7580 if err != nil {
76- fmt .Fprintf (os . Stderr , "[debug] Response: %s (body read failed: %v)\n " , resp .Status , err )
81+ fmt .Fprintf (c . logger , "[debug] Response: %s (body read failed: %v)\n " , resp .Status , err )
7782 resp .Body = io .NopCloser (bytes .NewReader (nil ))
7883 return
7984 }
8085 resp .Body = io .NopCloser (bytes .NewReader (raw ))
81- fmt .Fprintf (os . Stderr , "[debug] Response: %s (%d bytes)\n " , resp .Status , len (raw ))
86+ fmt .Fprintf (c . logger , "[debug] Response: %s (%d bytes)\n " , resp .Status , len (raw ))
8287 if len (raw ) == 0 {
8388 return
8489 }
8590 var pretty bytes.Buffer
8691 if json .Indent (& pretty , raw , "" , " " ) == nil {
87- fmt .Fprintf (os . Stderr , "[debug] Body:\n %s\n " , pretty .String ())
92+ fmt .Fprintf (c . logger , "[debug] Body:\n %s\n " , pretty .String ())
8893 } else {
89- fmt .Fprintf (os . Stderr , "[debug] Body: %s\n " , raw )
94+ fmt .Fprintf (c . logger , "[debug] Body: %s\n " , raw )
9095 }
9196}
9297
@@ -116,7 +121,7 @@ func (c *Client) do(req *http.Request) (*http.Response, error) {
116121 continue
117122 }
118123 if ! isRetryable (resp .StatusCode ) {
119- if c .debug {
124+ if c .logger != nil {
120125 c .logResponse (resp )
121126 }
122127 return resp , nil
@@ -135,7 +140,7 @@ func (c *Client) newRequest(method, path string, body io.Reader) (*http.Request,
135140 url := fmt .Sprintf ("%s%s" , c .baseURL , path )
136141
137142 var bodyBytes []byte
138- if body != nil && c .debug {
143+ if body != nil && c .logger != nil {
139144 var err error
140145 bodyBytes , err = io .ReadAll (body )
141146 if err != nil {
@@ -154,18 +159,18 @@ func (c *Client) newRequest(method, path string, body io.Reader) (*http.Request,
154159 req .Header .Set ("Content-Type" , "application/json" )
155160 }
156161
157- if c .debug {
158- fmt .Fprintf (os . Stderr , "[debug] %s %s\n " , method , url )
159- fmt .Fprintf (os . Stderr , "[debug] Authorization: Bearer [REDACTED]\n " )
162+ if c .logger != nil {
163+ fmt .Fprintf (c . logger , "[debug] %s %s\n " , method , url )
164+ fmt .Fprintf (c . logger , "[debug] Authorization: Bearer [REDACTED]\n " )
160165 if req .Header .Get ("Content-Type" ) != "" {
161- fmt .Fprintf (os . Stderr , "[debug] Content-Type: %s\n " , req .Header .Get ("Content-Type" ))
166+ fmt .Fprintf (c . logger , "[debug] Content-Type: %s\n " , req .Header .Get ("Content-Type" ))
162167 }
163168 if len (bodyBytes ) > 0 {
164169 var pretty bytes.Buffer
165170 if json .Indent (& pretty , bodyBytes , "" , " " ) == nil {
166- fmt .Fprintf (os . Stderr , "[debug] Body:\n %s\n " , pretty .String ())
171+ fmt .Fprintf (c . logger , "[debug] Body:\n %s\n " , pretty .String ())
167172 } else {
168- fmt .Fprintf (os . Stderr , "[debug] Body: %s\n " , bodyBytes )
173+ fmt .Fprintf (c . logger , "[debug] Body: %s\n " , bodyBytes )
169174 }
170175 }
171176 }
0 commit comments