web

package
v0.0.0-...-97b669a Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2019 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// PathSep is the character used to separate components of the ShareBase
	// path.
	PathSep = "\\"
)
View Source
const (
	// PhoenixTokenPrefix is the string prefixed to ShareBase's token value
	// within the Authorization header of every request.
	PhoenixTokenPrefix = "PHOENIX-TOKEN "
)

Variables

View Source
var (

	// ErrUnauthorized is returned when the request results in a 401
	// unauthorized response.
	ErrUnauthorized = errors.New("unauthorized")
)

Functions

func Concat

func Concat(values ...string) string

Concat appends strings together without a separator

Types

type AuthToken

type AuthToken struct {
	// Token is the special authenticated value returned after a successful
	// authentication request.
	Token string

	// UserName repeats the username authenticated.
	UserName string

	// UserID gets the unique integer identifier of this user in the ShareBase
	// environment.
	UserID int `json:"UserId"`

	// ExpirationDate holds the time stamp when the authentication request
	// expires.
	ExpirationDate time.Time
}

AuthToken is the structure returned when authenticating with a username and password. The Token field is passed to NewClient to make requests to the ShareBase API.

func AuthTokenForUsernameAndPassword

func AuthTokenForUsernameAndPassword(dataCenter, username, password string) (authToken AuthToken, err error)

AuthTokenForUsernameAndPassword gets a ShareBase AuthToken for the given username and password combination.

type Client

type Client struct {

	// DataCenter holds the URL that should prefix all non-absolute
	// requests
	DataCenter url.URL
	// contains filtered or unexported fields
}

Client defines the client struct used to communicate with the ShareBase web API.

func NewClient

func NewClient(dataCenter, token string) (*Client, error)

NewClient creates a new client from the given dataCenter URL string and API token.

func (*Client) Libraries

func (c *Client) Libraries() (libraries []Library, err error)

Libraries gets all of the libraries accessible from the current Client.

func (*Client) Library

func (c *Client) Library(id int) (library Library, err error)

Library gets a library with the given integer ID.

func (*Client) LibraryByName

func (c *Client) LibraryByName(name string) (library Library, err error)

LibraryByName attepts to retrieve a library by its name.

func (*Client) NumRequests

func (c *Client) NumRequests() uint64

NumRequests returns the total number of requests issued to the ShareBase API through this client. It's useful for benchmarking to determine how many queries are consumed in case ShareBase ever switches to a per-request payment model. This total includes both successful and failed requests.

type ClientPool

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

ClientPool holds a pool of ShareBase clients. Clients for multiple data centers with multiple authentication tokens can be stored in the same pool. ClientPools have a mutex to make accessing and caching clients safe but clients themselves cannot be used concurrently.

func NewClientPool

func NewClientPool() *ClientPool

NewClientPool creates a new pool of Clients.

func (*ClientPool) Cache

func (p *ClientPool) Cache(c *Client)

Cache the given client in the pool. It is not necessary for the client to have been created from the pool. It is critical that the client not be used after it has been returned to the client pool. If a client is in any broken state, it should not be returned to the pool; simply request a new client.

func (*ClientPool) Client

func (p *ClientPool) Client(dataCenter, token string) (*Client, error)

Client gets an existing cached client or creates one.

type Document

type Document struct {
	// DocumentID holds the unique ID of the document in ShareBase.
	DocumentID int `json:"DocumentId"`

	// DocumentName holds the name of the document in its parent.
	DocumentName string

	// DateModified stores the date that the Document was last modified.
	DateModified time.Time

	// Hash is a base-64 encoded SHA-1 hash of the file's contents.
	Hash []byte

	// Links holds links that the document has to other ShareBase objects.
	Links DocumentLinks
}

Document represents a single document in ShareBase.

func (*Document) Content

func (d *Document) Content(c *Client) (DocumentContent, error)

Content retrieves the document content. It must be closed after it is retrieved.

func (Document) String

func (d Document) String() string

String gets a string representation of the document.

type DocumentContent

type DocumentContent struct {
	*Document
	io.ReadCloser
	Length             int64
	ContentType        string
	ContentDisposition string
}

DocumentContent unsurprisingly holds the content of the document. It must be closed.

func (DocumentContent) Close

func (d DocumentContent) Close() error

Close wraps the io.ReadCloser's Close function but adds more context to the message.

func (DocumentContent) Len

func (d DocumentContent) Len() int64

Len gets the document content's length as an int64 (A normal int isn't large enough on a 32-bit platform for file sizes >2GiB).

type DocumentLinks struct {
	// Self holds a link back to the current object.
	Self string

	// Content holds a link to the document's content.
	Content string
}

DocumentLinks holds a set of links to other objects.

type DocumentWriter

type DocumentWriter struct {
	*Client
	*Folder
	NewLargeDocumentResponse
	// contains filtered or unexported fields
}

DocumentWriter is returned by a folder's DocumentWriter function. It implements the io.Writer interface and takes its written data and writes it to ShareBase. A DocumentWriter must be closed after using it!

func (*DocumentWriter) Close

func (w *DocumentWriter) Close() error

Close finalizes the document upload.

func (*DocumentWriter) Write

func (w *DocumentWriter) Write(p []byte) (n int, err error)

Write implements the io.Writer interface. It writes a chunk of a document to ShareBase with a PATCH.

type Folder

type Folder struct {
	// FolderID is the unique ID of the folder in ShareBase.
	FolderID int `json:"FolderId"`

	// FolderName holds the name of the folder within its parent (i.e. not the
	// full path).
	FolderName string

	// LibraryID holds the ID of the library in which this folder resides.
	LibraryID int `json:"LibraryId"`

	// Links holds the folder's links to other objects.
	Links FolderLinks

	// Embedded holds nested objects that might be embedded in the folder.
	Embedded FolderEmbedded
}

