1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-09-15 18:57:04 +00:00

Add per-application API Keys

This commit is contained in:
Frédéric Guillot 2020-03-01 17:38:29 -08:00
parent d1afe13a1c
commit 25cc0d2447
35 changed files with 940 additions and 71 deletions

View file

@ -24,8 +24,12 @@ import (
)
func main() {
// Authentication with username/password:
client := miniflux.New("https://api.example.org", "admin", "secret")
// Authentication with an API Key:
client := miniflux.New("https://api.example.org", "my-secret-token")
// Fetch all feeds.
feeds, err := client.Feeds()
if err != nil {

View file

@ -18,6 +18,14 @@ type Client struct {
request *request
}
// New returns a new Miniflux client.
func New(endpoint string, credentials ...string) *Client {
if len(credentials) == 2 {
return &Client{request: &request{endpoint: endpoint, username: credentials[0], password: credentials[1]}}
}
return &Client{request: &request{endpoint: endpoint, apiKey: credentials[0]}}
}
// Me returns the logged user information.
func (c *Client) Me() (*User, error) {
body, err := c.request.Get("/v1/me")
@ -448,11 +456,6 @@ func (c *Client) ToggleBookmark(entryID int64) error {
return nil
}
// New returns a new Miniflux client.
func New(endpoint, username, password string) *Client {
return &Client{request: &request{endpoint: endpoint, username: username, password: password}}
}
func buildFilterQueryString(path string, filter *Filter) string {
if filter != nil {
values := url.Values{}

View file

@ -38,6 +38,7 @@ type request struct {
endpoint string
username string
password string
apiKey string
}
func (r *request) Get(path string) (io.ReadCloser, error) {
@ -75,7 +76,10 @@ func (r *request) execute(method, path string, data interface{}) (io.ReadCloser,
Method: method,
Header: r.buildHeaders(),
}
request.SetBasicAuth(r.username, r.password)
if r.username != "" && r.password != "" {
request.SetBasicAuth(r.username, r.password)
}
if data != nil {
switch data.(type) {
@ -131,6 +135,9 @@ func (r *request) buildHeaders() http.Header {
headers.Add("User-Agent", userAgent)
headers.Add("Content-Type", "application/json")
headers.Add("Accept", "application/json")
if r.apiKey != "" {
headers.Add("X-Auth-Token", r.apiKey)
}
return headers
}