Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic gateways CRUD operations to js sdk #620

Merged
merged 4 commits into from
May 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better if we also unwrap the results here using Marshaler.payloadSingleResponse(). Same thing with Applications.delete().

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