Folder represents a folder in the ShareBase API.

func (*Folder) Document

func (f *Folder) Document(c *Client, id int) (Document, error)

Document attempts to retrieve a document from the folder by its ID.

func (*Folder) DocumentByName

func (f *Folder) DocumentByName(c *Client, name string) (Document, error)

DocumentByName attempts to retrieve a document from the folder by its document name.

func (*Folder) DocumentWriter

func (f *Folder) DocumentWriter(c *Client, name string) (w *DocumentWriter, err error)

DocumentWriter creates a new document writer with the given document name under the current folder. The DocumentWriter must be closed after writing!

func (*Folder) Documents

func (f *Folder) Documents(c *Client) (documents []Document, err error)

Documents gets the documents embedded in this folder.

func (*Folder) Folder

func (f *Folder) Folder(c *Client, id int) (folder Folder, err error)

Folder gets a single folder within the current folder by its ID.

func (*Folder) FolderByName

func (f *Folder) FolderByName(c *Client, name string) (Folder, error)

FolderByName gets a child folder from the current folder by its name.

func (*Folder) Folders

func (f *Folder) Folders(c *Client) (folders []Folder, err error)

Folders gets a slice of folders nested in this folder.

func (*Folder) NewDocument

func (f *Folder) NewDocument(c *Client, name string, content io.Reader) error

NewDocument creates a new ShareBase document in the given folder.

type FolderEmbedded

type FolderEmbedded struct {
	// Documents holds a slice of documents within the folder.
	Documents []Document

	// Folders holds a slice of folders within this folder.
	Folders []Folder
}

FolderEmbedded holds an embedded.

type FolderLinks struct {
	// Self is the link to the same folder.
	Self string

	// Folders holds a link to this folder's subfolders.
	Folders string

	// Documents holds a link to this folder's nested documents.
	Documents string

	// Shares holds a link to get this folder's links.
	Shares string
}

FolderLinks are the links specific to a folder.

type Kind

type Kind string

Kind is used when wrapping any ShareBase object to indicate what kind it is.

const (
	// LibraryKind is a ShareBase library
	LibraryKind Kind = "Library"

	// FolderKind is a ShareBase Folder.
	FolderKind Kind = "Folder"

	// DocumentKind is a ShareBaseDocument
	DocumentKind Kind = "Document"
)

func (Kind) String

func (k Kind) String() string

String implements the Stringer interface.

type Lener

type Lener interface {
	// Len gets the length of the type's storage in bytes.
	Len() int
}

Lener is implemented by types that have a Len method returning their length in bytes. It is used by the Folder.NewDocument function that picks the recommended upload type based on the file size.

type Library

type Library struct {
	// LibraryID is the library's unique ID in ShareBase.
	LibraryID int `json:"LibraryId"`

	// LibraryName is the library's name.
	LibraryName string

	// IsPrivate is true for personal libraries and false otherwise.
	IsPrivate bool

	// Links holds the library's links to other resources.
	Links LibraryLinks
}

Library is a library in ShareBase.

func (*Library) Folder

func (lib *Library) Folder(c *Client, id int) (folder Folder, err error)

Folder gets a folder by ID from the given library.

func (*Library) FolderByName

func (lib *Library) FolderByName(c *Client, name string) (Folder, error)

FolderByName gets a folder within the library by its name.

func (*Library) Folders

func (lib *Library) Folders(c *Client) (folders []Folder, err error)

Folders gets the collection of Folders under this Library.

func (*Library) NewFolder

func (lib *Library) NewFolder(c *Client, path ...string) (folder Folder, err error)

NewFolder creates a new folder within the library with the specified path.

type LibraryLinks struct {
	// Self is the library link.
	Self string

	// Folders is the URL that gets the folders within the library.
	Folders string
}

LibraryLinks holds the URLs that a Library's Links attribute has.

type NewDocumentRequest

type NewDocumentRequest struct {
	// DocumentName is the name of the document to be created in a folder.
	DocumentName string
}

NewDocumentRequest is marshaled when creating a new document.

type NewFolderRequest

type NewFolderRequest struct {
	// FolderPath holds the full path to the folder with the path components
	// separated by backslashes.
	FolderPath string
}

NewFolderRequest is used by the NewFolder function to create a new folder.

type NewLargeDocumentResponse

type NewLargeDocumentResponse struct {
	Links       NewLargeDocumentResponseLinks
	Identifier  uuid.UUID
	FileName    string
	CurrentSize uint64
	VolumeID    int `json:"VolumeId"`
}

NewLargeDocumentResponse is a JSON response returned when creating a large document in ShareBase.

type NewLargeDocumentResponseLinks struct {
	Location string
}

NewLargeDocumentResponseLinks is the embedded Links struct returned inside of the NewLargeDocumentResponse struct.

type NotFound

type NotFound struct {
	Kind
	ID   int
	Name string
}

NotFound is an error returned when a ShareBase object cannot be found.

func (NotFound) Error

func (err NotFound) Error() string

Error implements the error interface.

type Size

type Size int64

Size defines a file size in bytes

const (
	// B is the standard size unit of Bytes.
	B Size = 1 << (iota * 10)

	// K is kibibytes.
	K

	// M is mebibytes.
	M

	// G is gibibytes.
	G
)
const (
	// SmallFileCutoff is the file size limit under which ShareBase's
	// recommended small file upload method is used and above which ShareBase's
	// large file upload method is used.
	SmallFileCutoff Size = 5 * M

	// PatchSize is the size of patches made to a large file upload.
	// ShareBase's recommendation is 512K and not to exceed 2M, so I'm going
	// to go with the power of 2 between them.
	PatchSize Size = 512 * K
)

Jump to

Keyboard shortcuts

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