rest

package
v1.4.5-alpha1 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2019 License: MPL-2.0 Imports: 16 Imported by: 2

Documentation

Overview

Package rest is a default implementation of a RESTful server. That package provides "InterfaceRestService" functionality declared in "github.com/ottemo/commerce/api" package.

Package stands on a top of "github.com/julienschmidt/httprouter" package.

RESTful server is a HTTP protocol accessible service you can use to interact with application. Refer to "http://[url-base]/" URL fo a list of API function application build providing. ("http://localhost:300" by default). The default interaction protocol for API functions is "application/json" (but not limited to, some API returns raw data, others "plain text", some of them supports couple content types).

Ottemo API calls are supplied with ApplicationContext abstraction (refer "InterfaceApplicationContext"), to transfer API call related "attributes" and "content". For REST server attributes are parameters specified in URL string whereas content is a HTTP Request content. Notice that URL arguments are both - REST resource required parameters and optional URL parameters.

Example:
  endpoint: /category/:categoryID/products
  api call: http://localhost/category/5488485b49c43d4283000067/products?action=count,sku=~10

So, the arguments provided to API handler function are:
  categoryID="5488485b49c43d4283000067", action="count", sku="~10" (with string values)

Session specification addressed to "OTTEMOSESSION=[sessionID]" COOKIE value. Each request with unspecified session will be supplied with new one session. SessionID will be returned in mentioned COOKIE value.

Index

Constants

View Source
const (
	ConstDebugLogStorage = "rest.log" // log storage for debug log records

	ConstErrorModule = "api/rest"
	ConstErrorLevel  = env.ConstErrorLevelService

	ConstConfigPathAPI           = "api"
	ConstConfigPathAPILog        = "api.log"
	ConstConfigPathAPILogEnable  = "api.log.enable"
	ConstConfigPathAPILogExclude = "api.log.exclude"
)

Package global constants

Variables

This section is empty.

Functions

This section is empty.

Types

type DefaultRestApplicationContext

type DefaultRestApplicationContext struct {
	ResponseWriter    http.ResponseWriter
	Request           *http.Request
	RequestParameters map[string]string
	RequestArguments  map[string]string
	RequestContent    interface{}
	RequestFiles      map[string]io.Reader

	Session       api.InterfaceSession
	ContextValues map[string]interface{}
	Result        interface{}
}

DefaultRestApplicationContext is a structure to hold API request related information

func (*DefaultRestApplicationContext) GetContextValue

func (it *DefaultRestApplicationContext) GetContextValue(key string) interface{}

GetContextValue returns particular context related value or nil if not set

func (*DefaultRestApplicationContext) GetContextValues

func (it *DefaultRestApplicationContext) GetContextValues() map[string]interface{}

GetContextValues returns current context related values map

func (*DefaultRestApplicationContext) GetRequest

func (it *DefaultRestApplicationContext) GetRequest() interface{}

GetRequest returns raw request object

func (*DefaultRestApplicationContext) GetRequestArgument

func (it *DefaultRestApplicationContext) GetRequestArgument(name string) string

GetRequestArgument returns particular argument provided to API function or ""

func (*DefaultRestApplicationContext) GetRequestArguments

func (it *DefaultRestApplicationContext) GetRequestArguments() map[string]string

GetRequestArguments returns all arguments provided to API function

func (*DefaultRestApplicationContext) GetRequestContent

func (it *DefaultRestApplicationContext) GetRequestContent() interface{}

GetRequestContent returns request contents of nil if not specified (HTTP request body)

func (*DefaultRestApplicationContext) GetRequestContentType

func (it *DefaultRestApplicationContext) GetRequestContentType() string

GetRequestContentType returns MIME type of request content

func (*DefaultRestApplicationContext) GetRequestFile

func (it *DefaultRestApplicationContext) GetRequestFile(name string) io.Reader

GetRequestFile returns particular file attached to request or nil

func (*DefaultRestApplicationContext) GetRequestFiles

func (it *DefaultRestApplicationContext) GetRequestFiles() map[string]io.Reader

GetRequestFiles returns files were attached to request

func (*DefaultRestApplicationContext) GetRequestSetting

func (it *DefaultRestApplicationContext) GetRequestSetting(name string) interface{}

GetRequestSetting returns particular request related setting or nil

func (*DefaultRestApplicationContext) GetRequestSettings

func (it *DefaultRestApplicationContext) GetRequestSettings() map[string]interface{}

GetRequestSettings returns request related settings

  • for REST API settings are HTTP headers and COOKIES

func (*DefaultRestApplicationContext) GetResponse

func (it *DefaultRestApplicationContext) GetResponse() interface{}

GetResponse returns raw response object

