Skip to content

Commit

Permalink
Add form-data and qs dependencies, update getAccessTokenFromLoginEndp…
Browse files Browse the repository at this point in the history
…oint function, and modify openapi.yml and component.json files
  • Loading branch information
jirihofman committed Feb 27, 2024
1 parent 8ca19ac commit 1946f79
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 12 deletions.
7 changes: 6 additions & 1 deletion src/appmixer/daktela/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ module.exports = {
}
},

// This function is called in every component to get the access token from the login endpoint.
/**
* Function called in every component to get the access token from the login endpoint.
* It is cached so that we don't have to call the login endpoint in every component.
* Access token changes only when the user changes the password.
* @returns Access token
*/
getAccessTokenFromLoginEndpoint: async function(context) {

const cacheKey = `daktela-access-token-${context.username || context.auth.username}`;
Expand Down
24 changes: 22 additions & 2 deletions src/appmixer/daktela/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,36 @@ paths:
parameters:
offset: skip
limit: take
page: 100
# Ticket objects are quite large, so we limit the number of items per page
page: 10
results: result.data
count: result.count
count: total
parameters:
- name: q
in: query
required: false
description: Search query.
schema:
type: string
- name: filter
in: query
required: false
description: Filter tickets. See [Daktela API documentation](https://democz.daktela.com/external/apihelp/v6/working-with/tickets#example-1-url) for more information.
schema:
type: array
example: [{"field":"status","operator":"eq","value":"OPEN"}]
items:
type: object
- name: sort
in: query
required: false
description: |
Sorting is defined as an array of columns by which the data can be sorted. Each array has its own sorting name and each item within its range has two properties:
- field - string - Name of the column by which you want to sort
- dir - string - Direction of sorting 'asc' (sort in a ascending order) or 'desc' (sort in a descending order)
schema:
type: string
example: {"sort": [{"field":"firstname", "dir":"desc"},{"field":"lastname", "dir":"asc"}]}
responses:
'200':
description: A list of tickets
Expand Down
3 changes: 2 additions & 1 deletion src/appmixer/daktela/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"jsonata": "2.0.3",
"json-pointer": "0.6.2",
"jmespath": "0.16.0",
"form-data": "4.0.0"
"form-data": "4.0.0",
"qs": "6.11.2"
}
}
22 changes: 20 additions & 2 deletions src/appmixer/daktela/tickets/getTickets/component.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@
"schema": {
"type": "object",
"properties": {
"q": {
"type": "string"
},
"filter": {
"type": "string"
},
"sort": {
"type": "string"
},
"xConnectorOutputType": {
"type": "string"
},
Expand All @@ -24,11 +30,23 @@
},
"inspector": {
"inputs": {
"q": {
"type": "text",
"index": 0,
"label": "Search",
"tooltip": "<p>Search query.</p>"
},
"filter": {
"type": "textarea",
"index": 0,
"index": 1,
"label": "Filter",
"tooltip": "<p>Filter tickets. See <a href=\"https://democz.daktela.com/external/apihelp/v6/working-with/tickets#example-1-url\" rel=\"noopener noreferrer\" target=\"_blank\">Daktela API documentation</a> for more information.</p>"
"tooltip": "<p>Filter tickets. See <a href=\"https://democz.daktela.com/external/apihelp/v6/working-with/tickets#example-1-url\" rel=\"noopener noreferrer\" target=\"_blank\">Daktela API documentation</a> for more information.</p><p>Example: <code>{\"filter\":[{\"field\":\"stage\",\"operator\":\"eq\",\"value\":\"OPEN\"}]}</code></p>"
},
"sort": {
"type": "textarea",
"index": 2,
"label": "Sort",
"tooltip": "<p>Sorting is defined as an array of columns by which the data can be sorted. Each array has its own sorting name and each item within its range has two properties:</p>\n<ul>\n<li>field - string - Name of the column by which you want to sort</li>\n<li>dir - string - Direction of sorting 'asc' (sort in a ascending order) or 'desc' (sort in a descending order)</li>\n</ul>"
},
"xConnectorOutputType": {
"label": "Output Options",
Expand Down
31 changes: 25 additions & 6 deletions src/appmixer/daktela/tickets/getTickets/getTickets.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const lib = require('../../lib');
const { getAccessTokenFromLoginEndpoint } = require('../../auth');
const qs = require('qs');

module.exports = {

Expand All @@ -13,7 +14,7 @@ module.exports = {

const limit = context.messages.in.content.xConnectorPaginationLimit;
const query = {
'take': 100 ,
'take': 10 ,
'skip': 0
};
let data;
Expand All @@ -29,15 +30,15 @@ module.exports = {
result = page.slice(0, limit);

hasMore = result.length > 0;
const countExpression = lib.jsonata('result.count');
const countExpression = lib.jsonata('total');
let count = await countExpression.evaluate(data);
hasMore = hasMore && result.length < count;
needMore = result.length < limit;
// Failsafe in case the 3rd party API doesn't behave correctly, to prevent infinite loop.
let failsafe = 0;
// Repeat for other pages.
while (hasMore && needMore && failsafe < limit) {
query['skip'] += 100;
query['skip'] += 10;
({ data } = await this.httpRequest(context, { query }));
page = await pageExpression.evaluate(data);
result = result.concat(page);
Expand Down Expand Up @@ -66,7 +67,11 @@ module.exports = {
const headers = {};
const query = new URLSearchParams;

const queryParameters = { 'filter': input['filter'] };
const queryParameters = {
q: input['q'],
// 'filter': input['filter'], Done separately
'sort': input['sort']
};

if (override?.query) {
Object.keys(override.query).forEach(parameter => {
Expand Down Expand Up @@ -94,7 +99,21 @@ module.exports = {
if (override.headers) req.headers = override.headers;
if (override.method) req.method = override.method;

const queryString = query.toString();
let queryString = query.toString();
const inputFilter = input['filter']?.trim();
if (inputFilter) {
try {
const filterObject = JSON.parse(input['filter']);
const qsFilter = qs.stringify(filterObject, { encode: true });
if (qsFilter) {
queryString += '&' + qsFilter;
}
} catch (e) {
// context.log({ step: 'Error parsing filter object', error: e });
throw new context.CancelError('Error parsing filter object', e);
}
}

if (queryString) {
req.url += '?' + queryString;
}
Expand Down Expand Up @@ -123,7 +142,7 @@ module.exports = {
step: 'http-request-error',
request: {
url: req.url,
method: req.method,

headers: req.headers,
data: req.data
},
Expand Down

0 comments on commit 1946f79

Please sign in to comment.