Thanks to visit codestin.com
Credit goes to pkg.go.dev

etherpadlite

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2020 License: Apache-2.0 Imports: 5 Imported by: 2

README

etherpadlite-golang

An interface for Etherpad-Lite's HTTP API for Go.

Version 1.1

Version 1.1 was released on September 2019. Two things were changed, one that might affect currently running code.

First, a new option to return all ethperpad errors as Go errors was introduced (see below). Second a typo was removed from the code. The constant WrongParameters had a missing e, so I fixed the spelling. Please rename this constant!

Installation

As of April 19 2020 go modules are supported. I've added version 1.1.0 as the first version with go mod support. Thus go get in the "old-fashioned way" mentioned above should not be used anymore.

Run go get github.com/FabianWe/etherpadlite-golang. Read the code documentation on GoDoc.

Note that you need Go >= 1.7 to use this package because the project uses the new context package that was formerly golang.org/x/net/context.

Supported API Versions

Though I haven't tested each and every function I'm very confident that all versions including version 1.2.13 are supported. Feedback is very welcome!

Usage

Here's a very simple example that should give you the idea. It creates a new pad called foo with some initial content.

import (
	"fmt"
	"log"

	etherpadlite "github.com/FabianWe/etherpadlite-golang"
)

func main() {
	pad := etherpadlite.NewEtherpadLite("your-api-key")
	response, err := pad.CreatePad(context.Background(), "foo", "Lorem ipsum dolor sit amet.")
	if err != nil {
		log.Fatal(err)
	}
	// note that an API error is still possible, see below
	fmt.Println(response.Code, response.Message, response.Data)
}

Here is an example that uses a context to cancel the request if five seconds have passed:

import (
	"context"
	"fmt"
	"log"
	"time"

	etherpadlite "github.com/FabianWe/etherpadlite-golang"
)

func main() {
	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()
	pad := etherpadlite.NewEtherpadLite("your-api-key")
	response, err := pad.CreatePad(ctx, "foo", "Lorem ipsum dolor sit amet.")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(response.Code, response.Message, response.Data)
}

All methods return two values: A Response containing the parsed JSON response and an error. If err != nil something went really wrong, for example the connection to the host failed or the context was cancelled while doing the request. It is still possible that something went wrong inside of etherpad. The response has a field of type ReturnCode. If this code is != EverythingOk (constant in the package) something went wrong inside of the etherpad client. So after checking for the error returned by the API function you can check for errors in the return code. So use something like

if response.Code != etherpadlite.EverythingOk {
	fmt.Println("Something went wrong...")
}

As of version 1.1 (September 2019) it's also possible to return all etherpad API errors directly instead of doing the check above. Just set RaiseEtherpadErrors = true on your EtherpadLite instance:

pad.RaiseEtherpadErrors = true

In this case all responses with error code != EverythingOk will be returned as an error of type EtherpadError.

You can configure the EtherpadLite element, for example configure the http.Client.

An EtherpadLite instance has the following fields:

  • APIVersion: The HTTP API version. Defaults to 1.2.13. Note that this is a rather new version, if you have an older version of etherpad-lite you may have to adjust this!
  • BaseParams: A map that contains the parameters that are sent in every request. The API key gets added in NewEtherpadLite.
  • BaseURL: The URL pointing to the API of your pad, i.e. http://pad.domain/api. Defaults to http://localhost:9001/api in NewEtherpadLite.
  • Client: The http.Client used to send the GET requests.
  • RaiseEtherpadErrors: If set to true all errors from the etherpad API (return code != EverythingOk) will be returned as a Go error of type EtherpadError instead of being 'hidden' in the response.

All functions take as first argument a context.Context. If you pass ctx != nil the methods will get cancelled when ctx gets cancelled (i.e. return no Response and an error != nil). If you don't want to use a context at all simply set it to nil all the time. This is however not the optimal way of ignoring the context, according to the documentation you should always use a non-nil context, so better set it to context.Background or context.TODO.

If a method has an optional field, for example text in CreatePad, set the value to etherpadlite.OptionalParam if you don't want to use it. So to create a pad without text do:

response, err := pad.CreatePad(ctx, "foo", etherpadlite.OptionalParam)

If a method has a default argument, such as copyPad(sourceID, destinationID[, force=false]) setting the parameter to OptionalParam will set the value to its default.

It is safe to call the API methods simultaneously from multiple goroutines.

License

Copyright 2017 - 2019 Fabian Wenzelmann [email protected]

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Documentation

Overview

Package etherpadlite provides an interface for Etherpad-Lite's HTTP API written entirely in Go. The API documentation can be found at https://github.com/ether/etherpad-lite/wiki/HTTP-API. To use it create an instance of etherpadlite.EtherpadLite and call the API methods on it, for example CreatePad(nil, padID, text). If a parameter is optional, like text is in createPad, simply set the value to etherpadlite.OptionalParam. If there is a parameter with a default value, like copyPad(sourceID, destinationID[, force=false]), setting the parameter to OptionalParam will set the value to the default value.

All methods return a Response and an error (!= nil if something went wrong). The first argument of all methods is always a Context ctx. If set to a non-nil context the method will return nil and an error != nil when the context gets cancelled. If you don't want to use any context stuff just set it to nil. This is however not the best practice to set the context, better set it to context.Background or context.TODO if you don't want to use the context.

It is safe to call the API methods simultaneously from multiple goroutines.

I didn't document the methods since they're documented very well on the etherpad homepage: https://etherpad.org/doc/v1.7.5/#index_http_api

Index

Constants

View Source
const (
	// CurrentVersion is the default version to use.
	CurrentVersion = "1.2.13"

	// OptionalParam is a constant used to identify an optional parameter we don't
	// want to use.
	OptionalParam optionalParamType = 0
)

Variables

This section is empty.

Functions

This section is empty.

Types

type EtherpadError

type EtherpadError struct {
	// contains filtered or unexported fields
}

EtherpadError is an error returned by all methods if EtherpadLite.RaiseEtherpadErrors is true. It reports any internal error returned by calling the HTTP API of etherpad, signaling that the ReturnCode is not EverythingOk.

func NewEtherpadError

func NewEtherpadError(code ReturnCode, message string) EtherpadError

NewEtherpadError returns a new EtherpadError. The code should be != EverythingOk and the message is the error message returned by the HTTP API.

func (EtherpadError) Error

func (e EtherpadError) Error() string

Error returns the error as a string.

type EtherpadLite

type EtherpadLite struct {
	// APIVersion is the api version to use. It defaults to "1.2.13" in
	// NewEtherpadLite.
	APIVersion string

	// BaseParams are the parameter that are required in each request, i.e.
	// will be sent in each request.
	// The entry BaseParams["apikey"] = YOUR-API-KEY
	// should always be present.
	// NewEtherpadLite will take care of this.
	BaseParams map[string]interface{}

	// BaseURL is the URL pointing to the API of your pad, i.e.
	// http://pad.domain/api
	// It defaults to http://localhost:9001/api in NewEtherpadLite.
	BaseURL string

	// Client is used to send the GET requests to the API.
	// Set the values as required.
	// It defaults to the http.DefaultClient in NewEtherpadLite.
	Client *http.Client

	// RaiseEtherpadErrors specifies if errors returned by etherpad should be
	// returned as errors in Go or should be handled by the caller by checking
	// the response code.
	// It defaults to False.
	// By setting it to true the calls to all functions will return an error
	// for all responses with Response.Code != EverythingOk.
	// In this case an instance of EtherpadError is raised.
	RaiseEtherpadErrors bool
}

EtherpadLite is a struct that is used to connect to the etherpadlite API.

func NewEtherpadLite

func NewEtherpadLite(apiKey string) *EtherpadLite

NewEtherpadLite creates a new EtherpadLite instance given the mandatory apiKey. Create a new instance with this method and then configure it if you must.

func (*EtherpadLite) AppendText

func (pad *EtherpadLite) AppendText(ctx context.Context, padID, text interface{}) (*Response, error)

func (*EtherpadLite) CheckToken