func (*DefaultRestApplicationContext) GetResponseContentType

func (it *DefaultRestApplicationContext) GetResponseContentType() string

GetResponseContentType returns MIME type of supposed response content

func (*DefaultRestApplicationContext) GetResponseResult

func (it *DefaultRestApplicationContext) GetResponseResult() interface{}

GetResponseResult returns result going to be written to response writer

func (*DefaultRestApplicationContext) GetResponseSetting

func (it *DefaultRestApplicationContext) GetResponseSetting(name string) interface{}

GetResponseSetting returns specified setting value (for REST API returns header as settings)

func (*DefaultRestApplicationContext) GetResponseWriter

func (it *DefaultRestApplicationContext) GetResponseWriter() io.Writer

GetResponseWriter returns io.Writes for response (for DefaultRestApplicationContext it is clone to GetResponse)

func (*DefaultRestApplicationContext) GetSession

GetSession returns session assigned to current context or nil if nothing was assigned

func (*DefaultRestApplicationContext) SetContextValue

func (it *DefaultRestApplicationContext) SetContextValue(key string, value interface{})

SetContextValue stores specified value in current context

func (*DefaultRestApplicationContext) SetResponseContentType

func (it *DefaultRestApplicationContext) SetResponseContentType(mimeType string) error

SetResponseContentType changes response content type, returns error if not possible

func (*DefaultRestApplicationContext) SetResponseResult

func (it *DefaultRestApplicationContext) SetResponseResult(value interface{}) error

SetResponseResult changes result going to be written to response writer

func (*DefaultRestApplicationContext) SetResponseSetting

func (it *DefaultRestApplicationContext) SetResponseSetting(name string, value interface{}) error

SetResponseSetting specifies response setting (for REST API it just sets additional header)

func (*DefaultRestApplicationContext) SetResponseStatus

func (it *DefaultRestApplicationContext) SetResponseStatus(code int)

SetResponseStatus will set an HTTP response code

  • code is an integer correlating to HTTP response codes

func (*DefaultRestApplicationContext) SetResponseStatusBadRequest

func (it *DefaultRestApplicationContext) SetResponseStatusBadRequest()

SetResponseStatusBadRequest will set the ResponseWriter to StatusBadRequest (400)

func (*DefaultRestApplicationContext) SetResponseStatusForbidden

func (it *DefaultRestApplicationContext) SetResponseStatusForbidden()

SetResponseStatusForbidden will set the ResponseWriter to StatusForbidden (403)

func (*DefaultRestApplicationContext) SetResponseStatusInternalServerError

func (it *DefaultRestApplicationContext) SetResponseStatusInternalServerError()

SetResponseStatusInternalServerError will set the ResponseWriter to StatusInternalServerError (500)

func (*DefaultRestApplicationContext) SetResponseStatusNotFound

func (it *DefaultRestApplicationContext) SetResponseStatusNotFound()

SetResponseStatusNotFound will set the ResponseWriter to StatusNotFound (404)

func (*DefaultRestApplicationContext) SetSession

func (it *DefaultRestApplicationContext) SetSession(session api.InterfaceSession) error

SetSession assigns given session to current context

type DefaultRestService

type DefaultRestService struct {
	ListenOn string
	Router   *httprouter.Router
	Handlers []string
}

DefaultRestService is a default implementer of InterfaceRestService declared in "github.com/ottemo/commerce/api" package

func (*DefaultRestService) DELETE

func (it *DefaultRestService) DELETE(resource string, handler api.FuncAPIHandler)

DELETE is a wrapper for the HTTP DELETE verb

func (*DefaultRestService) GET

func (it *DefaultRestService) GET(resource string, handler api.FuncAPIHandler)

GET is a wrapper for the HTTP GET verb

func (*DefaultRestService) GetName

func (it *DefaultRestService) GetName() string

GetName returns implementation name of our REST API service

func (*DefaultRestService) POST

func (it *DefaultRestService) POST(resource string, handler api.FuncAPIHandler)

POST is a wrapper for the HTTP POST verb

func (*DefaultRestService) PUT

func (it *DefaultRestService) PUT(resource string, handler api.FuncAPIHandler)

PUT is a wrapper for the HTTP PUT verb

func (*DefaultRestService) Run

func (it *DefaultRestService) Run() error

Run is the Ottemo REST server startup function, analogous to "ListenAndServe"

func (DefaultRestService) ServeHTTP

func (it DefaultRestService) ServeHTTP(responseWriter http.ResponseWriter, request *http.Request)

ServeHTTP is an entry point for HTTP request, it takes control before request handled (go lang "http.server" package "Handler" interface implementation)

Jump to

Keyboard shortcuts

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