mirror of
https://github.com/jimeh/go-midjourney.git
synced 2026-02-19 01:46:41 +00:00
feat(client)!: simplify Client by extracting API methods to APIClient
BREAKING CHANGE: All API request related moved from Client to APIClient type.
This commit is contained in:
70
options.go
Normal file
70
options.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package midjourney
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
type Option interface {
|
||||
apply(*APIClient) error
|
||||
}
|
||||
|
||||
type optionFunc func(*APIClient) error
|
||||
|
||||
func (fn optionFunc) apply(o *APIClient) error {
|
||||
return fn(o)
|
||||
}
|
||||
|
||||
// WithAuthToken returns a new Option type which sets the auth token that the
|
||||
// client will use. The authToken value can be fetched from the
|
||||
// "__Secure-next-auth.session-token" cookie on the midjourney.com website.
|
||||
func WithAuthToken(authToken string) Option {
|
||||
return optionFunc(func(c *APIClient) error {
|
||||
c.AuthToken = authToken
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func WithAPIURL(baseURL string) Option {
|
||||
return optionFunc(func(c *APIClient) error {
|
||||
if !strings.HasSuffix(baseURL, "/") {
|
||||
baseURL += "/"
|
||||
}
|
||||
|
||||
u, err := url.Parse(baseURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.APIURL = u
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func WithHTTPClient(httpClient HTTPClient) Option {
|
||||
return optionFunc(func(c *APIClient) error {
|
||||
c.HTTPClient = httpClient
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func WithUserAgent(userAgent string) Option {
|
||||
return optionFunc(func(c *APIClient) error {
|
||||
c.UserAgent = userAgent
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func WithLogger(logger zerolog.Logger) Option {
|
||||
return optionFunc(func(c *APIClient) error {
|
||||
c.Logger = logger
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user