Add options and extra fields to support (possible soon) upcoming updates
This commit is contained in:
parent
8ccd081957
commit
b157d3be7e
|
@ -0,0 +1,5 @@
|
|||
module paste.ee/go
|
||||
|
||||
go 1.12
|
||||
|
||||
require github.com/google/go-querystring v1.0.0
|
|
@ -0,0 +1,2 @@
|
|||
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
|
||||
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
|
|
@ -0,0 +1,41 @@
|
|||
package pastee
|
||||
|
||||
import "github.com/google/go-querystring/query"
|
||||
|
||||
type PasteGetOptions struct {
|
||||
Urls bool `url:"urls,omitempty"`
|
||||
TimeFormat string `url:"time_format,omitempty"`
|
||||
}
|
||||
|
||||
func (o PasteGetOptions) encode() string {
|
||||
v, err := query.Values(o)
|
||||
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
return v.Encode()
|
||||
}
|
||||
|
||||
type PasteListOptions struct {
|
||||
PerPage int `url:"perpage"`
|
||||
Page int `url:"page,omitempty"`
|
||||
}
|
||||
|
||||
func (o PasteListOptions) encode() string {
|
||||
if o.PerPage == 0 {
|
||||
o.PerPage = 25
|
||||
}
|
||||
|
||||
if o.Page == 0 {
|
||||
o.Page = 1
|
||||
}
|
||||
|
||||
v, err := query.Values(o)
|
||||
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
return v.Encode()
|
||||
}
|
25
pastee.go
25
pastee.go
|
@ -6,7 +6,6 @@ import (
|
|||
"io"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
|
@ -50,12 +49,14 @@ func (p *Pastee) Authenticate(username, password string) (*AuthResponse, error)
|
|||
return &response, nil
|
||||
}
|
||||
|
||||
func (p *Pastee) List() (*PasteListResponse, error) {
|
||||
q := &url.Values{}
|
||||
q.Set("perpage", "25")
|
||||
q.Set("page", "1")
|
||||
func (p *Pastee) List(opts ...PasteListOptions) (*PasteListResponse, error) {
|
||||
urlPath := "pastes"
|
||||
|
||||
req, err := p.newRequest("GET", "pastes?" + q.Encode(), nil)
|
||||
if len(opts) > 0 {
|
||||
urlPath += "?" + opts[0].encode()
|
||||
}
|
||||
|
||||
req, err := p.newRequest("GET", urlPath, nil)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -78,8 +79,14 @@ func (p *Pastee) List() (*PasteListResponse, error) {
|
|||
return &response, nil
|
||||
}
|
||||
|
||||
func (p *Pastee) Get(id string) (*Paste, error) {
|
||||
req, err := p.newRequest("GET", "pastes/" + id, nil)
|
||||
func (p *Pastee) Get(id string, opts ...PasteGetOptions) (*Paste, error) {
|
||||
urlPath := "pastes/" + id
|
||||
|
||||
if len(opts) > 0 {
|
||||
urlPath += "?" + opts[0].encode()
|
||||
}
|
||||
|
||||
req, err := p.newRequest("GET", urlPath, nil)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -156,7 +163,7 @@ func (p *Pastee) Submit(paste *Paste) (*PasteResponse, error) {
|
|||
}
|
||||
|
||||
func (p *Pastee) newRequest(method, path string, body io.Reader) (*http.Request, error) {
|
||||
req, err := http.NewRequest(method, p.Base + "/" + path, body)
|
||||
req, err := http.NewRequest(method, p.Base+"/"+path, body)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
package pastee
|
||||
|
||||
import "net/http"
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Pastee struct {
|
||||
ApiKey string
|
||||
|
@ -14,12 +17,16 @@ type Paste struct {
|
|||
Encrypted bool `json:"encrypted,omitempty"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Sections []*Section `json:"sections"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
}
|
||||
|
||||
type Section struct {
|
||||
Name string `json:"name,omitempty"`
|
||||
Syntax string `json:"syntax,omitempty"`
|
||||
Contents string `json:"contents"`
|
||||
URL string `json:"url"`
|
||||
Size int `json:"size"`
|
||||
}
|
||||
|
||||
type Error struct {
|
||||
|
|
Loading…
Reference in New Issue