func (pad *EtherpadLite) CheckToken(ctx context.Context) (*Response, error)

func (*EtherpadLite) CopyPad

func (pad *EtherpadLite) CopyPad(ctx context.Context, sourceID, destinationID, force interface{}) (*Response, error)

func (*EtherpadLite) CreateAuthor

func (pad *EtherpadLite) CreateAuthor(ctx context.Context, name interface{}) (*Response, error)

func (*EtherpadLite) CreateAuthorIfNotExistsFor

func (pad *EtherpadLite) CreateAuthorIfNotExistsFor(ctx context.Context, authorMapper, name interface{}) (*Response, error)

func (*EtherpadLite) CreateDiffHTML

func (pad *EtherpadLite) CreateDiffHTML(ctx context.Context, padID, startRev, endRev interface{}) (*Response, error)

func (*EtherpadLite) CreateGroup

func (pad *EtherpadLite) CreateGroup(ctx context.Context) (*Response, error)

func (*EtherpadLite) CreateGroupIfNotExistsFor

func (pad *EtherpadLite) CreateGroupIfNotExistsFor(ctx context.Context, groupMapper interface{}) (*Response, error)

func (*EtherpadLite) CreateGroupPad

func (pad *EtherpadLite) CreateGroupPad(ctx context.Context, groupID, padName, text interface{}) (*Response, error)

func (*EtherpadLite) CreatePad

func (pad *EtherpadLite) CreatePad(ctx context.Context, padID, text interface{}) (*Response, error)

func (*EtherpadLite) CreateSession

func (pad *EtherpadLite) CreateSession(ctx context.Context, groupID, authorID, validUntil interface{}) (*Response, error)

func (*EtherpadLite) DeleteGroup

func (pad *EtherpadLite) DeleteGroup(ctx context.Context, groupID interface{}) (*Response, error)

func (*EtherpadLite) DeletePad

func (pad *EtherpadLite) DeletePad(ctx context.Context, padID interface{}) (*Response, error)

func (*EtherpadLite) DeleteSession

func (pad *EtherpadLite) DeleteSession(ctx context.Context, sessionID interface{}) (*Response, error)

func (*EtherpadLite) GetAttributePool

func (pad *EtherpadLite) GetAttributePool(ctx context.Context, padID interface{}) (*Response, error)

func (*EtherpadLite) GetAuthorName

func (pad *EtherpadLite) GetAuthorName(ctx context.Context, authorID interface{}) (*Response, error)

func (*EtherpadLite) GetChatHead

func (pad *EtherpadLite) GetChatHead(ctx context.Context, padID interface{}) (*Response, error)

func (*EtherpadLite) GetChatHistory

func (pad *EtherpadLite) GetChatHistory(ctx context.Context, padID, start, end interface{}) (*Response, error)

func (*EtherpadLite) GetHTML

func (pad *EtherpadLite) GetHTML(ctx context.Context, padID, rev interface{}) (*Response, error)

func (*EtherpadLite) GetLastEdited

func (pad *EtherpadLite) GetLastEdited(ctx context.Context, padID interface{}) (*Response, error)

func (*EtherpadLite) GetPadID

func (pad *EtherpadLite) GetPadID(ctx context.Context, readOnlyID interface{}) (*Response, error)

func (*EtherpadLite) GetPublicStatus

func (pad *EtherpadLite) GetPublicStatus(ctx context.Context, padID interface{}) (*Response, error)

func (*EtherpadLite) GetReadOnlyID

func (pad *EtherpadLite) GetReadOnlyID(ctx context.Context, padID interface{}) (*Response, error)

func (*EtherpadLite) GetRevisionChangeset

func (pad *EtherpadLite) GetRevisionChangeset(ctx context.Context, padID, rev interface{}) (*Response, error)

func (*EtherpadLite) GetRevisionsCount

func (pad *EtherpadLite) GetRevisionsCount(ctx context.Context, padID interface{}) (*Response, error)

func (*EtherpadLite) GetSavedRevisionsCount

