1
0
Fork 0

Update to Lenpaste API v1.2

main v1.2.0
Leonid Maslakov 8 months ago
parent 7287f388e8
commit 4a879ea09c

@ -17,6 +17,7 @@ import (
func main() {
pasteSrv := pasteapi.NewServer("https://paste.lcomrade.su/")
//pasteSrv.Auth("user", "pass")
// Getting server information
srvInfo, err := pasteSrv.GetServerInfo()
@ -31,15 +32,18 @@ func main() {
fmt.Println("Maximum paste lifetime:", srvInfo.MaxLifeTime)
fmt.Println("About this server:", srvInfo.ServerAbout)
fmt.Println("Server rules:", srvInfo.ServerRules)
fmt.Println("Server Term of Use:", srvInfo.ServerTermsOfUse)
fmt.Println("Administrator name:", srvInfo.AdminName)
fmt.Println("Administrator mail:", srvInfo.AdminMail)
fmt.Println("Syntaxes:", srvInfo.Syntaxes)
fmt.Println("UI default paste lifetime:", srvInfo.UiDefaultLifeTime)
fmt.Println("Need auth to create pastes:", srvInfo.AuthRequired)
// Creating a paste
fmt.Println("\n## TEST ##")
fmt.Println("Creating a new paste...")
pasteID, err := pasteSrv.New(pasteapi.NewPaste{
pasteID, _, _, err := pasteSrv.New(pasteapi.NewPaste{
Title: "Test paste",
Body: `package main
@ -55,6 +59,9 @@ func main() {
AuthorEmail: "me@example.org",
AuthorURL: "example.org",
})
if err != nil {
panic(err)
}
fmt.Println("A new paste is created:", pasteSrv.Server+"/"+pasteID)
// Getting the paste

@ -7,17 +7,18 @@ package pasteapi
import (
"encoding/json"
"errors"
"strings"
"net/http"
"strconv"
"strings"
)
var (
ErrBadRequest = errors.New("lenpaste: bad request") // HTTP code 400; This API method exists on the server, but you passed the wrong arguments for it.
ErrNotFoundID = errors.New("lenpaste: could not find ID") // HTTP code 404; There is no paste with this ID.
ErrNotFound = errors.New("lenpaste: not found") // HTTP code 404; There is no such API method.
ErrMethodNotAllowed = errors.New("lenpaste: method not allowed") // HTTP code 405; You made a mistake with HTTP request (example: you made POST instead of GET).
ErrInternalServerError = errors.New("lenpaste: internal server error") // HTTP code 500; There was a failure on the server.
ErrUnknown = errors.New("lenpaste: unknown") // Other HTTP code not equal to 200.
ErrBadRequest = errors.New("lenpaste: 400 bad request") // HTTP code 400; This API method exists on the server, but you passed the wrong arguments for it.
ErrUnauthorized = errors.New("lenpaste: 401 unauthorized") // HTTP code 401; This server requires authentication.
ErrNotFoundID = errors.New("lenpaste: 404 could not find ID") // HTTP code 404; There is no paste with this ID.
ErrNotFound = errors.New("lenpaste: 404 not found") // HTTP code 404; There is no such API method.
ErrMethodNotAllowed = errors.New("lenpaste: 405 method not allowed") // HTTP code 405; You made a mistake with HTTP request (example: you made POST instead of GET).
ErrInternalServerError = errors.New("lenpaste: 500 internal server error") // HTTP code 500; There was a failure on the server.
)
const (
@ -30,6 +31,10 @@ type Server struct {
// Example: https://paste.lcomrade.su
// Warning: If you add / to the end of the address, it will cause an error!
Server string
// If the user or password is blank, no authorization will be performed.
AuthUser string
AuthPass string
}
type errorAnswer struct {
@ -44,7 +49,7 @@ type NewPaste struct {
Syntax string // Web interface syntax highlighting (default: plaintext).
OneUse bool // If set to "true", the paste can be viewed once (default: false).
Expiration int64 // The time in seconds that the paste will live (default: unlimited).
Author string // Author name (may not be set).
Author string // Author name (may not be set).
AuthorEmail string // Author email (may not be set).
AuthorURL string // Author URL (may not be set).
}
@ -57,21 +62,24 @@ type Paste struct {
DeleteTime int64 `json:"deleteTime"` // The time after which the paste will be removed. If set to "0", the paste has no expiration date.
OneUse bool `json:"oneUse"` // If set to "true", the paste can be viewed once.
Syntax string `json:"syntax"` // Web interface syntax highlighting. If set to "plaintext", syntax highlighting is disabled.
Author string `json:"author"` // Author name (can be blank).
Author string `json:"author"` // Author name (can be blank).
AuthorEmail string `json:"authorEmail"` // Author email (can be blank).
AuthorURL string `json:"authorURL"` // Author URL (can be blank).
}
type ServerInfo struct {
Version string `json:"version"` // Version of Lenpaste installed on server.
TitleMaxLen int `json:"titleMaxlength"` // Maximum length paste title can have. If 0 disable title, if -1 disable length limit.
BodyMaxLen int `json:"bodyMaxlength"` // Maximum length paste body can have. If -1 disable length limit. Can't be -1.
MaxLifeTime int64 `json:"maxLifeTime"` // Maximum "Expiration" value.
ServerAbout string `json:"serverAbout"` // Server description (can be blank).
ServerRules string `json:"serverRules"` // Server rules (can be blank).
AdminName string `json:"adminName"` // Server administrator name (can be blank).
AdminMail string `json:"adminMail"` // Server administrator email (can be blank).
Syntaxes []string `json:"syntaxes"` // Syntaxes that this server can highlight in the web interface. For example: "plaintext", "Go", "C", and so on.
Version string `json:"version"` // Version of Lenpaste installed on server.
TitleMaxLen int `json:"titleMaxlength"` // Maximum length paste title can have. If 0 disable title, if -1 disable length limit.
BodyMaxLen int `json:"bodyMaxlength"` // Maximum length paste body can have. If -1 disable length limit. Can't be -1.
MaxLifeTime int64 `json:"maxLifeTime"` // Maximum "Expiration" value.
ServerAbout string `json:"serverAbout"` // Server description (can be blank).
ServerRules string `json:"serverRules"` // Server rules (can be blank).
ServerTermsOfUse string `json:"serverTermsOfUse"` // Server Terms of Use (can be blank).
AdminName string `json:"adminName"` // Server administrator name (can be blank).
AdminMail string `json:"adminMail"` // Server administrator email (can be blank).
Syntaxes []string `json:"syntaxes"` // Syntaxes that this server can highlight in the web interface. For example: "plaintext", "Go", "C", and so on.
UiDefaultLifeTime string `json:"uiDefaultLifeTime"` // Default lifetime of paste selected in UI. Possible values: 10min, 1h, 1d, 2w, 6mon, 1y.
AuthRequired bool `json:"authRequired"` // If true, authorization is required to create paste.
}
// NewServer is intended to replace the manual filling of fields in the Server structure.
@ -82,6 +90,15 @@ func NewServer(address string) Server {
}
}
// Auth remembers the user and password for authorization on the server requesting it.
//
// The correctness of the data entered is not checked.
// If the user or password is blank, no authorization will be performed.
func (server *Server) Auth(user string, pass string) {
server.AuthUser = user
server.AuthPass = pass
}
func checkStatus(resp *http.Response) error {
if resp.StatusCode != 200 {
var errInfo errorAnswer
@ -95,6 +112,9 @@ func checkStatus(resp *http.Response) error {
case "Bad Request":
return ErrBadRequest
case "Unauthorized":
return ErrUnauthorized
case "Could not find ID":
return ErrNotFoundID
@ -108,7 +128,7 @@ func checkStatus(resp *http.Response) error {
return ErrInternalServerError
default:
return ErrUnknown
return errors.New("lenpaste: server return " + strconv.Itoa(resp.StatusCode) + " HTTP code")
}
}

@ -9,17 +9,21 @@ import (
"net/http"
"net/url"
"strconv"
"strings"
)
type newPasteAnswer struct {
ID string `json:"id"`
ID string `json:"id"`
CreateTime int64 `json:"createTime"`
DeleteTime int64 `json:"deleteTime"`
}
// New creates a new paste and returns its ID.
// New creates a new paste and returns its ID, create time, delete time.
// If the delete time is 0, then the paste will exist forever.
//
// Remember that you observe the limits on header length, body length, and maximum lifetime on your own.
// If this function returns ErrBadRequest, the problem is probably not respecting the limits.
func (server Server) New(newPaste NewPaste) (string, error) {
func (server Server) New(newPaste NewPaste) (string, int64, int64, error) {
// Prepare data
data := url.Values{}
data.Add("title", newPaste.Title)
@ -33,24 +37,32 @@ func (server Server) New(newPaste NewPaste) (string, error) {
data.Add("authorURL", newPaste.AuthorURL)
// Make POST request
resp, err := http.PostForm(server.Server+"/api/v1/new", data)
req, err := http.NewRequest("POST", server.Server+"/api/v1/new", strings.NewReader(data.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
if server.AuthUser != "" && server.AuthPass != "" {
req.SetBasicAuth(server.AuthUser, server.AuthPass)
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", err
return "", 0, 0, err
}
defer resp.Body.Close()
// Check HTTP response code
err = checkStatus(resp)
if err != nil {
return "", err
return "", 0, 0, err
}
// Parse answer
var paste newPasteAnswer
err = json.NewDecoder(resp.Body).Decode(&paste)
if err != nil {
return paste.ID, err
return paste.ID, paste.CreateTime, paste.DeleteTime, err
}
return paste.ID, nil
return paste.ID, paste.CreateTime, paste.DeleteTime, nil
}

Loading…
Cancel
Save