prclient

package
v0.0.0-...-2b37877 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2020 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Token implements access to prToken microservice which stores information about token to access 3rd party sites.

HTTP verbs are translated into the following function calls:

Verbs to Functions ------ --------- POST Create GET Get GET/ List resources PUT Replace DELETE Delete PATCH Edit

user ID mapper translates 3rd party sides credentials into internal user id.

HTTP verbs are translated into the following function calls:

Verbs to keys Functions ---------- -------------------------- --------- POST Create GET /credential Get GET /prUserIdMappersLIST List resources PUT /credential Replace DELETE /credential Delete PATCH /credential Edit

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool

func Bool(v bool) *bool

Bool is a helper routine that allocates a new bool value to store v and returns a pointer to it.

func CheckResponse

func CheckResponse(r *http.Response) error

CheckResponse checks the API response for errors, and returns them if present. A response is considered an error if it has a status code outside the 200 range or equal to 202 Accepted. API error responses are expected to have either no response body, or a JSON response body that maps to ErrorResponse. Any other response body will be silently ignored.

*AcceptedError for 202 Accepted status codes, and *TwoFactorAuthError for two-factor authentication errors.

func Int

func Int(v int) *int

Int is a helper routine that allocates a new int value to store v and returns a pointer to it.

func Int64

func Int64(v int64) *int64

Int64 is a helper routine that allocates a new int64 value to store v and returns a pointer to it.

func String

func String(v string) *string

String is a helper routine that allocates a new string value to store v and returns a pointer to it.

func Stringify

func Stringify(message interface{}) string

Stringify attempts to create a reasonable string representation of types. It does things like resolve pointers to their values and omits struct fields with nil values.

Types

type AcceptedError

type AcceptedError struct {
	// Raw contains the response body.
	Raw []byte
}

AcceptedError occurs when PavedRoad returns 202 Accepted response with an empty body, which means a job was scheduled on the PavedRoad side to process the information needed and cache it. Technically, 202 Accepted is not a real error, it's just used to indicate that results are not ready yet, but should be available soon. The request can be repeated after some time.

func (*AcceptedError) Error

func (*AcceptedError) Error() string

type BasicAuthTransport

type BasicAuthTransport struct {
	Username string // PavedRoad username
	Password string // PavedRoad password
	OTP      string // one-time password for users with two-factor auth enabled

	// Transport is the underlying HTTP transport to use when making requests.
	// It will default to http.DefaultTransport if nil.
	Transport http.RoundTripper
}

BasicAuthTransport is an http.RoundTripper that authenticates all requests using HTTP Basic Authentication with the provided username and password. It additionally supports users who have two-factor authentication enabled.

func (*BasicAuthTransport) Client

func (t *BasicAuthTransport) Client() *http.Client

Client returns an *http.Client that makes requests that are authenticated using HTTP Basic Authentication.

func (*BasicAuthTransport) RoundTrip

func (t *BasicAuthTransport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements the RoundTripper interface.

type Client

type Client struct {

	// Base URL for API requests. Defaults to the public PavedRoad API, but can be
	// set to a domain endpoint to use with PavedRoad Enterprise. BaseURL should
	// always be specified with a trailing slash.
	BaseURL *url.URL

	// Base URL for uploading files.
	UploadURL *url.URL

	// User agent used when communicating with the PavedRoad API.
	UserAgent string

	// Services used for talking to different parts of the PavedRoad API.
	Token        *TokensService
	UserIdMapper *UserIdMappersService
	// contains filtered or unexported fields
}

A Client manages communication with the PavedRoad API.

func NewClient

func NewClient(httpClient *http.Client) *Client

NewClient returns a new PavedRoad API client. If a nil httpClient is provided, a new http.Client will be used. To use API methods which require authentication, provide an http.Client that will perform the authentication for you (such as that provided by the golang.org/x/oauth2 library).

func (*Client) Do

func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*Response, error)

Do sends an API request and returns the API response. The API response is JSON decoded and stored in the value pointed to by v, or returned as an error if an API error has occurred. If v implements the io.Writer interface, the raw response body will be written to v, without attempting to first decode it.

The provided ctx must be non-nil. If it is canceled or times out, ctx.Err() will be returned.

func (*Client) NewRequest

func (c *Client) NewRequest(method, urlStr string, body interface{}) (*http.Request, error)

