diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e17a7f8 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module paste.ee/go + +go 1.12 + +require github.com/google/go-querystring v1.0.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..0bb18dc --- /dev/null +++ b/go.sum @@ -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= diff --git a/options.go b/options.go new file mode 100644 index 0000000..12f7f71 --- /dev/null +++ b/options.go @@ -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() +} diff --git a/pastee.go b/pastee.go index a8fc93c..4d135f8 100644 --- a/pastee.go +++ b/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 @@ -192,4 +199,4 @@ func RandStringBytesMaskImprSrc(n int) string { } return string(b) -} \ No newline at end of file +} diff --git a/structs.go b/structs.go index fed0dd3..b2f4a84 100644 --- a/structs.go +++ b/structs.go @@ -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"` -} \ No newline at end of file +}