core

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2019 License: Apache-2.0 Imports: 22 Imported by: 111

Documentation

Index

Constants

View Source
const (
	APIKEY                       = "apikey"
	ICP_PREFIX                   = "icp-"
	USER_AGENT                   = "User-Agent"
	AUTHORIZATION                = "Authorization"
	BEARER                       = "Bearer"
	IBM_CREDENTIAL_FILE_ENV      = "IBM_CREDENTIALS_FILE"
	DEFAULT_CREDENTIAL_FILE_NAME = "ibm-credentials.env"
	URL                          = "url"
	USERNAME                     = "username"
	PASSWORD                     = "password"
	IAM_APIKEY                   = "iam_apikey"
	IAM_URL                      = "iam_url"
	SDK_NAME                     = "ibm-go-sdk-core"
	UNKNOWN_ERROR                = "Unknown Error"
	ICP4D                        = "icp4d"
	IAM                          = "iam"
)

common constants for core

View Source
const (
	DEFAULT_IAM_URL             = "https://iam.cloud.ibm.com/identity/token"
	DEFAULT_IAM_CLIENT_ID       = "bx"
	DEFAULT_IAM_CLIENT_SECRET   = "bx"
	DEFAULT_CONTENT_TYPE        = "application/x-www-form-urlencoded"
	REQUEST_TOKEN_GRANT_TYPE    = "urn:ibm:params:oauth:grant-type:apikey"
	REQUEST_TOKEN_RESPONSE_TYPE = "cloud_iam"
)

constants for IAM token authentication

View Source
const (
	POST   = http.MethodPost
	GET    = http.MethodGet
	DELETE = http.MethodDelete
	PUT    = http.MethodPut
	PATCH  = http.MethodPatch
)

common HTTP methods

View Source
const (
	Accept                  = "Accept"
	APPLICATION_JSON        = "application/json"
	CONTENT_DISPOSITION     = "Content-Disposition"
	CONTENT_TYPE            = "Content-Type"
	FORM_URL_ENCODED_HEADER = "application/x-www-form-urlencoded"
)

common headers

View Source
const (
	PRE_AUTH_PATH = "/v1/preauth/validateAuth"
)

constants for ICP4D

Variables

Validate single instance of Validate, it caches struct info

Functions

func BoolPtr

func BoolPtr(literal bool) *bool

BoolPtr : return pointer to boolean literal

func Float32Ptr

func Float32Ptr(literal float32) *float32

Float32Ptr : return pointer to float32 literal

func Float64Ptr

func Float64Ptr(literal float64) *float64

Float64Ptr : return pointer to float64 literal

func GetCurrentTime added in v0.4.0

func GetCurrentTime() int64

GetCurrentTime :

func HasBadFirstOrLastChar

func HasBadFirstOrLastChar(str string) bool

HasBadFirstOrLastChar checks if the string starts with `{` or `"` or ends with `}` or `"`

func Int64Ptr

func Int64Ptr(literal int64) *int64

Int64Ptr : return pointer to int64 literal

func IsJSONMimeType

func IsJSONMimeType(mimeType string) bool

IsJSONMimeType : Returns true iff the specified mimeType value represents a "JSON" mimetype.

func IsJSONPatchMimeType

func IsJSONPatchMimeType(mimeType string) bool

IsJSONPatchMimeType : Returns true iff the specified mimeType value represents a "JSON Patch" mimetype.

func PrettyPrint added in v0.2.2

func PrettyPrint(result interface{}, resultName string)

PrettyPrint print pretty

func StringNilMapper

func StringNilMapper(s *string) string

StringNilMapper - de-references the parameter 's' and returns the result, or "" if 's' is nil

func StringPtr

func StringPtr(literal string) *string

StringPtr : return pointer to string literal

func SystemInfo

func SystemInfo() string

SystemInfo : returns the system information

func UserHomeDir

func UserHomeDir() string

UserHomeDir returns the user home directory

func ValidateNotNil

func ValidateNotNil(object interface{}, errorMsg string) error

ValidateNotNil - returns the specified error if 'object' is nil, nil otherwise

func ValidateStruct

func ValidateStruct(param interface{}, paramName string) error

ValidateStruct - validates 'param' (assumed to be a struct) according to the annotations attached to its fields

Types

type BaseService

type BaseService struct {
	Options           *ServiceOptions
	DefaultHeaders    http.Header
	IAMTokenManager   *IAMTokenManager
	ICP4DTokenManager *ICP4DTokenManager
	Client            *http.Client
	UserAgent         string
}

BaseService Base Service

func NewBaseService

func NewBaseService(options *ServiceOptions, serviceName, displayName string) (*BaseService, error)

NewBaseService Instantiate a Base Service

func (*BaseService) BuildUserAgent

func (service *BaseService) BuildUserAgent() string

BuildUserAgent : Builds the user agent string

