From 1eba1730d1af50ed545f4fde78b22d6fb62ca11e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= <fred@miniflux.net>
Date: Sat, 28 Apr 2018 10:51:07 -0700
Subject: [PATCH] Move HTTP client to its own package

---
 http/{ => client}/client.go            | 43 ++++++++++++++------------
 http/{ => client}/response.go          |  2 +-
 http/{ => client}/response_test.go     |  2 +-
 http/doc.go                            | 10 ------
 integration/instapaper/instapaper.go   |  7 +++--
 integration/nunuxkeeper/nunuxkeeper.go |  8 +++--
 integration/pinboard/pinboard.go       |  6 ++--
 integration/wallabag/wallabag.go       | 11 ++++---
 reader/feed/handler.go                 | 11 ++++---
 reader/icon/finder.go                  | 10 +++---
 reader/scraper/scraper.go              |  6 ++--
 reader/subscription/finder.go          |  6 ++--
 ui/proxy.go                            |  6 ++--
 13 files changed, 64 insertions(+), 64 deletions(-)
 rename http/{ => client}/client.go (85%)
 rename http/{ => client}/response.go (99%)
 rename http/{ => client}/response_test.go (99%)
 delete mode 100644 http/doc.go

diff --git a/http/client.go b/http/client/client.go
similarity index 85%
rename from http/client.go
rename to http/client/client.go
index 1884224c..76630641 100644
--- a/http/client.go
+++ b/http/client/client.go
@@ -1,8 +1,8 @@
-// Copyright 2017 Frédéric Guillot. All rights reserved.
+// Copyright 2018 Frédéric Guillot. All rights reserved.
 // Use of this source code is governed by the Apache 2.0
 // license that can be found in the LICENSE file.
 
