Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,27 @@ type Client struct {
config *Config
}

// ClientOption is an optional configuration for the client.
type ClientOption func(*Client)

// WithHTTPRoundTripper sets a Bring-Your-Own http.RoundTripper.
// By default, http.DefaultTransport is set.
func WithHTTPRoundTripper(roundTripper http.RoundTripper) ClientOption {
return func(c *Client) {
c.httpClient.Transport = roundTripper
}
}

// NewClient returns a new freee API client.
func NewClient(config *Config) *Client {
return &Client{
config: config,
func NewClient(config *Config, options ...ClientOption) *Client {
client := &Client{
httpClient: &http.Client{Transport: http.DefaultTransport},
config: config,
}
for _, opt := range options {
opt(client)
}
return client
}

func (c *Client) call(ctx context.Context,
Expand Down Expand Up @@ -162,7 +178,7 @@ func (c *Client) do(
if c.config.EarlyExpiry != nil {
tokenSource = oauth2.ReuseTokenSourceWithExpiry(oauth2Token, tokenSource, *c.config.EarlyExpiry)
}
httpClient := oauth2.NewClient(ctx, tokenSource)
httpClient := oauth2.NewClient(context.WithValue(ctx, oauth2.HTTPClient, c.httpClient), tokenSource)
response, err := httpClient.Do(req)
if err != nil {
e := &oauth2.RetrieveError{}
Expand Down
51 changes: 51 additions & 0 deletions examples/roundtrip/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package main

import (
"context"
"fmt"
"log"
"net"
"net/http"
"os"
"time"

"github.com/LayerXcom/freee-go"
"golang.org/x/oauth2"
)

func main() {
clientID := os.Getenv("CLIENT_ID")
clientSecret := os.Getenv("CLIENT_SECRET")
redirectURL := os.Getenv("REDIRECT_URL")
conf := freee.NewConfig(clientID, clientSecret, redirectURL)
conf.Log = log.New(os.Stdout, "", log.LstdFlags)
client := freee.NewClient(
conf, freee.WithHTTPRoundTripper(
&http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
),
)

ctx := context.Background()
token := &oauth2.Token{
AccessToken: os.Getenv("ACCESS_TOKEN"),
RefreshToken: os.Getenv("REFRESH_TOKEN"),
}
me, token, err := client.GetUsersMe(ctx, token, freee.GetUsersMeOpts{})
if err != nil {
log.Fatal(err)
}

fmt.Printf("%#v\n", me)
fmt.Printf("%#v\n", token)
}