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"
"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
@ -192,4 +199,4 @@ func RandStringBytesMaskImprSrc(n int) string {
}
return string(b)
}
}

View File

@ -1,39 +1,46 @@
package pastee
import "net/http"
import (
"net/http"
"time"
)
type Pastee struct {
ApiKey string
Base string
Base string
Client *http.Client
}
type Paste struct {
ID string `json:"id,omitempty"`
Views int `json:"views,omitempty"`
Encrypted bool `json:"encrypted,omitempty"`
Description string `json:"description,omitempty"`
Sections []*Section `json:"sections"`
ID string `json:"id,omitempty"`
Views int `json:"views,omitempty"`
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"`
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 {
Field string `json:"field"`
Code int `json:"code"`
Field string `json:"field"`
Code int `json:"code"`
Message string `json:"message"`
}
type PasteResponse struct {
Success bool `json:"success"`
Errors []*Error `json:"errors"`
Key string `json:"-"`
ID string `json:"id"`
Link string `json:"link"`
Success bool `json:"success"`
Errors []*Error `json:"errors"`
Key string `json:"-"`
ID string `json:"id"`
Link string `json:"link"`
}
type authRequest struct {
@ -42,23 +49,23 @@ type authRequest struct {
}
type AuthResponse struct {
Success bool `json:"success"`
Key string `json:"key"`
Success bool `json:"success"`
Key string `json:"key"`
}
type PaginationResponse struct {
Total int `json:"total"`
PerPage int `json:"per_page"`
CurrentPage int `json:"current_page"`
LastPage int `json:"last_page"`
NextPageURL string `json:"next_page_url"`
Total int `json:"total"`
PerPage int `json:"per_page"`
CurrentPage int `json:"current_page"`
LastPage int `json:"last_page"`
NextPageURL string `json:"next_page_url"`
PreviousPageURL string `json:"prev_page_url"`
From int `json:"from"`
To int `json:"to"`
From int `json:"from"`
To int `json:"to"`
}
type PasteListResponse struct {
*PaginationResponse
Data []*Paste `json:"data"`
}
}