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:
2022-12-11 21:37:35 +00:00
parent 013e896076
commit e6b9af36de
9 changed files with 282 additions and 247 deletions

70
options.go Normal file
View 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
})
}