Add options and extra fields to support (possible soon) upcoming updates

This commit is contained in:
Tyler 2019-10-01 23:17:54 -04:00
parent 8ccd081957
commit b157d3be7e
5 changed files with 98 additions and 36 deletions

5
go.mod Normal file
View File

@ -0,0 +1,5 @@
module paste.ee/go
go 1.12
require github.com/google/go-querystring v1.0.0

2
go.sum Normal file
View File

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

41
options.go Normal file
View File

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

View File

@ -6,7 +6,6 @@ import (
"io" "io"
"math/rand" "math/rand"
"net/http" "net/http"
"net/url"
"strconv" "strconv"
"time" "time"
@ -50,12 +49,14 @@ func (p *Pastee) Authenticate(username, password string) (*AuthResponse, error)
return &response, nil return &response, nil
} }
func (p *Pastee) List() (*PasteListResponse, error) { func (p *Pastee) List(opts ...PasteListOptions) (*PasteListResponse, error) {
q := &url.Values{} urlPath := "pastes"
q.Set("perpage", "25")
q.Set("page", "1")
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 { if err != nil {
return nil, err return nil, err
@ -78,8 +79,14 @@ func (p *Pastee) List() (*PasteListResponse, error) {
return &response, nil return &response, nil
} }
func (p *Pastee) Get(id string) (*Paste, error) { func (p *Pastee) Get(id string, opts ...PasteGetOptions) (*Paste, error) {
req, err := p.newRequest("GET", "pastes/" + id, nil) urlPath := "pastes/" + id
if len(opts) > 0 {
urlPath += "?" + opts[0].encode()
}
req, err := p.newRequest("GET", urlPath, nil)
if err != nil { if err != nil {
return nil, err 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) { 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 { if err != nil {
return nil, err return nil, err

View File

@ -1,39 +1,46 @@
package pastee package pastee
import "net/http" import (
"net/http"
"time"
)
type Pastee struct { type Pastee struct {
ApiKey string ApiKey string
Base string Base string
Client *http.Client Client *http.Client
} }
type Paste struct { type Paste struct {
ID string `json:"id,omitempty"` ID string `json:"id,omitempty"`
Views int `json:"views,omitempty"` Views int `json:"views,omitempty"`
Encrypted bool `json:"encrypted,omitempty"` Encrypted bool `json:"encrypted,omitempty"`
Description string `json:"description,omitempty"` Description string `json:"description,omitempty"`
Sections []*Section `json:"sections"` Sections []*Section `json:"sections"`
CreatedAt time.Time `json:"created_at"`
ExpiresAt time.Time `json:"expires_at"`
} }
type Section struct { type Section struct {
Name string `json:"name,omitempty"` Name string `json:"name,omitempty"`
Syntax string `json:"syntax,omitempty"` Syntax string `json:"syntax,omitempty"`
Contents string `json:"contents"` Contents string `json:"contents"`
URL string `json:"url"`
Size int `json:"size"`
} }
type Error struct { type Error struct {
Field string `json:"field"` Field string `json:"field"`
Code int `json:"code"` Code int `json:"code"`
Message string `json:"message"` Message string `json:"message"`
} }
type PasteResponse struct { type PasteResponse struct {
Success bool `json:"success"` Success bool `json:"success"`
Errors []*Error `json:"errors"` Errors []*Error `json:"errors"`
Key string `json:"-"` Key string `json:"-"`
ID string `json:"id"` ID string `json:"id"`
Link string `json:"link"` Link string `json:"link"`
} }
type authRequest struct { type authRequest struct {
@ -42,19 +49,19 @@ type authRequest struct {
} }
type AuthResponse struct { type AuthResponse struct {
Success bool `json:"success"` Success bool `json:"success"`
Key string `json:"key"` Key string `json:"key"`
} }
type PaginationResponse struct { type PaginationResponse struct {
Total int `json:"total"` Total int `json:"total"`
PerPage int `json:"per_page"` PerPage int `json:"per_page"`
CurrentPage int `json:"current_page"` CurrentPage int `json:"current_page"`
LastPage int `json:"last_page"` LastPage int `json:"last_page"`
NextPageURL string `json:"next_page_url"` NextPageURL string `json:"next_page_url"`
PreviousPageURL string `json:"prev_page_url"` PreviousPageURL string `json:"prev_page_url"`
From int `json:"from"` From int `json:"from"`
To int `json:"to"` To int `json:"to"`
} }
type PasteListResponse struct { type PasteListResponse struct {