Skip to content

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.

Import

/* if in src/ */
import { ParameterContainer } from './models/Core'

Interface

ParameterContainer extends Immutable.Record using the following pattern:

Class ParameterContainer extends Immutable.Record({
    headers: Immutable.List(),
    queries: Immutable.List(),
    body: Immutable.List()
})

Fields

ParameterContainer.headers
  • ParameterContainer.headers expects an Immutable.List of Parameters. These Parameters should all be located in the Header section of an HTTP request.
ParameterContainer.queries
  • 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
  • ParameterContainer.body expects an Immutable.List of Parameters. These Parameters should all be located in the body section of an HTTP request.

Methods

In addition to these fields, ParameterContainer also provides a function that can help manipulate its data:

getHeadersSet
  • ParameterContainer.getHeadersSet() returns an Immutable.OrderedMap of Parameters based on their key. If multiple Parameters share the same key, the last Parameter with that key in ParameterContainer.headers will override the others.
getUrlParams
  • ParameterContainer.getUrlParams(generateIfNotExist) returns a string that transforms the list of Parameters in ParameterContainer.queries into a string that follows the definition of the query structure, as described in rfc#3986, for example: ?key=value1&second=value2
filter
  • 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 its isValid method against each of the parameters in _parameters_. If the Parameter is valid against all of _parameters_, it is included in the resulting ParameterContainer, otherwise it is discarded. This allows the generation on the fly of ParameterContainers that match specific environments, as to easily construct Requests.

Example

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] ])
Clone this wiki locally