-package http
+package client
 
 import (
 	"bytes"
@@ -49,6 +49,26 @@ type Client struct {
 	Insecure            bool
 }
 
+// WithCredentials defines the username/password for HTTP Basic authentication.
+func (c *Client) WithCredentials(username, password string) *Client {
+	c.username = username
+	c.password = password
+	return c
+}
+
+// WithAuthorization defines authorization header value.
+func (c *Client) WithAuthorization(authorization string) *Client {
+	c.authorizationHeader = authorization
+	return c
+}
+
+// WithCacheHeaders defines caching headers.
+func (c *Client) WithCacheHeaders(etagHeader, lastModifiedHeader string) *Client {
+	c.etagHeader = etagHeader
+	c.lastModifiedHeader = lastModifiedHeader
+	return c
+}
+
 // Get execute a GET HTTP request.
 func (c *Client) Get() (*Response, error) {
 	request, err := c.buildRequest(http.MethodGet, nil)
@@ -197,22 +217,7 @@ func (c *Client) buildHeaders() http.Header {
 	return headers
 }
 
-// NewClient returns a new HTTP client.
-func NewClient(url string) *Client {
+// New returns a new HTTP client.
+func New(url string) *Client {
 	return &Client{url: url, Insecure: false}
 }
-
-// NewClientWithCredentials returns a new HTTP client that requires authentication.
-func NewClientWithCredentials(url, username, password string) *Client {
-	return &Client{url: url, Insecure: false, username: username, password: password}
-}
-
-// NewClientWithAuthorization returns a new client with a custom authorization header.
-func NewClientWithAuthorization(url, authorization string) *Client {
-	return &Client{url: url, Insecure: false, authorizationHeader: authorization}
-}
-
-// NewClientWithCacheHeaders returns a new HTTP client that send cache headers.
-func NewClientWithCacheHeaders(url, etagHeader, lastModifiedHeader string) *Client {
-	return &Client{url: url, etagHeader: etagHeader, lastModifiedHeader: lastModifiedHeader, Insecure: false}
-}
diff --git a/http/response.go b/http/client/response.go
similarity index 99%
rename from http/response.go
rename to http/client/response.go
index a0cfc3fc..f033d610 100644
--- a/http/response.go
+++ b/http/client/response.go
@@ -2,7 +2,7 @@
 // Use of this source code is governed by the Apache 2.0
 // license that can be found in the LICENSE file.
 
-package http
+package client
 
 import (
 	"io"
diff --git a/http/response_test.go b/http/client/response_test.go
similarity index 99%
rename from http/response_test.go
rename to http/client/response_test.go
index c5f6a1c4..f3402a80 100644
--- a/http/response_test.go
+++ b/http/client/response_test.go
@@ -2,7 +2,7 @@
 // Use of this source code is governed by the Apache 2.0
 // license that can be found in the LICENSE file.
 
-package http
+package client
 
 import "testing"
 
diff --git a/http/doc.go b/http/doc.go
deleted file mode 100644
index f0eb1fa6..00000000
--- a/http/doc.go
+++ /dev/null
@@ -1,10 +0,0 @@
-// Copyright 2018 Frédéric Guillot. All rights reserved.
-// Use of this source code is governed by the MIT license
-// that can be found in the LICENSE file.
-
-/*
-
-Package http implements a set of utilities related to the HTTP protocol.
-
-*/
-package http
diff --git a/integration/instapaper/instapaper.go b/integration/instapaper/instapaper.go
index 33a25353..52d8ee14 100644
--- a/integration/instapaper/instapaper.go
+++ b/integration/instapaper/instapaper.go
@@ -8,7 +8,7 @@ import (
 	"fmt"
 	"net/url"
 
-	"github.com/miniflux/miniflux/http"
+	"github.com/miniflux/miniflux/http/client"
 )
 
 // Client represents an Instapaper client.
@@ -24,8 +24,9 @@ func (c *Client) AddURL(link, title string) error {
 	values.Add("title", title)
 
 	apiURL := "https://www.instapaper.com/api/add?" + values.Encode()
-	client := http.NewClientWithCredentials(apiURL, c.username, c.password)
-	response, err := client.Get()
+	clt := client.New(apiURL)
+	clt.WithCredentials(c.username, c.password)
+	response, err := clt.Get()
 	if response.HasServerFailure() {
 		return fmt.Errorf("instapaper: unable to send url, status=%d", response.StatusCode)
 	}
diff --git a/integration/nunuxkeeper/nunuxkeeper.go b/integration/nunuxkeeper/nunuxkeeper.go
index 4627e8d8..4ea5c45b 100644
--- a/integration/nunuxkeeper/nunuxkeeper.go
+++ b/integration/nunuxkeeper/nunuxkeeper.go
@@ -9,7 +9,7 @@ import (
 	"net/url"
 	"path"
 
-	"github.com/miniflux/miniflux/http"
+	"github.com/miniflux/miniflux/http/client"
 )
 
 // Document structure of a Nununx Keeper document
@@ -39,8 +39,10 @@ func (c *Client) AddEntry(link, title, content string) error {
 	if err != nil {
 		return err
 	}
-	client := http.NewClientWithCredentials(apiURL, "api", c.apiKey)
-	response, err := client.PostJSON(doc)
+
+	clt := client.New(apiURL)
+	clt.WithCredentials("api", c.apiKey)
+	response, err := clt.PostJSON(doc)
 	if response.HasServerFailure() {
 		return fmt.Errorf("nunux-keeper: unable to send entry, status=%d", response.StatusCode)
 	}
diff --git a/integration/pinboard/pinboard.go b/integration/pinboard/pinboard.go
index bad65b1e..54294ae5 100644
--- a/integration/pinboard/pinboard.go
+++ b/integration/pinboard/pinboard.go
@@ -8,7 +8,7 @@ import (
 	"fmt"
 	"net/url"
 
-	"github.com/miniflux/miniflux/http"
+	"github.com/miniflux/miniflux/http/client"
 )
 
 // Client represents a Pinboard client.
@@ -30,8 +30,8 @@ func (c *Client) AddBookmark(link, title, tags string, markAsUnread bool) error
 	values.Add("tags", tags)
 	values.Add("toread", toRead)
 
-	client := http.NewClient("https://api.pinboard.in/v1/posts/add?" + values.Encode())
-	response, err := client.Get()
+	clt := client.New("https://api.pinboard.in/v1/posts/add?" + values.Encode())
+	response, err := clt.Get()
 	if response.HasServerFailure() {
 		return fmt.Errorf("pinboard: unable to send bookmark, status=%d", response.StatusCode)
 	}
diff --git a/integration/wallabag/wallabag.go b/integration/wallabag/wallabag.go
index fbb100a8..58d9c5b7 100644
--- a/integration/wallabag/wallabag.go
+++ b/integration/wallabag/wallabag.go
@@ -10,7 +10,7 @@ import (
 	"io"
 	"net/url"
 
-	"github.com/miniflux/miniflux/http"
+	"github.com/miniflux/miniflux/http/client"
 )
 
 // Client represents a Wallabag client.
@@ -38,8 +38,9 @@ func (c *Client) createEntry(accessToken, link, title string) error {
 		return fmt.Errorf("wallbag: unable to get entries endpoint: %v", err)
 	}
 
-	client := http.NewClientWithAuthorization(endpoint, "Bearer "+accessToken)
-	response, err := client.PostJSON(map[string]string{"url": link, "title": title})
+	clt := client.New(endpoint)
+	clt.WithAuthorization("Bearer " + accessToken)
+	response, err := clt.PostJSON(map[string]string{"url": link, "title": title})
 	if err != nil {
 		return fmt.Errorf("wallabag: unable to post entry: %v", err)
 	}
@@ -64,8 +65,8 @@ func (c *Client) getAccessToken() (string, error) {
 		return "", fmt.Errorf("wallbag: unable to get token endpoint: %v", err)
 	}
 
-	client := http.NewClient(endpoint)
-	response, err := client.PostForm(values)
+	clt := client.New(endpoint)
+	response, err := clt.PostForm(values)
 	if err != nil {
 		return "", fmt.Errorf("wallabag: unable to get access token: %v", err)
 	}
diff --git a/reader/feed/handler.go b/reader/feed/handler.go
index da02e991..4b508207 100644
--- a/reader/feed/handler.go
+++ b/reader/feed/handler.go
@@ -9,7 +9,7 @@ import (
 	"time"
 
 	"github.com/miniflux/miniflux/errors"
-	"github.com/miniflux/miniflux/http"
+	"github.com/miniflux/miniflux/http/client"
 	"github.com/miniflux/miniflux/locale"
 	"github.com/miniflux/miniflux/logger"
 	"github.com/miniflux/miniflux/model"
@@ -43,8 +43,8 @@ func (h *Handler) CreateFeed(userID, categoryID int64, url string, crawler bool)
 		return nil, errors.NewLocalizedError(errCategoryNotFound)
 	}
 
-	client := http.NewClient(url)
-	response, err := client.Get()
+	clt := client.New(url)
+	response, err := clt.Get()
 	if err != nil {
 		if _, ok := err.(*errors.LocalizedError); ok {
 			return nil, err
@@ -129,8 +129,9 @@ func (h *Handler) RefreshFeed(userID, feedID int64) error {
 		return errors.NewLocalizedError(errNotFound, feedID)
 	}
 
-	client := http.NewClientWithCacheHeaders(originalFeed.FeedURL, originalFeed.EtagHeader, originalFeed.LastModifiedHeader)
-	response, err := client.Get()
+	clt := client.New(originalFeed.FeedURL)
+	clt.WithCacheHeaders(originalFeed.EtagHeader, originalFeed.LastModifiedHeader)
+	response, err := clt.Get()
 	if err != nil {
 		var customErr errors.LocalizedError
 		if lerr, ok := err.(*errors.LocalizedError); ok {
diff --git a/reader/icon/finder.go b/reader/icon/finder.go
index 2e7cb16f..296ddeca 100644
--- a/reader/icon/finder.go
+++ b/reader/icon/finder.go
@@ -12,7 +12,7 @@ import (
 	"strings"
 
 	"github.com/miniflux/miniflux/crypto"
-	"github.com/miniflux/miniflux/http"
+	"github.com/miniflux/miniflux/http/client"
 	"github.com/miniflux/miniflux/logger"
 	"github.com/miniflux/miniflux/model"
 	"github.com/miniflux/miniflux/url"
@@ -23,8 +23,8 @@ import (
 // FindIcon try to find the website's icon.
 func FindIcon(websiteURL string) (*model.Icon, error) {
 	rootURL := url.RootURL(websiteURL)
-	client := http.NewClient(rootURL)
-	response, err := client.Get()
+	clt := client.New(rootURL)
+	response, err := clt.Get()
 	if err != nil {
 		return nil, fmt.Errorf("unable to download website index page: %v", err)
 	}
@@ -87,8 +87,8 @@ func parseDocument(websiteURL string, data io.Reader) (string, error) {
 }
 
 func downloadIcon(iconURL string) (*model.Icon, error) {
-	client := http.NewClient(iconURL)
-	response, err := client.Get()
+	clt := client.New(iconURL)
+	response, err := clt.Get()
 	if err != nil {
 		return nil, fmt.Errorf("unable to download iconURL: %v", err)
 	}
diff --git a/reader/scraper/scraper.go b/reader/scraper/scraper.go
index b90cc14d..17e67145 100644
--- a/reader/scraper/scraper.go
+++ b/reader/scraper/scraper.go
@@ -11,7 +11,7 @@ import (
 	"strings"
 
 	"github.com/PuerkitoBio/goquery"
-	"github.com/miniflux/miniflux/http"
+	"github.com/miniflux/miniflux/http/client"
 	"github.com/miniflux/miniflux/logger"
 	"github.com/miniflux/miniflux/reader/readability"
 	"github.com/miniflux/miniflux/url"
@@ -19,8 +19,8 @@ import (
 
 // Fetch downloads a web page a returns relevant contents.
 func Fetch(websiteURL, rules string) (string, error) {
-	client := http.NewClient(websiteURL)
-	response, err := client.Get()
+	clt := client.New(websiteURL)
+	response, err := clt.Get()
 	if err != nil {
 		return "", err
 	}
diff --git a/reader/subscription/finder.go b/reader/subscription/finder.go
index 4e2879a1..e5a27f5a 100644
--- a/reader/subscription/finder.go
+++ b/reader/subscription/finder.go
@@ -11,7 +11,7 @@ import (
 	"time"
 
 	"github.com/miniflux/miniflux/errors"
-	"github.com/miniflux/miniflux/http"
+	"github.com/miniflux/miniflux/http/client"
 	"github.com/miniflux/miniflux/logger"
 	"github.com/miniflux/miniflux/reader/feed"
 	"github.com/miniflux/miniflux/timer"
@@ -30,8 +30,8 @@ var (
 func FindSubscriptions(websiteURL string) (Subscriptions, error) {
 	defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[FindSubscriptions] url=%s", websiteURL))
 
-	client := http.NewClient(websiteURL)
-	response, err := client.Get()
+	clt := client.New(websiteURL)
+	response, err := clt.Get()
 	if err != nil {
 		if _, ok := err.(errors.LocalizedError); ok {
 			return nil, err
diff --git a/ui/proxy.go b/ui/proxy.go
index 5237d006..78ab67de 100644
--- a/ui/proxy.go
+++ b/ui/proxy.go
@@ -11,7 +11,7 @@ import (
 	"time"
 
 	"github.com/miniflux/miniflux/crypto"
-	"github.com/miniflux/miniflux/http"
+	"github.com/miniflux/miniflux/http/client"
 	"github.com/miniflux/miniflux/http/handler"
 	"github.com/miniflux/miniflux/logger"
 )
@@ -36,8 +36,8 @@ func (c *Controller) ImageProxy(ctx *handler.Context, request *handler.Request,
 		return
 	}
 
-	client := http.NewClient(string(decodedURL))
-	resp, err := client.Get()
+	clt := client.New(string(decodedURL))
+	resp, err := clt.Get()
 	if err != nil {
 		logger.Error("[Controller:ImageProxy] %v", err)
 		response.HTML().NotFound()
-- 
GitLab