1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-07-12 16:58:36 +00:00

Add API endpoint to import OPML file

This commit is contained in:
Frédéric Guillot 2018-04-29 18:56:40 -07:00
parent 7a1653a2e9
commit 5cacae6cf2
9 changed files with 96 additions and 23 deletions

View file

@ -26,7 +26,7 @@ package main
import (
"fmt"
"io/ioutil"
"github.com/miniflux/miniflux-go"
)
@ -41,7 +41,7 @@ func main() {
}
fmt.Println(feeds)
// Backup to opml file.
// Backup your feeds to an OPML file.
opml, err := client.Export()
if err != nil {
fmt.Println(err)
@ -53,8 +53,8 @@ func main() {
fmt.Println(err)
return
}
fmt.Println("backup done!")
fmt.Println("backup done!")
}
```

View file

@ -7,6 +7,7 @@ package miniflux
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/url"
"strconv"
@ -230,6 +231,12 @@ func (c *Client) Export() ([]byte, error) {
return opml, nil
}
// Import imports an OPML file.
func (c *Client) Import(f io.ReadCloser) error {
_, err := c.request.PostFile("/v1/import", f)
return err
}
// Feed gets a feed.
func (c *Client) Feed(feedID int64) (*Feed, error) {
body, err := c.request.Get(fmt.Sprintf("/v1/feeds/%d", feedID))

View file

@ -26,6 +26,7 @@ var (
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")
)
type errorResponse struct {
@ -46,6 +47,10 @@ func (r *request) Post(path string, data interface{}) (io.ReadCloser, error) {
return r.execute(http.MethodPost, path, data)
}
func (r *request) PostFile(path string, f io.ReadCloser) (io.ReadCloser, error) {
return r.execute(http.MethodPost, path, f)
}
func (r *request) Put(path string, data interface{}) (io.ReadCloser, error) {
return r.execute(http.MethodPut, path, data)
}
@ -72,7 +77,12 @@ func (r *request) execute(method, path string, data interface{}) (io.ReadCloser,
request.SetBasicAuth(r.username, r.password)
if data != nil {
request.Body = ioutil.NopCloser(bytes.NewBuffer(r.toJSON(data)))
switch data.(type) {
case io.ReadCloser:
request.Body = data.(io.ReadCloser)
default:
request.Body = ioutil.NopCloser(bytes.NewBuffer(r.toJSON(data)))
}
}
client := r.buildClient()
@ -88,6 +98,8 @@ func (r *request) execute(method, path string, data interface{}) (io.ReadCloser,
return nil, errForbidden
case http.StatusInternalServerError:
return nil, errServerError
case http.StatusNotFound:
return nil, errNotFound
case http.StatusBadRequest:
defer response.Body.Close()
@ -100,8 +112,8 @@ func (r *request) execute(method, path string, data interface{}) (io.ReadCloser,
return nil, fmt.Errorf("miniflux: bad request (%s)", resp.ErrorMessage)
}
if response.StatusCode >= 400 {
return nil, fmt.Errorf("miniflux: server error (statusCode=%d)", response.StatusCode)
if response.StatusCode > 400 {
return nil, fmt.Errorf("miniflux: status code=%d", response.StatusCode)
}
return response.Body, nil