Skip to content

Commit

Permalink
Merge pull request #620 from TheThingsNetwork/feature/26-gateways-ser…
Browse files Browse the repository at this point in the history
…vice

Add basic gateways CRUD operations to js sdk
  • Loading branch information
Bogdans authored May 3, 2019
2 parents 40b55b0 + ec8b4e2 commit 2970ec3
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 11 deletions.
6 changes: 6 additions & 0 deletions pkg/webui/console/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ export default {
gateways: {
list: ttnClient.Gateways.getAll.bind(ttnClient.Gateways),
},
gateway: {
get: ttnClient.Gateways.getById.bind(ttnClient.Gateways),
'delete': ttnClient.Gateways.deleteById.bind(ttnClient.Gateways),
create: ttnClient.Gateways.create.bind(ttnClient.Gateways),
update: ttnClient.Gateways.updateById.bind(ttnClient.Gateways),
},
rights: {
applications: ttnClient.Applications.getRightsById.bind(ttnClient.Applications),
},
Expand Down
18 changes: 10 additions & 8 deletions pkg/webui/console/store/middleware/gateways.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,22 @@ import * as gateways from '../actions/gateways'
const getGatewaysLogic = createLogic({
type: [
gateways.GET_GTWS_LIST,
gateways.CHANGE_GTWS_ORDER,
gateways.CHANGE_GTWS_PAGE,
gateways.SEARCH_GTWS_LIST,
],
latest: true,
async process ({ getState, action }, dispatch, done) {
const { filters } = action
const { page, pageSize: limit, query } = action.filters

try {
const data = filters.query
? await api.gateways.search(filters)
: await api.gateways.list(filters)
const gtws = data.gateways.map(g => ({ ...g, antennasCount: g.antennas.length }))
dispatch(gateways.getGatewaysSuccess(gtws, data.totalCount))
const data = query
? await api.gateways.search({
page,
limit,
id_contains: query,
name_contains: query,
})
: await api.gateways.list({ page, limit }, [ 'name,description,frequency_plan_id' ])
dispatch(gateways.getGatewaysSuccess(data.gateways, data.totalCount))
} catch (error) {
dispatch(gateways.getGatewaysFailure(error))
}
Expand Down
4 changes: 3 additions & 1 deletion sdk/js/src/service/applications.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,11 @@ class Applications {
// Delete

async deleteById (applicationId) {
return this._api.ApplicationRegistry.Delete({
const response = await this._api.ApplicationRegistry.Delete({
routeParams: { application_id: applicationId },
})

return Marshaler.payloadSingleResponse(response)
}

async getRightsById (applicationId) {
Expand Down
54 changes: 52 additions & 2 deletions sdk/js/src/service/gateways.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,63 @@ import Marshaler from '../util/marshaler'
class Gateways {
constructor (api, { defaultUserId, stackConfig, proxy = true }) {
this._api = api
this._defaultUserId = defaultUserId
}

async getAll (params) {
const response = await this._api.GatewayRegistry.List(undefined, params)
// Retrieval

async getAll (params, selector) {
const response = await this._api.GatewayRegistry.List(undefined, {
...params,
...Marshaler.selectorToFieldMask(selector),
})

return Marshaler.unwrapGateways(response)
}

async getById (id, selector) {
const fieldMask = Marshaler.selectorToFieldMask(selector)
const response = await this._api.GatewayRegistry.Get({
routeParams: { 'gateway_ids.gateway_id': id },
}, fieldMask)

return Marshaler.unwrapGateway(response)
}

// Update

async updateById (id, patch, mask = Marshaler.fieldMaskFromPatch(patch)) {
const response = await this._api.GatewayRegistry.Update({
routeParams: { 'gateway.ids.gateway_id': id },
},
{
gateway: patch,
field_mask: Marshaler.fieldMask(mask),
})

return Marshaler.unwrapGateway(response)
}

// Create

async create (userId = this._defaultUserId, gateway) {
const response = await this._api.GatewayRegistry.Create({
routeParams: { 'collaborator.user_ids.user_id': userId },
},
{ gateway })

return Marshaler.unwrapGateway(response)
}

// Delete

async deleteById (id) {
const response = await this._api.GatewayRegistry.Delete({
routeParams: { gateway_id: id },
})

return Marshaler.payloadSingleResponse(response)
}
}

export default Gateways
4 changes: 4 additions & 0 deletions sdk/js/src/util/marshaler.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ class Marshaler {
return this.payloadListResponse('gateways', result, transform)
}

static unwrapGateway (result, transform) {
return this.payloadSingleResponse(result, transform)
}

static fieldMaskFromPatch (patch) {
return traverse(patch).paths().slice(1).map( e => e.join('.') )
}
Expand Down

0 comments on commit 2970ec3

Please sign in to comment.