commit 3c2e10423470d1fb165699245dfab7a60123a6ed Author: Tyler Date: Thu Jun 8 01:40:35 2017 -0400 Initial commit diff --git a/pastee.go b/pastee.go new file mode 100644 index 0000000..54a487d --- /dev/null +++ b/pastee.go @@ -0,0 +1,57 @@ +package pastee + +import ( + "net/http" + "io" + "encoding/json" + "bytes" +) + +func New(key string) *Pastee { + return &Pastee{ApiKey: key, Base: "https://api.paste.ee/v1", Client: &http.Client{}} +} + +func (p *Pastee) Submit(paste *Paste) (*PasteResponse, error) { + body, err := json.Marshal(paste) + + if err != nil { + return nil, err + } + + req, err := p.newRequest("POST", "pastes", bytes.NewReader(body)) + + if err != nil { + return nil, err + } + + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Content-Length", len(body)) + + res, err := p.Client.Do(req) + + if err != nil { + return nil, err + } + + defer res.Body.Close() + + var response PasteResponse + + if err := json.NewDecoder(res.Body).Decode(&response); err != nil { + return nil, err + } + + return &response, nil +} + +func (p *Pastee) newRequest(method, path string, body io.Reader) (*http.Request, error) { + req, err := http.NewRequest(method, p.Base + path, body) + + if err != nil { + return nil, err + } + + req.Header.Set("X-Auth-Token", p.ApiKey) + + return req, nil +} \ No newline at end of file diff --git a/structs.go b/structs.go new file mode 100644 index 0000000..0e66748 --- /dev/null +++ b/structs.go @@ -0,0 +1,34 @@ +package pastee + +import "net/http" + +type Pastee struct { + ApiKey string + Base string + Client *http.Client +} + +type Paste struct { + Encrypted bool `json:"encrypted,omitempty"` + Description string `json:"description,omitempty"` + Sections []*Section `json:"sections"` +} + +type Section struct { + Name string `json:"name,omitempty"` + Syntax string `json:"syntax,omitempty"` + Contents string `json:"contents"` +} + +type Error struct { + Field string `json:"field"` + Code int `json:"code"` + Message string `json:"message"` +} + +type PasteResponse struct { + Success bool `json:"success"` + Errors []*Error `json:"errors"` + ID string `json:"id"` + Link string `json:"link"` +} \ No newline at end of file