func (pad *EtherpadLite) GetSavedRevisionsCount(ctx context.Context, padID interface{}) (*Response, error)

func (*EtherpadLite) GetSessionInfo

func (pad *EtherpadLite) GetSessionInfo(ctx context.Context, sessionID interface{}) (*Response, error)

func (*EtherpadLite) GetText

func (pad *EtherpadLite) GetText(ctx context.Context, padID, rev interface{}) (*Response, error)

func (*EtherpadLite) IsPasswordProtected

func (pad *EtherpadLite) IsPasswordProtected(ctx context.Context, padID interface{}) (*Response, error)

func (*EtherpadLite) ListAllGroups

func (pad *EtherpadLite) ListAllGroups(ctx context.Context) (*Response, error)

func (*EtherpadLite) ListAllPads

func (pad *EtherpadLite) ListAllPads(ctx context.Context) (*Response, error)

func (*EtherpadLite) ListAuthorsOfPad

func (pad *EtherpadLite) ListAuthorsOfPad(ctx context.Context, padID interface{}) (*Response, error)

func (*EtherpadLite) ListPads

func (pad *EtherpadLite) ListPads(ctx context.Context, groupID interface{}) (*Response, error)

func (*EtherpadLite) ListPadsOfAuthor

func (pad *EtherpadLite) ListPadsOfAuthor(ctx context.Context, authorID interface{}) (*Response, error)

func (*EtherpadLite) ListSavedRevisions

func (pad *EtherpadLite) ListSavedRevisions(ctx context.Context, padID interface{}) (*Response, error)

func (*EtherpadLite) ListSessionsOfAuthor

func (pad *EtherpadLite) ListSessionsOfAuthor(ctx context.Context, authorID interface{}) (*Response, error)

func (*EtherpadLite) ListSessionsOfGroup

func (pad *EtherpadLite) ListSessionsOfGroup(ctx context.Context, groupID interface{}) (*Response, error)

func (*EtherpadLite) MovePad

func (pad *EtherpadLite) MovePad(ctx context.Context, sourceID, destinationID, force interface{}) (*Response, error)

func (*EtherpadLite) PadUsers

func (pad *EtherpadLite) PadUsers(ctx context.Context, padID interface{}) (*Response, error)

func (*EtherpadLite) PadUsersCount

func (pad *EtherpadLite) PadUsersCount(ctx context.Context, padID interface{}) (*Response, error)

func (*EtherpadLite) RestoreRevision

func (pad *EtherpadLite) RestoreRevision(ctx context.Context, padId, rev interface{}) (*Response, error)

func (*EtherpadLite) SaveRevision

func (pad *EtherpadLite) SaveRevision(ctx context.Context, padID, rev interface{}) (*Response, error)

func (*EtherpadLite) SendClientsMessage

func (pad *EtherpadLite) SendClientsMessage(ctx context.Context, padID, msg interface{}) (*Response, error)

func (*EtherpadLite) SetHTML

func (pad *EtherpadLite) SetHTML(ctx context.Context, padID, html interface{}) (*Response, error)

func (*EtherpadLite) SetPassword

func (pad *EtherpadLite) SetPassword(ctx context.Context, padID, password interface{}) (*Response, error)

func (*EtherpadLite) SetPublicStatus

func (pad *EtherpadLite) SetPublicStatus(ctx context.Context, padID, publicStatus interface{}) (*Response, error)

func (*EtherpadLite) SetText

func (pad *EtherpadLite) SetText(ctx context.Context, padID, text interface{}) (*Response, error)

type Response

type Response struct {
	Code    ReturnCode
	Message string
	Data    map[string]interface{}
}

Response is the response from the etherpad API. See https://github.com/ether/etherpad-lite/wiki/HTTP-API

type ReturnCode

type ReturnCode int

ReturnCode is the code return by the etherpad API, see API documentation for more details.

const (
	EverythingOk ReturnCode = iota
	WrongParameters
	InternalError
	NoSuchFunction
	WrongAPIKey
)

func (ReturnCode) String

func (c ReturnCode) String() string

Jump to

Keyboard shortcuts

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