NewRequest creates an API request. A relative URL can be provided in urlStr, in which case it is resolved relative to the BaseURL of the Client. Relative URLs should always be specified without a preceding slash. If specified, the value pointed to by body is JSON encoded and included as the request body.

func (*Client) NewUploadRequest

func (c *Client) NewUploadRequest(urlStr string, reader io.Reader, size int64, mediaType string) (*http.Request, error)

NewUploadRequest creates an upload request. A relative URL can be provided in urlStr, in which case it is resolved relative to the UploadURL of the Client. Relative URLs should always be specified without a preceding slash.

type Error

type Error struct {
	Resource string `json:"resource"` // resource on which the error occurred
	Field    string `json:"field"`    // field on which the error occurred
	Code     string `json:"code"`     // validation error code
	Message  string `json:"message"`  // Message describing the error. Errors with Code == "custom" will always have this set.
}

An Error reports more details on an individual error in an ErrorResponse. These are the possible validation error codes:

missing:
    resource does not exist
missing_field:
    a required field on a resource has not been set
invalid:
    the formatting of a field is invalid
already_exists:
    another resource has the same valid as this field
custom:
    some resources return this, additional information is
    set in the Message field of the Error

PavedRoad API docs: https://developer.pavedroad.io/v1/#client-errors

func (*Error) Error

func (e *Error) Error() string

type ErrorResponse

type ErrorResponse struct {
	Response *http.Response // HTTP response that caused this error
	Message  string         `json:"message"` // error message
	Errors   []Error        `json:"errors"`  // more detail on individual errors
	// Block is only populated on certain types of errors such as code 451.
	Block *struct {
		Reason    string     `json:"reason,omitempty"`
		CreatedAt *Timestamp `json:"created_at,omitempty"`
	} `json:"block,omitempty"`
	// Most errors will also include a documentation_url field pointing
	// to some content that might help you resolve the error, see
	// TODO:(tbd)  https://developer.pavedroad.io/v3/#client-errors
	DocumentationURL string `json:"documentation_url,omitempty"`
}

An ErrorResponse reports one or more errors caused by an API request.

TODO:(tbd)PavedRoad API docs: https://developer.pavedroad.io/v3/#client-errors

func (*ErrorResponse) Error

func (r *ErrorResponse) Error() string

type ListOptions

type ListOptions struct {
	// For paginated result sets, page of results to retrieve.
	Page int `url:"page,omitempty"`

	// For paginated result sets, the number of results to include per page.
	PerPage int `url:"per_page,omitempty"`
}

ListOptions specifies the optional parameters to various List methods that support pagination.

type Metadata

type Metadata struct {
	Name      string   `json:"name"`
	Namespace string   `json:"namespace"`
	UID       string   `json:"uid"`
	Site      string   `json:"site"`
	EndPoint  string   `json:"endPoint"`
	Token     string   `json:"token"`
	Scope     []string `json:"scope"`
}

Metadata stored for a token

type RawOptions

type RawOptions struct {
	Type RawType
}

RawOptions specifies parameters when user wants to get raw format of a response instead of JSON.

type RawType

type RawType uint8

RawType represents type of raw format of a request instead of JSON.

const (
	// Diff format.
	Diff RawType = 1 + iota
	// Patch format.
	Patch
)

type Response

type Response struct {
	*http.Response

	NextPage  int
	PrevPage  int
	FirstPage int
	LastPage  int
}

Response is a PavedRoad API response. This wraps the standard http.Response returned from PavedRoad and provides convenient access to things like pagination links.

type Timestamp

type Timestamp struct {
	time.Time
}

Timestamp represents a time that can be unmarshalled from a JSON string formatted as either an RFC3339 or Unix timestamp.

func (Timestamp) Equal

func (t Timestamp) Equal(u Timestamp) bool

Equal reports whether t and u are equal based on time.Equal

func (Timestamp) String

func (t Timestamp) String() string

func (*Timestamp) UnmarshalJSON

func (t *Timestamp) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON implements the json.Unmarshaler interface. Time is expected in RFC3339 or Unix format.

type Token

type Token struct {
	APIVersion string   `json:"apiVersion"`
	Kind       string   `json:"kind"`
	Metadata   Metadata `json:"metadata"`
	Created    string   `json:"created,ignoreempty"`
	Updated    string   `json:"updated"`
	Active     bool     `json:"active"`
}