func (*BaseService) DisableSSLVerification

func (service *BaseService) DisableSSLVerification()

DisableSSLVerification skips SSL verification

func (*BaseService) Request

func (service *BaseService) Request(req *http.Request, result interface{}) (*DetailedResponse, error)

Request performs the HTTP request

func (*BaseService) SetDefaultHeaders

func (service *BaseService) SetDefaultHeaders(headers http.Header)

SetDefaultHeaders sets HTTP headers to be sent in every request.

func (*BaseService) SetHTTPClient

func (service *BaseService) SetHTTPClient(client *http.Client)

SetHTTPClient updates the client handling the requests

func (*BaseService) SetIAMAPIKey

func (service *BaseService) SetIAMAPIKey(iamAPIKey string) error

SetIAMAPIKey Sets the IAM API key

func (*BaseService) SetIAMAccessToken

func (service *BaseService) SetIAMAccessToken(iamAccessToken string)

SetIAMAccessToken Sets the IAM access token

func (*BaseService) SetICP4DAccessToken added in v0.4.0

func (service *BaseService) SetICP4DAccessToken(icp4dAccessToken string)

SetICP4DAccessToken Sets the ICP4D access token

func (*BaseService) SetURL

func (service *BaseService) SetURL(url string) error

SetURL sets the service URL

func (*BaseService) SetUserAgent

func (service *BaseService) SetUserAgent(userAgentString string)

SetUserAgent : Sets the user agent value

func (*BaseService) SetUsernameAndPassword

func (service *BaseService) SetUsernameAndPassword(username string, password string) error

SetUsernameAndPassword Sets the Username and Password

type Credential

type Credential struct {
	URL      string `json:"url,omitempty"`
	Username string `json:"username,omitempty"`
	Password string `json:"password,omitempty"`
	APIKey   string `json:"apikey,omitempty"`
}

Credential : The service credential

func LoadFromVCAPServices

func LoadFromVCAPServices(serviceName string) *Credential

LoadFromVCAPServices : returns the credential of the service

type DetailedResponse

type DetailedResponse struct {
	StatusCode int         // HTTP status code
	Headers    http.Header // HTTP response headers
	Result     interface{} // response from service
}

DetailedResponse : Generic response for IBM API

func (*DetailedResponse) GetHeaders

func (response *DetailedResponse) GetHeaders() http.Header

GetHeaders returns the headers

func (*DetailedResponse) GetResult

func (response *DetailedResponse) GetResult() interface{}

GetResult returns the result from the service

func (*DetailedResponse) GetStatusCode

func (response *DetailedResponse) GetStatusCode() int

GetStatusCode returns the HTTP status code

func (*DetailedResponse) String

func (response *DetailedResponse) String() string

type Error added in v0.2.0

type Error struct {
	Message string `json:"message,omitempty"`
}

Error : specifies the error

type Errors added in v0.2.0

type Errors struct {
	Errors []Error `json:"errors,omitempty"`
}

Errors : a struct for errors array

type FormData

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

A FormData stores information for form data

type IAMTokenInfo added in v0.4.0

type IAMTokenInfo struct {
	AccessToken  string `json:"access_token"`
	RefreshToken string `json:"refresh_token"`
	TokenType    string `json:"token_type"`
	ExpiresIn    int64  `json:"expires_in"`
	Expiration   int64  `json:"expiration"`
}

IAMTokenInfo : Response struct from token request

type IAMTokenManager added in v0.4.0

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

IAMTokenManager : IAM token manager

func NewIAMTokenManager added in v0.4.0

func NewIAMTokenManager(iamAPIkey string, iamURL string, userAccessToken string,
	iamClientId string, iamClientSecret string) (*IAMTokenManager, error)

NewIAMTokenManager : Instantiate IAMTokenManager

func (*IAMTokenManager) GetToken added in v0.4.0

func (tm *IAMTokenManager) GetToken() (string, error)

GetToken : Return token set by user or fresh token The source of the token is determined by the following logic:

  1. If user provides their own managed access token, assume it is valid and send it
  2. a) If this class is managing tokens and does not yet have one, make a request for one b) If this class is managing tokens and the token has expired, request a new one
  3. If this class is managing tokens and has a valid token stored, send it

func (*IAMTokenManager) SetIAMAPIKey added in v0.4.0

func (tm *IAMTokenManager) SetIAMAPIKey(key string)

SetIAMAPIKey : Set API key so that SDK manages token

func (*IAMTokenManager) SetIAMAccessToken added in v0.4.0

func (tm *IAMTokenManager) SetIAMAccessToken(userAccessToken string)

SetIAMAccessToken : sets a self-managed access token. The access token should be valid and not yet expired.

type ICP4DTokenInfo added in v0.4.0

