Skip to content

Commit

Permalink
Merge pull request #25 from domapic/release-1.0.0-alpha.5
Browse files Browse the repository at this point in the history
Release 1.0.0 alpha.5
  • Loading branch information
javierbrea committed Oct 15, 2018
2 parents fe6a3aa + 340a564 commit d1cc677
Show file tree
Hide file tree
Showing 24 changed files with 488 additions and 89 deletions.
9 changes: 9 additions & 0 deletions .narval.yml
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ suites:
specs:
- test/functional/specs/ability-action-api.specs.js
coverage: *base-coverage
- name: ability-state
describe: Ability state api should work as expected
before: *base-before
services: *base-services
test:
<<: *base-test
specs:
- test/functional/specs/ability-state-api.specs.js
coverage: *base-coverage
- name: users-cli
describe: Users cli should be able to add and remove users
before: *base-before
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Fixed
### Removed

## [1.0.0-alpha.5] - 2018-10-15
### Added
- Add ability state api

### Changed
- Upgrade dependencies
- Returns BadGateway error instead of ClientTimeOut error when service is not available

## [1.0.0-alpha.4] - 2018-10-14
### Added
- Add service action api
Expand Down
31 changes: 24 additions & 7 deletions lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ const uris = require('./uris')
const templates = require('./templates')

const Client = service => {
const handleServiceError = (error) => {
if (error.typeof === 'ServerUnavailable') {
return Promise.reject(new service.errors.BadGateway(templates.serviceNotAvailable()))
}
return Promise.reject(error)
}

const sendAction = (serviceData, ability, data) => {
const client = new service.client.Connection(serviceData.url, {
apiKey: serviceData.apiKey
Expand All @@ -14,17 +21,27 @@ const Client = service => {
_ability: ability._id,
data: data.data
})).then(() => client.post(uris.abilityActionHandler(ability.name), data)
.catch((err) => {
if (err.typeof === 'ServerUnavailable') {
return Promise.reject(new service.errors.ClienTimeOut(templates.serviceNotAvailable()))
}
return Promise.reject(err)
})
.catch(handleServiceError)
)
}

const getState = (serviceData, ability) => {
const client = new service.client.Connection(serviceData.url, {
apiKey: serviceData.apiKey
})

return service.tracer.debug(templates.gettingAbilityState({
_service: serviceData._id,
_ability: ability._id
})).then(() => client.get(uris.abilityStateHandler(ability.name))
.then(response => Promise.resolve(response.body))
.catch(handleServiceError)
)
}

return {
sendAction
sendAction,
getState
}
}

Expand Down
3 changes: 3 additions & 0 deletions lib/api/abilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ const Operations = (service, commands) => {
res.header('location', `/api/abilities/${params.path.id}/state`)
return Promise.resolve()
})
},
abilityState: {
handler: (params, body, res) => commands.composed.getAbilityState(params.path.id)
}
}
}
Expand Down
44 changes: 43 additions & 1 deletion lib/api/abilities.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
},{
"name": "action",
"description": "Domapic service ability action"
},{
"name": "state",
"description": "Domapic service ability state"
}],
"components": {
"schemas": {
Expand Down Expand Up @@ -538,7 +541,7 @@
],
"tags": ["ability", "action"],
"summary": "Dispatches an ability action",
"description": "Dispatches a request to the correspondant ability action. If command is an state, controller will make a request to the ability in order to return the state. Event command can be used by the service only, in order to inform about changes",
"description": "Dispatches a request to the correspondant ability action.",
"operationId": "abilityAction",
"requestBody": {
"description": "Data of the action",
Expand All @@ -565,6 +568,45 @@
"apiKey": []
}]
}
},
"/abilities/{id}/state": {
"get": {
"parameters": [
{
"in": "path",
"name": "id",
"schema": {
"type": "string"
},
"required": true,
"description": "Ability id"
}
],
"tags": ["ability", "state"],
"summary": "Get ability state",
"description": "Controller makes a request to the ability in order to return the state.",
"operationId": "abilityState",
"responses": {
"200": {
"description": "Get ability state success",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/AbilityData"
}
}
}
},
"404": {
"$ref": "#/components/responses/NotFoundError"
}
},
"security": [{
"jwt": []
}, {
"apiKey": []
}]
}
}
}
}
11 changes: 10 additions & 1 deletion lib/commands/ability.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,23 @@ const Commands = (service, models, client) => {
return validateData(ability, data)
})

const validateState = (_id, data) => getById(_id)
.then(ability => {
if (!ability.state) {
return Promise.reject(new service.errors.NotFound(templates.abilityStateNotDefined()))
}
return Promise.resolve(ability)
})

return {
add,
getFiltered,
get,
getById,
update,
remove,
validateAction
validateAction,
validateState
}
}

Expand Down
11 changes: 8 additions & 3 deletions lib/commands/composed.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@ const Commands = (service, models, client, commands) => {
const dispatchAbilityAction = (abilityId, data) => commands.ability.validateAction(abilityId, data)
.then(ability => commands.service.getById(ability._service, {
allFields: true
}).then(service => client.sendAction(service, ability, data))
)
}).then(service => client.sendAction(service, ability, data)))

const getAbilityState = abilityId => commands.ability.validateState(abilityId)
.then(ability => commands.service.getById(ability._service, {
allFields: true
}).then(service => client.getState(service, ability)))

return {
initUsers,
dispatchAbilityAction
dispatchAbilityAction,
getAbilityState
}
}

Expand Down
2 changes: 2 additions & 0 deletions lib/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ const templates = {
abilityRemoved: 'Ability with name "{{name}}" of service "{{_service}}" was removed',

abilityActionNotDefined: 'Ability has not an action defined',
abilityStateNotDefined: 'Ability has not an state defined',
sendingAbilityAction: 'Sending action to service "{{_service}}", ability "{{_ability}}". Data: {{{toJSON data}}}',
gettingAbilityState: 'Sending request to get state from service "{{_service}}", ability "{{_ability}}"',
serviceNotAvailable: 'Service not available'
}

Expand Down
Loading

0 comments on commit d1cc677

Please sign in to comment.