client

package
v0.0.0-...-95e2c8e Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2020 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoConfiguration = errors.New("no configuration directory created yet")
	ErrNoConfDir       = errors.New("could not find a directory to write credentials to")
	ErrNoCredentials   = errors.New("could not find credentials, run configure")
	ErrNoEndpoint      = errors.New("no endpoint specified in credentials, run configure")
	ErrLoggedIn        = errors.New("already logged in, logout before logging in again")
	ErrNotLoggedIn     = errors.New("not logged in, run the login command first")
	ErrNotRefreshable  = errors.New("cannot refresh tokens, please login again")
)

Errors for standardized error handling

Functions

func Configuration

func Configuration() (string, error)

Configuration returns the directory of the configuration file(s).

func Confirm

func Confirm(prompt string, retries int, caseSensitive bool) bool

Confirm the users intention with a Y/n prompt. Can optionally supply a number of retries to get confirmation depending on how strict the confirmation parsing is. True is returned if the user specifies yes, false if no.

func Prompt

func Prompt(prompt, defaultResponse string) string

Prompt the user for input on the command line and optionally supply a default response that is returned if the user simply hits enter without entering anything. Note that the prompt will be formatted with a ": " at the end of the line.

func PromptPassword

func PromptPassword(prompt string, confirm, allowEmpty bool) (string, error)

PromptPassword reads the password from the user with or without confirmation. Text input by the user is masked for password entry.

func StatusError

func StatusError(status int, text string) error

StatusError creates an error for the status and the error text in the reply or uses the normal http status text for the error message if needed.

Types

type Client

type Client struct {
	http.Client
	// contains filtered or unexported fields
}

Client interacts with the todos API server.

func New

func New() (c *Client, err error)

New creates a new todos API client and prepares the credentials and configuration. TODO: handle TLS transport.

func (*Client) CheckLogin

func (c *Client) CheckLogin() (err error)

CheckLogin ensures that the user is ready to make an authenticated request by verifying that a non-expired access token exists. If the access token is expired but the refresh token is not, it refreshes the token automatically. Otherwise, it runs the login command to get an access token (which may prompt the user for a password).

func (*Client) CreateChecklist

func (c *Client) CreateChecklist(in *todos.Checklist) (out *todos.CreateChecklistResponse, err error)

CreateChecklist posts the checklist to the server in order to create it. This function checks the response for errors, but does not otherwise modify the output response. User authentication is required.

func (*Client) CreateTask

func (c *Client) CreateTask(in *todos.Task) (out *todos.CreateTaskResponse, err error)

CreateTask posts the task to the server in order to create it. This function checks the response for errors, but does not otherwise modify the output response. User authentication is required.

func (*Client) DeleteChecklist

func (c *Client) DeleteChecklist(id uint) (out *todos.DeleteChecklistResponse, err error)

DeleteChecklist sends a delete request for the specified id. This function checks the response for errors, but does not otherwise modify the output response. User authentication is required.

func (*Client) DeleteTask

func (c *Client) DeleteTask(id uint) (out *todos.DeleteTaskResponse, err error)

DeleteTask sends a delete request for the specified id. This function checks the response for errors, but does not otherwise modify the output response. User authentication is required.

func (*Client) DetailChecklist

func (c *Client) DetailChecklist(id uint) (out *todos.DetailChecklistResponse, err error)

DetailChecklist returns as much information as possible about the specified checklist. This function checks the response for errors, but does not otherwise modify the output response. User authentication is required.

func (*Client) DetailTask

func (c *Client) DetailTask(id uint) (out *todos.DetailTaskResponse, err error)

DetailTask returns as much information as possible about the specified task. This function checks the response for errors, but does not otherwise modify the output response. User authentication is required.

func (*Client) Do

func (c *Client) Do(req *http.Request, data interface{}) (status int, err error)

Do the http request and parse the JSON response returning the data and code.

func (*Client) ListChecklists

func (c *Client) ListChecklists(in *todos.ListChecklistsRequest) (out *todos.ListChecklistsResponse, err error)

ListChecklists returns all checklists for the authenticated user, sorted and filtered by the input request. This function checks the response for errors but does not otherwise modify the output response. User authentication is required.

func (*Client) ListTasks

func (c *Client) ListTasks(in *todos.ListTasksRequest) (out *todos.ListTasksResponse, err error)

ListTasks returns all tasks for the authenticated user, sorted and filtered by the input request. This function checks the response for errors but does not otherwise modify the output response. User authentication is required.

func (*Client) Login

func (c *Client) Login() (err error)

