How to send post request to node.js from python to utilize geckoboard?

808 Views Asked by At

Geckoboard offers this documentation to connect to their datasets API, which you can see the implementation below.

var API_KEY = 'API_KEY';

var gb = require('geckoboard')(
  API_KEY
);


gb.datasets.findOrCreate(
  {
    id: 'sales.by_day',
    fields: {
      quantity: {
        type: 'number',
        name: 'Number of sales'
      },
      gross: {
        type: 'money',
        name: 'Gross value of sales',
        currency_code: "USD"
      },
      date: {
        type: 'date',
        name: 'Date'
      }
    }
  },
  function (err, dataset) {
    if (err) {
      console.error(err);
      return;
    }
    dataset.put(
      [
        { date: '2016-01-01', quantity: 819, gross: 2457000 },
        { date: '2016-01-02', quantity: 409, gross: 1227000 },
        { date: '2016-01-03', quantity: 164, gross: 492000 }
      ],
      function (err) {
        if (err) {
          console.error(err);
          return;
        }

        console.log('Dataset created and data added');
      }
    );
  }
);

I'd like to see if there is a way to post this additional data via python (not using node.js). Would something like this be possible or must I use mode?

[
    { date: '2017-01-01', quantity: 1213, gross: 23423 },
    { date: '2017-01-02', quantity: 111, gross: 1313123 },
    { date: '2017-01-03', quantity: 333, gross: 21314 }
]
1

There are 1 best solutions below

2
On BEST ANSWER

Update: Geckoboard now has their own official Python client library for their Datasets API https://github.com/geckoboard/geckoboard-python


It's definitely possible to use Python with Geckoboard's Datasets API. You can use any language or platform that can perform HTTPS requests with JSON - though Geckoboard has only released official library's for Ruby and Node so far.

Edit: I made a quick example below, but later found this: https://github.com/helium/geckoboard-python/

In short you just need to:

  • PUT with the a schema object to https://api.geckoboard.com/datasets to create a dataset
  • PUT with the a data array to https://api.geckoboard.com/datasets/:id to replace all data in a dataset
  • POST with the a data array to https://api.geckoboard.com/datasets/:id to append to the data in a dataset
  • DELETE to https://api.geckoboard.com/datasets/:id to delete a dataset

These requests are outlined at -- https://developer.geckoboard.com/api-reference/curl/

I haven't written much Python, so this may not be very Pythonic, but here's a way you could create a Geckoboard library in Python to do this for you, using the requests library underneath:

import requests

  class Geckoboard(object):
          def __init__(self, api_key):
                  self.api_key = api_key

          def create(self, name, schema):
                  url = 'https://api.geckoboard.com/datasets/%s' % name
                  return requests.put(url, json=schema, auth=(self.api_key, ''))

          def delete(self, name):
                  url = 'https://api.geckoboard.com/datasets/%s' % name
                  return request.delete(url, auth=(self.api_key, ''))

          def replace(self, name, data):
                  url = 'https://api.geckoboard.com/datasets/%s/data' % name
                  return requests.put(url, json=data, auth=(self.api_key, ''))

          def append(self, name, data):
                  url = 'https://api.geckoboard.com/datasets/%s/data' % name
                  return requests.post(url, json=data, auth=(self.api_key, ''))