Skip to content

Commit

Permalink
Merge pull request #181 from particle-iot/add-setLedgerInstance-setMo…
Browse files Browse the repository at this point in the history
…de-query-arg

Add setMode arg to setLedgerInstance
  • Loading branch information
cole-abbeduto-particle authored Jan 3, 2024
2 parents 86cfa13 + df63f47 commit 3bc2e6a
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 27 deletions.
11 changes: 6 additions & 5 deletions src/Agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class Agent {
* @param {string} params.uri The URI to request
* @param {Auth} [params.auth] Authorization token to use
* @param {object} [params.headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
* @param {object} [params.data] Key/Value pairs of query params
* @param {object} [params.data] Request body
* @param {object} [params.context] The invocation context, describing the tool and project
* @returns {Promise<RequestResponse, RequestError>} A promise that resolves with either the requested data or an error object
*/
Expand All @@ -117,12 +117,13 @@ class Agent {
* @param {string} params.uri The URI to request
* @param {Auth} [params.auth] Authorization token to use
* @param {object} [params.headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
* @param {object} [params.data] Key/VAlue pairs of query params
* @param {object} [params.data] Request body
* @param {object} [params.query] Key/Value pairs of query params or a correctly formatted string
* @param {object} [params.context] The invocation context, describing the tool and project
* @returns {Promise<RequestResponse, RequestError>} A promise that resolves with either the requested data or an error object
*/
put({ uri, auth, headers, data, context }) {
return this.request({ uri, method: 'put', auth, headers, data, context });
put({ uri, auth, headers, data, query, context }) {
return this.request({ uri, method: 'put', auth, headers, data, query, context });
}

/**
Expand All @@ -131,7 +132,7 @@ class Agent {
* @param {string} params.uri The URI to request
* @param {Auth} [params.auth] Authorization token to use
* @param {object} [params.headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
* @param {object} [params.data] Key/Value pairs of query params
* @param {object} [params.data] Request body
* @param {object} [params.context] The invocation context, describing the tool and project
* @returns {Promise<RequestResponse, RequestError>} A promise that resolves with either the requested data or an error object
*/
Expand Down
37 changes: 23 additions & 14 deletions src/Particle.js
Original file line number Diff line number Diff line change
Expand Up @@ -2477,23 +2477,31 @@ class Particle {
});
}

/**
* @typedef {"Replace" | "Merge"} SetMode
*/

/**
* Set ledger instance data.
*
* @param {Object} options The options for updating the ledger instance.
* @param {Auth} [options.auth] The access token or basic auth object. Can be ignored if provided in constructor
* @param {string} [options.org] The Organization ID or slug. If not provided, the request will go to your sandbox account.
* @param {string} options.ledgerName Ledger name.
* @param {string} options.scopeValue Scope value.
* @param {object} options.instance The instance with the data
* @param {Object} [options.headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
* @param {Object} [options.context] Request context.
* @param {Object} options The options for updating the ledger instance.
* @param {Auth} [options.auth] The access token or basic auth object. Can be ignored if provided in constructor
* @param {string} [options.org] The Organization ID or slug. If not provided, the request will go to your sandbox account.
* @param {string} options.ledgerName Ledger name.
* @param {string} options.scopeValue Scope value.
* @param {object} options.instance The instance with the data
* @param {SetMode} [options.setMode] How the data should be set with existing data. Default is "Replace"
* @param {Object} [options.headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
* @param {Object} [options.context] Request context.
*
* @returns {Promise<RequestResponse>} A promise that resolves to the updated ledger instance data.
*/
setLedgerInstance({ auth, org, ledgerName, scopeValue, instance, headers, context }) {
setLedgerInstance({ auth, org, ledgerName, scopeValue, instance, setMode, headers, context }) {
return this.put({
uri: this._namespacedPath(org, `ledgers/${ledgerName}/instances/${scopeValue}`),
query: {
set_mode: setMode
},
auth,
data: { instance },
headers,
Expand Down Expand Up @@ -2685,7 +2693,7 @@ class Particle {
* @param {string} params.uri The URI to request
* @param {Auth} [params.auth] Authorization token to use
* @param {object} [params.headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
* @param {string | object} [params.data] Key/Value pairs of query params or a correctly formatted string
* @param {string | object} [params.data] Request body
* @param {object} [params.context] The invocation context, describing the tool and project
* @returns {Promise<RequestResponse, RequestError>} A promise that resolves with either the requested data or an error object
*/
Expand All @@ -2701,14 +2709,15 @@ class Particle {
* @param {string} params.uri The URI to request
* @param {Auth} [params.auth] Authorization token to use
* @param {object} [params.headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
* @param {string | object} [params.data] Key/Value pairs of query params or a correctly formatted string
* @param {string | object} [params.data] Request body
* @param {object} [params.query] Key/Value pairs of query params or a correctly formatted string
* @param {object} [params.context] The invocation context, describing the tool and project
* @returns {Promise<RequestResponse, RequestError>} A promise that resolves with either the requested data or an error object
*/
put({ uri, auth, headers, data, context }){
put({ uri, auth, headers, data, query, context }){
context = this._buildContext(context);
auth = this._getActiveAuthToken(auth);
return this.agent.put({ uri, auth, headers, data, context });
return this.agent.put({ uri, auth, headers, data, query, context });
}

/**
Expand All @@ -2717,7 +2726,7 @@ class Particle {
* @param {string} params.uri The URI to request
* @param {Auth} [params.auth] Authorization token to use
* @param {object} [params.headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
* @param {string | object} [params.data] Key/Value pairs of query params or a correctly formatted string
* @param {string | object} [params.data] Request body
* @param {object} [params.context] The invocation context, describing the tool and project
* @returns {Promise<RequestResponse, RequestError>} A promise that resolves with either the requested data or an error object
*/
Expand Down
4 changes: 2 additions & 2 deletions test/Agent.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ describe('Agent', () => {

it('can PUT a resource', () => {
method = 'put';
return agent.put({ uri, auth, headers, data, context }).then(() => {
expect(agent.request).to.be.calledWith({ uri, method, auth, headers, data, context });
return agent.put({ uri, auth, headers, data, context, query }).then(() => {
expect(agent.request).to.be.calledWith({ uri, method, auth, headers, data, context, query });
});
});

Expand Down
6 changes: 3 additions & 3 deletions test/FakeAgent.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ class FakeAgent {
return this.request({ uri, method: 'post', auth, headers, data, context });
}

put({ uri, auth, headers, data, context }){
return this.request({ uri, method: 'put', auth, headers, data, context });
put({ uri, auth, headers, data, query, context }){
return this.request({ uri, method: 'put', auth, headers, data, query, context });
}

delete({ uri, auth, headers, data, context }){
return this.request({ uri, method: 'delete', auth, headers, data, context });
}

request(opts){
return new Promise((resolve) => resolve(opts));
return Promise.resolve(opts);
}
}
module.exports = FakeAgent;
17 changes: 14 additions & 3 deletions test/Particle.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,13 @@ const props = {
scope: 'Owner',
name: 'myledger',
description: 'my ledger',
direction: 'Downstream'
direction: 'CloudOnly'
},
scopeValue: 'abc1234',
instance: {
property: 'yes'
}
},
setMode: 'Replace'
};

const product = 'ze-product-v1';
Expand Down Expand Up @@ -315,6 +316,7 @@ describe('ParticleAPI', () => {
method: 'put',
auth: props.auth,
headers: props.headers,
query: undefined,
data: {
current_password: props.password,
},
Expand Down Expand Up @@ -1462,6 +1464,7 @@ describe('ParticleAPI', () => {
method: 'put',
auth: 'X',
headers: undefined,
query: undefined,
data: {
account_info: { first_name: 'John', last_name: 'Scully' }
},
Expand All @@ -1480,6 +1483,7 @@ describe('ParticleAPI', () => {
method: 'put',
auth: 'X',
headers: undefined,
query: undefined,
data: {
current_password: 'blabla',
username: '[email protected]'
Expand All @@ -1496,6 +1500,7 @@ describe('ParticleAPI', () => {
method: 'put',
auth: 'X',
headers: undefined,
query: undefined,
data: {
current_password: 'blabla',
username: '[email protected]',
Expand All @@ -1516,6 +1521,7 @@ describe('ParticleAPI', () => {
method: 'put',
auth: 'X',
headers: undefined,
query: undefined,
data: {
current_password: 'blabla',
password: 'blabla2'
Expand All @@ -1533,6 +1539,7 @@ describe('ParticleAPI', () => {
uri: '/v1/user',
auth: 'X',
headers: undefined,
query: undefined,
data: {
current_password: 'blabla',
password: 'blabla2',
Expand Down Expand Up @@ -2814,6 +2821,9 @@ describe('ParticleAPI', () => {
results.should.match({
method: 'put',
uri: `/v1/orgs/${org}/ledgers/${props.ledgerName}/instances/${props.scopeValue}`,
query: {
set_mode: props.setMode
},
auth: props.auth,
data: {
instance: props.instance
Expand Down Expand Up @@ -3015,14 +3025,15 @@ describe('ParticleAPI', () => {

it('calls _buildContext and _getActiveAuthToken from put', () => {
api.agent.put = sinon.stub().returns(result);
const options = { uri, auth, headers, data, context };
const options = { uri, auth, headers, data, context, query };
const res = api.put(options);
expect(res).to.equal(result);
expect(api.agent.put).to.have.been.calledWith({
uri,
auth,
headers,
data,
query,
context: contextResult
});
});
Expand Down

0 comments on commit 3bc2e6a

Please sign in to comment.