From 0e49559d0effb845c8392e00a3b860464ed88002 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Sun, 2 Jan 2022 03:05:14 +0700 Subject: lib/http: refactoring NewClient to accept single struct Previously, the NewClient function accept three parameters: serverURL, http.Header, and insecure. If we want to add another parameter, for example timeout it will cause changes on the function signature. To prevent this changes in the future, we change it now. The NewClient now accept single struct. While at it, we add option to set Timeout. The Timeout affect the http Transport Timeout and TLSHandshakeTimeout. The field is optional, if not set it will set to 10 seconds. --- api/slack/webhook_client.go | 6 ++++-- api/telegram/bot/bot.go | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) (limited to 'api') diff --git a/api/slack/webhook_client.go b/api/slack/webhook_client.go index 1896f9fc..e5ac2f45 100644 --- a/api/slack/webhook_client.go +++ b/api/slack/webhook_client.go @@ -35,9 +35,11 @@ func NewWebhookClient(webhookURL, user, channel string) (wcl *WebhookClient, err return nil, fmt.Errorf("NewWebhookClient: %w", err) } - host := fmt.Sprintf("%s://%s", wurl.Scheme, wurl.Host) + clientOpts := &libhttp.ClientOptions{ + ServerUrl: fmt.Sprintf("%s://%s", wurl.Scheme, wurl.Host), + } wcl = &WebhookClient{ - Client: libhttp.NewClient(host, nil, false), + Client: libhttp.NewClient(clientOpts), webhookPath: wurl.Path, user: user, channel: channel, diff --git a/api/telegram/bot/bot.go b/api/telegram/bot/bot.go index 844a8c25..1a5eeb97 100644 --- a/api/telegram/bot/bot.go +++ b/api/telegram/bot/bot.go @@ -114,10 +114,12 @@ func New(opts Options) (bot *Bot, err error) { return nil, fmt.Errorf("bot.New: %w", err) } - serverURL := defURL + opts.Token + "/" + clientOpts := &http.ClientOptions{ + ServerUrl: defURL + opts.Token + "/", + } bot = &Bot{ opts: opts, - client: http.NewClient(serverURL, nil, false), + client: http.NewClient(clientOpts), } fmt.Printf("Bot options: %+v\n", opts) -- cgit v1.3-6-g1900