2023-06-19 17:42:47 -04:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2017-11-25 13:40:23 -05:00
|
|
|
|
2023-08-10 00:15:55 -04:00
|
|
|
package client // import "miniflux.app/v2/client"
|
2017-11-25 13:40:23 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-01-07 21:08:42 -05:00
|
|
|
userAgent = "Miniflux Client Library"
|
2017-11-25 13:40:23 -05:00
|
|
|
defaultTimeout = 80
|
|
|
|
)
|
|
|
|
|
2018-05-14 21:52:12 -04:00
|
|
|
// List of exposed errors.
|
2017-11-25 13:40:23 -05:00
|
|
|
var (
|
2018-05-14 21:52:12 -04:00
|
|
|
ErrNotAuthorized = errors.New("miniflux: unauthorized (bad credentials)")
|
|
|
|
ErrForbidden = errors.New("miniflux: access forbidden")
|
|
|
|
ErrServerError = errors.New("miniflux: internal server error")
|
|
|
|
ErrNotFound = errors.New("miniflux: resource not found")
|
2017-11-25 13:40:23 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type errorResponse struct {
|
|
|
|
ErrorMessage string `json:"error_message"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type request struct {
|
|
|
|
endpoint string
|
|
|
|
username string
|
|
|
|
password string
|
2020-03-01 20:38:29 -05:00
|
|
|
apiKey string
|
2017-11-25 13:40:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *request) Get(path string) (io.ReadCloser, error) {
|
|
|
|
return r.execute(http.MethodGet, path, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *request) Post(path string, data interface{}) (io.ReadCloser, error) {
|
|
|
|
return r.execute(http.MethodPost, path, data)
|
|
|
|
}
|
|
|
|
|
2018-04-29 21:56:40 -04:00
|
|
|
func (r *request) PostFile(path string, f io.ReadCloser) (io.ReadCloser, error) {
|
|
|
|
return r.execute(http.MethodPost, path, f)
|
|
|
|
}
|
|
|
|
|
2017-11-25 13:40:23 -05:00
|
|
|
func (r *request) Put(path string, data interface{}) (io.ReadCloser, error) {
|
|
|
|
return r.execute(http.MethodPut, path, data)
|
|
|
|
}
|
|
|
|
|
2020-07-27 21:41:27 -04:00
|
|
|
func (r *request) Delete(path string) error {
|
|
|
|
_, err := r.execute(http.MethodDelete, path, nil)
|
|
|
|
return err
|
2017-11-25 13:40:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *request) execute(method, path string, data interface{}) (io.ReadCloser, error) {
|
2017-12-26 15:10:48 -05:00
|
|
|
if r.endpoint[len(r.endpoint)-1:] == "/" {
|
|
|
|
r.endpoint = r.endpoint[:len(r.endpoint)-1]
|
|
|
|
}
|
|
|
|
|
2017-11-25 13:40:23 -05:00
|
|
|
u, err := url.Parse(r.endpoint + path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
request := &http.Request{
|
|
|
|
URL: u,
|
|
|
|
Method: method,
|
|
|
|
Header: r.buildHeaders(),
|
|
|
|
}
|
2020-03-01 20:38:29 -05:00
|
|
|
|
|
|
|
if r.username != "" && r.password != "" {
|
|
|
|
request.SetBasicAuth(r.username, r.password)
|
|
|
|
}
|
2017-11-25 13:40:23 -05:00
|
|
|
|
|
|
|
if data != nil {
|
2021-03-23 00:04:10 -04:00
|
|
|
switch data := data.(type) {
|
2018-04-29 21:56:40 -04:00
|
|
|
case io.ReadCloser:
|
2021-03-23 00:04:10 -04:00
|
|
|
request.Body = data
|
2018-04-29 21:56:40 -04:00
|
|
|
default:
|
2021-02-17 00:19:03 -05:00
|
|
|
request.Body = io.NopCloser(bytes.NewBuffer(r.toJSON(data)))
|
2018-04-29 21:56:40 -04:00
|
|
|
}
|
2017-11-25 13:40:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
client := r.buildClient()
|
|
|
|
response, err := client.Do(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch response.StatusCode {
|
|
|
|
case http.StatusUnauthorized:
|
2020-07-27 21:41:27 -04:00
|
|
|
response.Body.Close()
|
2018-05-14 21:52:12 -04:00
|
|
|
return nil, ErrNotAuthorized
|
2017-11-25 13:40:23 -05:00
|
|
|
case http.StatusForbidden:
|
2020-07-27 21:41:27 -04:00
|
|
|
response.Body.Close()
|
2018-05-14 21:52:12 -04:00
|
|
|
return nil, ErrForbidden
|
2017-11-25 13:40:23 -05:00
|
|
|
case http.StatusInternalServerError:
|
2021-06-16 05:26:39 -04:00
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
var resp errorResponse
|
|
|
|
decoder := json.NewDecoder(response.Body)
|
|
|
|
// If we failed to decode, just return a generic ErrServerError
|
|
|
|
if err := decoder.Decode(&resp); err != nil {
|
|
|
|
return nil, ErrServerError
|
|
|
|
}
|
|
|
|
return nil, errors.New("miniflux: internal server error: " + resp.ErrorMessage)
|
2018-04-29 21:56:40 -04:00
|
|
|
case http.StatusNotFound:
|
2020-07-27 21:41:27 -04:00
|
|
|
response.Body.Close()
|
2018-05-14 21:52:12 -04:00
|
|
|
return nil, ErrNotFound
|
2020-07-27 21:41:27 -04:00
|
|
|
case http.StatusNoContent:
|
|
|
|
response.Body.Close()
|
|
|
|
return nil, nil
|
2017-11-25 13:40:23 -05:00
|
|
|
case http.StatusBadRequest:
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
var resp errorResponse
|
|
|
|
decoder := json.NewDecoder(response.Body)
|
|
|
|
if err := decoder.Decode(&resp); err != nil {
|
|
|
|
return nil, fmt.Errorf("miniflux: bad request error (%v)", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("miniflux: bad request (%s)", resp.ErrorMessage)
|
|
|
|
}
|
|
|
|
|
2018-04-29 21:56:40 -04:00
|
|
|
if response.StatusCode > 400 {
|
2020-07-27 21:41:27 -04:00
|
|
|
response.Body.Close()
|
2018-04-29 21:56:40 -04:00
|
|
|
return nil, fmt.Errorf("miniflux: status code=%d", response.StatusCode)
|
2017-11-25 13:40:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return response.Body, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *request) buildClient() http.Client {
|
|
|
|
return http.Client{
|
|
|
|
Timeout: time.Duration(defaultTimeout * time.Second),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *request) buildHeaders() http.Header {
|
|
|
|
headers := make(http.Header)
|
|
|
|
headers.Add("User-Agent", userAgent)
|
|
|
|
headers.Add("Content-Type", "application/json")
|
|
|
|
headers.Add("Accept", "application/json")
|
2020-03-01 20:38:29 -05:00
|
|
|
if r.apiKey != "" {
|
|
|
|
headers.Add("X-Auth-Token", r.apiKey)
|
|
|
|
}
|
2017-11-25 13:40:23 -05:00
|
|
|
return headers
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *request) toJSON(v interface{}) []byte {
|
|
|
|
b, err := json.Marshal(v)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Unable to convert interface to JSON:", err)
|
|
|
|
return []byte("")
|
|
|
|
}
|
|
|
|
|
|
|
|
return b
|
|
|
|
}
|