1
0
Fork 0

Move HTTP client to its own package

This commit is contained in:
Frédéric Guillot 2018-04-28 10:51:07 -07:00
parent 04adf5fdf5
commit 1eba1730d1
13 changed files with 64 additions and 64 deletions

View file

@ -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}
}

View file

@ -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"

View file

@ -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"

View file

@ -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

View file

@ -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)
}

View file

@ -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)
}

View file

@ -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)
}

View file

@ -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)
}

View file

@ -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 {

View file

@ -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)
}

View file

@ -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
}

View file

@ -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

View file

@ -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()