-
Notifications
You must be signed in to change notification settings - Fork 24
ParameterContainer
JonathanMontane edited this page May 4, 2016
·
4 revisions
A ParameterContainer component represents the ensemble of Parameters that can be used in a Request. A container splits the parameters in different fields depending on their location in an HTTP Request, namely headers, queries, and body. It also has a few helpers to simplify the management of these Parameters.
/* if in src/ */
import { ParameterContainer } from './models/Core'
ParameterContainer extends Immutable.Record using the following pattern:
Class ParameterContainer extends Immutable.Record({
headers: Immutable.List(),
queries: Immutable.List(),
body: Immutable.List()
})
-
ParameterContainer.headers
expects an Immutable.List of Parameters. These Parameters should all be located in the Header section of an HTTP request.
-
ParameterContainer.queries
an Immutable.List of Parameters. These Parameters should all be located in the Query section of the url of an HTTP request. API-Flow follows the rfc#3986 for the definition of the different sections of a uri.
-
ParameterContainer.body
expects an Immutable.List of Parameters. These Parameters should all be located in the body section of an HTTP request.
In addition to these fields, ParameterContainer
also provides a function that can help manipulate its data:
-
ParameterContainer.getHeadersSet()
returns an Immutable.OrderedMap of Parameters based on theirkey
. If multiple Parameters share the same key, the last Parameter with that key inParameterContainer.headers
will override the others.
-
ParameterContainer.getUrlParams(generateIfNotExist)
returns astring
that transforms the list of Parameters inParameterContainer.queries
into a string that follows the definition of the query structure, as described in rfc#3986, for example:?key=value1&second=value2
-
ParameterContainer.filter(_parameters_)
filters its content based their validity against the List of Parameters_parameters_
. Internally, this means that each Parameter in the ParameterContainer calls itsisValid
method against each of the parameters in_parameters_
. If the Parameter is valid against all of_parameters_
, it is included in the resultingParameterContainer
, otherwise it is discarded. This allows the generation on the fly ofParameterContainers
that match specific environments, as to easily construct Requests.
Please note that this example is abstract, and is there purely to represent the different interactions with ParameterContainer
. The data may be non-sensical.
import parameters from './samples/parameters-examples'
import containers from './samples/containers-examples'
import { ParameterContainer } from './models/Core'
let container = new ParameterContainer({
headers: new Immutable.List([
parameters[0], parameters[1]
]),
queries: new Immutable.List([
parameters[2], parameters[3]
]),
body: new Immutable.List([
parameters[4], parameters[5]
])
})
container = container
.set('headers', new Immutable.List([ parameters[2], parameters[3] ]))
.set('queries', new Immutable.List([ parameters[4], parameters[5] ]))
.set('body', new Immutable.List([ parameters[0], parameters[1] ]))
.setIn([ 'headers', 0 ], parameters[0])
.setIn([ 'queries', 1 ], parameters[1])
.setIn([ 'body', 0 ], parameters[2])
let hSet = container.getHeadersSet()
let queryString = container.getUrlParams(true)
let filtered = container
.filter([ parameters[6], parameters[7] ])
.filter([ parameters[8] ])