1
0
Fork 0
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pasteapi.go/client.go

137 lines
5.5 KiB

// Copyright 2022 Leonid Maslakov. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package pasteapi
import (
"encoding/json"
"errors"
"net/http"
"strconv"
"strings"
)
var (
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 (
LineEndUnix = "LF"
LineEndDos = "CRLF"
LineEndOldMac = "CR"
)
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 {
Code int `json:"code"`
Error string `json:"error"`
}
type NewPaste struct {
Title string // Paste title (may not be set).
Body string // Paste content.
LineEnd string // Paste line end (default: LF)
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).
AuthorEmail string // Author email (may not be set).
AuthorURL string // Author URL (may not be set).
}
type Paste struct {
ID string `json:"id"` // Unique paste ID.
Title string `json:"title"` // Paste title (can be blank).
Body string `json:"body"` // Paste content.
CreateTime int64 `json:"createTime"` // Paste creation time (UNIX time in seconds).
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).
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).
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.
// For example, if there are errors in the server address, NewServer will try to fix them.
func NewServer(address string) Server {
return Server{
Server: strings.TrimRight(address, "/"),
}
}
// 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
err := json.NewDecoder(resp.Body).Decode(&errInfo)
if err != nil {
return err
}
switch errInfo.Error {
case "Bad Request":
return ErrBadRequest
case "Unauthorized":
return ErrUnauthorized
case "Could not find ID":
return ErrNotFoundID
case "Not Found":
return ErrNotFound
case "Method Not Allowed":
return ErrMethodNotAllowed
case "Internal Server Error":
return ErrInternalServerError
default:
return errors.New("lenpaste: server return " + strconv.Itoa(resp.StatusCode) + " HTTP code")
}
}
return nil
}