Login to the todos API, saving the access tokens to disk for use during other sessions. If the password is in the credentials, login executes directly, otherwise it prompts the user for the password. This is not a standard API client request, e.g. it does not take a LoginRequest and return a LoginResponse. Instead this method entirely manages the login process on behalf of the user.

func (*Client) Logout

func (c *Client) Logout(revokeAll bool) (err error)

Logout issues a logout request to the server then clears cached tokens locally. If revokeAll is true, then the server will remove all outstanding tokens, not just the token posted by the current client. If the logout succeeds, then the cached tokens are revoked, but they are not deleted if the request fails.

func (*Client) NewRequest

func (c *Client) NewRequest(method, url string, auth bool, data interface{}) (req *http.Request, err error)

NewRequest creates an http request to the endpoint specified in the credentials and sets the appropriate headers for the request, including authentication if required.

func (*Client) Overview

func (c *Client) Overview() (rep *todos.OverviewResponse, err error)

Overview returns the user's current todo listing and m ust be authenticated.

func (*Client) Refresh

func (c *Client) Refresh() (err error)

Refresh uses the refresh token to get a new access token without having to login.

func (*Client) Status

func (c *Client) Status() (rep *todos.StatusResponse, err error)

Status returns the current status of the todo API server.

func (*Client) UpdateChecklist

func (c *Client) UpdateChecklist(id uint, list *todos.Checklist) (out *todos.UpdateChecklistResponse, err error)

UpdateChecklist puts the checklist info to the specified id in order to update it. This function checks the response for errors, but does not otherwise modify the output response. User authentication is required.

func (*Client) UpdateTask

func (c *Client) UpdateTask(id uint, task *todos.Task) (out *todos.UpdateTaskResponse, err error)

UpdateTask puts the task info to the specified id in order to update it. This function checks the response for errors, but does not otherwise modify the output response. User authentication is required.

type Credentials

type Credentials struct {
	Version  string `yaml:"version"`            // api version to prepend to all path requests
	Endpoint string `yaml:"endpoint"`           // the endpoint to connect to
	Username string `yaml:"username,omitempty"` // username to login with (optional)
	Password string `yaml:"password,omitempty"` // password to login with (optional)
	Tokens   struct {
		Access    string    `yaml:"access"`     // access token to send with Bearer requests
		Refresh   string    `yaml:"refresh"`    // refresh token to obtain a new access token without login
		IssuedAt  time.Time `yaml:"issued_at"`  // timestamp of the login
		ExpiresAt time.Time `yaml:"expires_at"` // when the access token expires
		NotBefore time.Time `yaml:"not_before"` // earliest timestamp the access token can be refreshed
		RefreshBy time.Time `yaml:"refresh_by"` // when the refresh token expires
	} `yaml:"tokens,omitempty"` // access and refresh tokens for requests
	// contains filtered or unexported fields
}

Credentials stores login and configuration information to connect to the todos API. The only required item is the endpoint to make requests to. If the username or password are not provided in the credentials, then they will be prompted for during login. The access token is used in the Bearer header to make request. After the NotBefore timestamp, the access token is automatically refreshed until the refresh token expires. If the password is stored, then automatic login occurs in this case. Note that the local client can only maintain one set of credentials at a time.

func (*Credentials) Dump

func (c *Credentials) Dump() (err error)

Dump the credentials to an OS specific configuration folder.

func (*Credentials) GetURL

func (c *Credentials) GetURL(path string) (_ string, err error)

GetURL constructs a complete URL to the specified location from the base endpoint

func (*Credentials) IsLoggedIn

func (c *Credentials) IsLoggedIn() bool

IsLoggedIn returns true if the credentials hold an access token that is still valid, e.g. it has not expired yet. This function does not modify the credentials file.

func (*Credentials) IsRefreshable

func (c *Credentials) IsRefreshable() bool

IsRefreshable returns true if the credentials hold a refresh token that is still valid and a refresh request can be issued at the current time.

func (*Credentials) Load

func (c *Credentials) Load() (err error)

Load the credentials from an OS specific configuration folder.

func (*Credentials) MustGetURL

func (c *Credentials) MustGetURL(path string) string

MustGetURL panics if the url or path cannot be parsed

func (*Credentials) Revoke

func (c *Credentials) Revoke() (err error)

Revoke the tokens in the credentials file and overwrite the previous file.

func (*Credentials) SetTokens

func (c *Credentials) SetTokens(tokens *todos.LoginResponse) (err error)

SetTokens on the credentials and save the credentials to disk for future use.

Jump to

Keyboard shortcuts

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