Initial commit

This commit is contained in:
Tyler 2017-06-08 01:40:35 -04:00
commit 3c2e104234
2 changed files with 91 additions and 0 deletions

57
pastee.go Normal file
View File

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

34
structs.go Normal file
View File

@ -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"`
}