type ICP4DTokenInfo struct {
	Username    string   `json:"username,omitempty"`
	Role        string   `json:"role,omitempty"`
	Permissions []string `json:"permissions,omitempty"`
	Subject     string   `json:"sub,omitempty"`
	Issuer      string   `json:"iss,omitempty"`
	Audience    string   `json:"aud,omitempty"`
	UID         string   `json:"uid,omitempty"`
	MessageCode string   `json:"_messageCode_,omitempty"`
	Message     string   `json:"message,omitempty"`
	AccessToken string   `json:"accessToken,omitempty"`
}

ICP4DTokenInfo : Response struct from token request

type ICP4DTokenManager added in v0.4.0

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

ICP4DTokenManager : Manager for handling ICP4D authentication

func NewICP4DTokenManager added in v0.4.0

func NewICP4DTokenManager(icp4dURL, username, password, accessToken string) *ICP4DTokenManager

NewICP4DTokenManager : New instance of ICP4D token manager

func (*ICP4DTokenManager) DisableSSLVerification added in v0.4.0

func (tm *ICP4DTokenManager) DisableSSLVerification()

DisableSSLVerification skips SSL verification

func (*ICP4DTokenManager) GetToken added in v0.4.0

func (tm *ICP4DTokenManager) GetToken() (string, error)

GetToken : Return token set by user or fresh token The source of the token is determined by the following logic:

  1. If user provides their own managed access token, assume it is valid and send it
  2. a) If this class is managing tokens and does not yet have one, make a request for one b) If this class is managing tokens and the token has expired, request a new one
  3. If this class is managing tokens and has a valid token stored, send it

func (*ICP4DTokenManager) SetICP4DAccessToken added in v0.4.0

func (tm *ICP4DTokenManager) SetICP4DAccessToken(userAccessToken string)

SetICP4DAccessToken : sets a self-managed access token. The access token should be valid and not yet expired.

type RequestBuilder

type RequestBuilder struct {
	Method string
	URL    *url.URL
	Header http.Header
	Body   io.Reader
	Query  map[string]string
	Form   map[string]FormData
}

A RequestBuilder is an HTTP request to be sent to the service

func NewRequestBuilder

func NewRequestBuilder(method string) *RequestBuilder

NewRequestBuilder : Initiates a new request

func (*RequestBuilder) AddFormData

func (requestBuilder *RequestBuilder) AddFormData(fieldName string, fileName string, contentType string,
	contents interface{}) *RequestBuilder

AddFormData makes an entry for Form data

func (*RequestBuilder) AddHeader

func (requestBuilder *RequestBuilder) AddHeader(name string, value string) *RequestBuilder

AddHeader adds header name and value

func (*RequestBuilder) AddQuery

func (requestBuilder *RequestBuilder) AddQuery(name string, value string) *RequestBuilder

AddQuery adds Query name and value

func (*RequestBuilder) Build

func (requestBuilder *RequestBuilder) Build() (*http.Request, error)

Build the request

func (*RequestBuilder) ConstructHTTPURL

func (requestBuilder *RequestBuilder) ConstructHTTPURL(endPoint string, pathSegments []string, pathParameters []string) *RequestBuilder

ConstructHTTPURL creates a properly encoded URL with path parameters.

func (*RequestBuilder) SetBodyContent

func (requestBuilder *RequestBuilder) SetBodyContent(contentType string, jsonContent interface{}, jsonPatchContent interface{},
	nonJSONContent interface{}) (*RequestBuilder, error)

SetBodyContent - sets the body content from one of three different sources

func (*RequestBuilder) SetBodyContentForMultipart

func (requestBuilder *RequestBuilder) SetBodyContentForMultipart(contentType string, content interface{}, writer io.Writer) error

SetBodyContentForMultipart - sets the body content for a part in a multi-part form

func (*RequestBuilder) SetBodyContentJSON

func (requestBuilder *RequestBuilder) SetBodyContentJSON(bodyContent interface{}) (*RequestBuilder, error)

SetBodyContentJSON - set the body content from a JSON structure

func (*RequestBuilder) SetBodyContentStream

func (requestBuilder *RequestBuilder) SetBodyContentStream(bodyContent io.Reader) (*RequestBuilder, error)

SetBodyContentStream - set the body content from an io.Reader instance

func (*RequestBuilder) SetBodyContentString

func (requestBuilder *RequestBuilder) SetBodyContentString(bodyContent string) (*RequestBuilder, error)

SetBodyContentString - set the body content from a string

type Service

type Service struct {
	Credentials Credential `json:"credentials,omitempty"`
}

Service : The service

type ServiceOptions

type ServiceOptions struct {
	Version            string
	URL                string
	Username           string
	Password           string
	IAMApiKey          string
	IAMAccessToken     string
	IAMURL             string
	IAMClientId        string
	IAMClientSecret    string
	ICP4DAccessToken   string
	ICP4DURL           string
	AuthenticationType string
}

ServiceOptions Service options

Jump to

Keyboard shortcuts

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