Skip to content

Instantly share code, notes, and snippets.

@paul121
Last active February 8, 2022 01:01
Show Gist options
  • Save paul121/26bed0987b73c6886fa3a0743c0f47eb to your computer and use it in GitHub Desktop.
Save paul121/26bed0987b73c6886fa3a0743c0f47eb to your computer and use it in GitHub Desktop.
Example node script for making aggregator requests via farmOS.js
// Import client factory method for node.
const {client} = require('farmos/dist/cjs/farmOS');
// Create a custom auth method to set the api-key header and farm_id query param.
const apiKeyAuth = (request, authOpts = {}) => {
request.interceptors.request.use(
config => {
return {
...config,
headers: {
...config.headers,
'api-key': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJuYmYiOjE2NDQyNzM0MzkuMDAyMTc1LCJmYXJtX2lkIjpbXSwiYWxsX2Zhcm1zIjp0cnVlLCJzY29wZXMiOlsiZmFybTpyZWFkIiwiZmFybS5pbmZvIl19.b7arBzMkesSQBbt0UgLNT-up3Ml9_XbevvH61zLfZp0',
},
params: {
'farm_id': 1,
},
};
},
Promise.reject,
);
return {
authorize(apiKey) {
// Could use this method to set the api-key header or farm_id query param.
},
};
};
// Default token functions.
let token;
const getToken = () => token;
const setToken = (t) => { token = t; };
// Create the client.
const host = 'http://localhost/api/v2/farms/relay';
const farm = client(host, {
clientId: 'farm',
getToken,
setToken,
auth: apiKeyAuth,
});
// Create a log object.
let newLog = {
type: 'log--activity',
attributes: {
'name': 'My test log',
'status': 'done',
}
}
// Fetch logs.
farm.log.fetch('activity')
.then(res => console.log(`Have ${res.data.data.length} logs.`))
// Create a log.
.then(() => farm.log.send('activity', newLog))
.then(res => {
console.log('Created new log:', res.data);
return res;
})
// Fetch the single log.
.then((res) => farm.log.fetch('activity', {id: {$eq: res.data.id}}))
.then(res => console.log('Fetched the log by ID:', res.data))
// Fetch logs.
.then(() => farm.log.fetch('activity'))
.then(res => {
console.log(`Have ${res.data.data.length} logs.`);
return res;
})
// Delete log.
.then(res => farm.log.delete('activity', res.data.data[0].id))
.then(() => console.log('Delete the log.'))
// Fetch logs.
.then(() => farm.log.fetch('activity'))
.then(res => console.log(`Now have ${res.data.data.length} logs.`));
@paul121
Copy link
Author

paul121 commented Feb 7, 2022

Sample output:

$ node aggregator_js_test.js 
Have 4 logs.
Created new log: {
  jsonapi: { version: '1.0', meta: { links: [Object] } },
  data: {
    type: 'log--activity',
    id: '88cadb8f-5c7f-44dd-950d-9b3981d5cf63',
    links: { self: [Object] },
    attributes: {
      drupal_internal__id: 8,
      drupal_internal__revision_id: 8,
      langcode: 'en',
      revision_created: '2022-02-08T01:00:29+00:00',
      revision_log_message: null,
      name: 'My test log',
      timestamp: '2022-02-08T01:00:29+00:00',
      status: 'done',
      created: '2022-02-08T01:00:29+00:00',
      changed: '2022-02-08T01:00:29+00:00',
      default_langcode: true,
      revision_translation_affected: true,
      data: null,
      notes: null,
      flag: [],
      is_group_assignment: null,
      geometry: null,
      is_movement: false
    },
    relationships: {
      log_type: [Object],
      revision_user: [Object],
      uid: [Object],
      category: [Object],
      file: [Object],
      image: [Object],
      group: [Object],
      location: [Object],
      asset: [Object],
      quantity: [Object],
      owner: [Object],
      equipment: [Object]
    }
  },
  links: {
    self: {
      href: 'http://main-kma795eoyln7kyyvmmq0xwsfbiqkslpm.tugboat.qa/api/log/activity'
    }
  }
}
Fetched the log by ID: {
  jsonapi: { version: '1.0', meta: { links: [Object] } },
  data: [
    {
      type: 'log--activity',
      id: '3f091216-2643-4377-bb9a-b339a5839794',
      links: [Object],
      attributes: [Object],
      relationships: [Object]
    },
    {
      type: 'log--activity',
      id: 'c3f35375-29fa-4f95-8813-f4fe94733f8b',
      links: [Object],
      attributes: [Object],
      relationships: [Object]
    },
    {
      type: 'log--activity',
      id: 'e92eec54-c37f-4305-aaa3-37f4b4dda969',
      links: [Object],
      attributes: [Object],
      relationships: [Object]
    },
    {
      type: 'log--activity',
      id: '9202aaeb-4896-44f8-8b93-9dee6d079ec5',
      links: [Object],
      attributes: [Object],
      relationships: [Object]
    },
    {
      type: 'log--activity',
      id: '88cadb8f-5c7f-44dd-950d-9b3981d5cf63',
      links: [Object],
      attributes: [Object],
      relationships: [Object]
    }
  ],
  links: {
    self: {
      href: 'http://main-kma795eoyln7kyyvmmq0xwsfbiqkslpm.tugboat.qa/api/log/activity'
    }
  }
}
Have 5 logs.
Delete the log.
Now have 4 logs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment