Browse Source

Add NewClientWith constructor

This adds a new constructor to build Clients which operates on a Config
struct rather than positional parameters. This will enable adding new
parameters in the future without breaking the API.

To begin with, this introduces a new URL parameter that wasn't
previously available. An example of another parameter that could be
added in the future is the timeout (to override the default timeout).

Existing constructors have been transitioned to the new one because it
implements a superset of their functionality.

Minor note: The Tor constructor wasn't respecting the default timeout
but with this version, it does.

Resolves #8
pull/9/head
Abhinav Gupta 5 years ago
parent
commit
2ad70a7602
1 changed files with 36 additions and 12 deletions
  1. +36
    -12
      writeas.go

+ 36
- 12
writeas.go View File

@@ -44,31 +44,55 @@ const defaultHTTPTimeout = 10 * time.Second
// c := writeas.NewClient()
// c.SetToken("00000000-0000-0000-0000-000000000000")
func NewClient() *Client {
return &Client{
client: &http.Client{Timeout: defaultHTTPTimeout},
baseURL: apiURL,
}
return NewClientWith(Config{URL: apiURL})
}

// NewTorClient creates a new API client for communicating with the Write.as
// Tor hidden service, using the given port to connect to the local SOCKS
// proxy.
func NewTorClient(port int) *Client {
dialSocksProxy := socks.DialSocksProxy(socks.SOCKS5, fmt.Sprintf("127.0.0.1:%d", port))
transport := &http.Transport{Dial: dialSocksProxy}
return &Client{
client: &http.Client{Transport: transport},
baseURL: torAPIURL,
}
return NewClientWith(Config{URL: torAPIURL, TorPort: port})
}

// NewDevClient creates a new API client for development and testing. It'll
// communicate with our development servers, and SHOULD NOT be used in
// production.
func NewDevClient() *Client {
return NewClientWith(Config{URL: devAPIURL})
}

// Config configures a Write.as client.
type Config struct {
// URL of the Write.as API service. Defaults to https://write.as/api.
URL string

// If specified, the API client will communicate with the Write.as Tor
// hidden service using the provided port to connect to the local SOCKS
// proxy.
TorPort int

// If specified, requests will be authenticated using this user token.
// This may be provided after making a few anonymous requests with
// SetToken.
Token string
}

// NewClientWith builds a new API client with the provided configuration.
func NewClientWith(c Config) *Client {
if c.URL == "" {
c.URL = apiURL
}

httpClient := &http.Client{Timeout: defaultHTTPTimeout}
if c.TorPort > 0 {
dialSocksProxy := socks.DialSocksProxy(socks.SOCKS5, fmt.Sprintf("127.0.0.1:%d", c.TorPort))
httpClient.Transport = &http.Transport{Dial: dialSocksProxy}
}

return &Client{
client: &http.Client{Timeout: defaultHTTPTimeout},
baseURL: devAPIURL,
client: httpClient,
baseURL: c.URL,
token: c.Token,
}
}



Loading…
Cancel
Save