Token data structure for token storage

func (Token) String

func (u Token) String() string

type TokenListOptions

type TokenListOptions struct {
	// ID of the last token seen
	Since int64 `url:"since,omitempty"`

	// Note: Pagination is powered exclusively by the Since parameter,
	// ListOptions.Page has no effect.
	// ListOptions.PerPage controls an undocumented PavedRoad API parameter.
	ListOptions
}

TokenListOptions specifies optional parameters to the TokensService.ListAll method.

type TokensService

type TokensService service

TokensService handles communication with the token related methods of the PavedRoad API.

func (*TokensService) Create

func (s *TokensService) Create(ctx context.Context, newToken Token) (*Token, *Response, error)

Create a token PavedRoad API endpoint /prTokens/

func (*TokensService) Delete

func (s *TokensService) Delete(ctx context.Context, uuid string) (*Response, error)

Delete a token using a UUID. PavedRoad API endpoint /prTokens/uuid.

func (*TokensService) Edit

func (s *TokensService) Edit(ctx context.Context, token *Token, uuid string) (*Token, *Response, error)

Edit a token. PavedRoad API docs: https://developer.pavedroad.io/v1/token/#update-token

func (*TokensService) Get

func (s *TokensService) Get(ctx context.Context, uuid string) (*Token, *Response, error)

Get fetches a token using based on a UUID. PavedRoad API endpoint /prTokens/uuid.

func (*TokensService) List

ListAll lists all PavedRoad token.

func (*TokensService) Replace

func (s *TokensService) Replace(ctx context.Context, token *Token, uuid string) (*Token, *Response, error)

Replace a token. PavedRoad API docs: https://developer.pavedroad.io/v1/token/#replace-token

type UploadOptions

type UploadOptions struct {
	Name      string `url:"name,omitempty"`
	Label     string `url:"label,omitempty"`
	MediaType string `url:"-"`
}

UploadOptions specifies the parameters to methods that support uploads.

type UserIdMapper

type UserIdMapper struct {
	APIVersion string `json:"apiVersion"`
	ObjVersion string `json:"objVersion"`
	Kind       string `json:"kind"`
	Credential string `json:"login"`
	UserUUID   string `json:"userUUID"`
	LoginCount int    `json:"loginCount"`
	Created    string `json:"created,ignoreempty"`
	Updated    string `json:"updated,ignoreempty"`
	Active     string `json:"active"`
}

prUserIdMapper data structure for token storage

func (UserIdMapper) String

func (u UserIdMapper) String() string

type UserIdMapperListOptions

type UserIdMapperListOptions struct {
	// ID of the last token seen
	Since int64 `url:"since,omitempty"`

	// Note: Pagination is powered exclusively by the Since parameter,
	// ListOptions.Page has no effect.
	// ListOptions.PerPage controls an undocumented PavedRoad API parameter.
	ListOptions
}

UserIdMapperListOptions specifies optional parameters to the UserIdMappersService.ListAll method.

type UserIdMappersService

type UserIdMappersService service

UserIdMappersService handles communication with the token related methods of the PavedRoad API.

func (*UserIdMappersService) Create

func (s *UserIdMappersService) Create(ctx context.Context, newUserIdMapper UserIdMapper) (*UserIdMapper, *Response, error)

Create a token PavedRoad API endpoint /prUserIdMappers/

func (*UserIdMappersService) Delete

func (s *UserIdMappersService) Delete(ctx context.Context, cred string) (*Response, error)

Delete a token using a Ucredential. PavedRoad API endpoint /prUserIdMappers/cred.

func (*UserIdMappersService) Edit

Edit a token. PavedRoad API docs: https://developer.pavedroad.io/v1/token/#update-token

func (*UserIdMappersService) Get

Get fetches a token using based on a Ucredential. PavedRoad API endpoint /prUserIdMappers/credential.

func (*UserIdMappersService) List

ListAll lists all PavedRoad token.

func (*UserIdMappersService) Replace

func (s *UserIdMappersService) Replace(ctx context.Context, token *UserIdMapper, cred string) (*UserIdMapper, *Response, error)

Replace a token. PavedRoad API docs: https://developer.pavedroad.io/v1/token/#replace-token

Jump to

Keyboard shortcuts

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