pasteapi

package module
Version: v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 1, 2022 License: MIT Imports: 6 Imported by: 0

README

pasteapi is a Go library for working with the Lenpaste API.

Install

Add to go.mod file:

require git.lcomrade.su/root/pasteapi.go v1

Example

package main

import (
	"fmt"
	"git.lcomrade.su/root/pasteapi.go"
)

func main() {
	pasteSrv := pasteapi.NewServer("https://paste.lcomrade.su/")
	//pasteSrv.Auth("user", "pass")

	// Getting server information
	srvInfo, err := pasteSrv.GetServerInfo()
	if err != nil {
		panic(err)
	}
	fmt.Println("## Server Info ##")
	fmt.Println("URL:", pasteSrv.Server)
	fmt.Println("Version:", srvInfo.Version)
	fmt.Println("Maximum paste title length:", srvInfo.TitleMaxLen)
	fmt.Println("Maximum paste body length:", srvInfo.BodyMaxLen)
	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{
		Title: "Test paste",
		Body: `package main

func main() {
	println("Hello world!")
}
`,
		LineEnd:    pasteapi.LineEndUnix, // LF (\n) line end
		Syntax:     "Go",
		OneUse:     false,
		Expiration: 60 * 10, // 10 minutes
		Author: "Anon",
		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
	fmt.Println("Obtaining the newly created paste...")
	pasteGet, err := pasteSrv.Get(pasteID, false)
	if err != nil {
		panic(err)
	}
	fmt.Println("ID:", pasteGet.ID)
	fmt.Println("Title:", pasteGet.Title)
	fmt.Println("Body:", pasteGet.Body)
	fmt.Println("Create time:", pasteGet.CreateTime)
	fmt.Println("Delete time:", pasteGet.DeleteTime)
	fmt.Println("One use:", pasteGet.OneUse)
	fmt.Println("Syntax:", pasteGet.Syntax)
	fmt.Println("Author:", pasteGet.Author)
	fmt.Println("Author email:", pasteGet.AuthorEmail)
	fmt.Println("Author URL:", pasteGet.AuthorURL)
}

Documentation

Read documentation online on pkg.go.dev. Or use go doc -all git.lcomrade.su/root/pasteapi.go to read offline.

License

This library is distributed under the MIT license.

Documentation

Index

Constants

View Source
const (
	LineEndUnix   = "LF"
	LineEndDos    = "CRLF"
	LineEndOldMac = "CR"
)

Variables

View Source
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.
)

Functions

This section is empty.

Types

type NewPaste

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

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 Server

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
}

func NewServer added in v1.1.0

func NewServer(address string) Server

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 (*Server) Auth added in v1.2.0

func (server *Server) Auth(user string, pass string)

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) Get

func (server Server) Get(id string, openOneUse bool) (Paste, error)

Get gets the paste from the server by its ID.

1. If the "openOneUse" parameter is set to "true" and the paste is intended for a single use, the function will return the entire paste, and the paste will be deleted from the server.

2. If the "openOneUse" parameter is set to "false" and the paste is intended for a single use, the function will return only the "ID" and "OneUse" fields and the paste will NOT be deleted from the server.

3. If the paste is not destroyed after reading the "openOneUse" parameter does not affect anything.

func (Server) GetServerInfo

func (server Server) GetServerInfo() (ServerInfo, error)

GetServerInfo returns information about the server in use (including header and body length limits and maximum possible lifetime).

func (Server) New

func (server Server) New(newPaste NewPaste) (string, int64, int64, error)

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.

type ServerInfo

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL