Coder Social home page Coder Social logo

mixmaxhq / batchelor Goto Github PK

View Code? Open in Web Editor NEW
67.0 67.0 11.0 824 KB

A lovely little Node.js module to perform batch requests with the Google REST API

Home Page: https://www.npmjs.com/package/batchelor

License: MIT License

JavaScript 100.00%
batch-request batchelor google-api javascript node-module nodejs npm-package

batchelor's People

Contributors

bradvogel avatar bradwalton avatar dtkav avatar guilhermemj avatar iamchucky avatar jamesmhaley avatar jongyoonlee avatar jsalvata avatar maur1th avatar maxko87 avatar renovate[bot] avatar vladmiller avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

batchelor's Issues

Getting response { parts: [], errors: 0 } regardless of what token we use for Bearer

This may not be a bug on your part, just wondering if you've seen this? We've been using the access_token acquired by the usual oauth2 exchange auth code for tokens flow, server side. Is this even the correct token to use here? It seems no matter what we put in for Bearer we get this response. I would expect a 400 or 401 response, not { parts: [], errors: 0 } Thanks for any info you can provide!

JSON.parse SyntaxError when using GMAIL API

Hi,

Great package btw

If invalid json is returned in the part.data.body then JSON.parse throws an error. It should be wrapped in a try catch. Example:

//  Parse response JSON
if (part.data.body) {
  try {
	  part.data.body = JSON.parse(part.data.body);
  } catch (e) {
	  console.error(e); 
  }
}

The error was happening when I was trying to label a old gmail message. It works most of the time. Google must be returning something strange in certain instances.

"err": {
        "message": "Unexpected token N in JSON at position 0",
        "name": "SyntaxError",
        "stack": "SyntaxError: Unexpected token N in JSON at position 0\n    at JSON.parse (<anonymous>)\n    at PartStream.<anonymous> (/src/node_modules/batchelor/lib/batchelor.js:223:33)\n    at emitNone (events.js:91:20)\n    at PartStream.emit (events.js:186:7)\n    at endReadableNT (_stream_readable.js:974:12)\n    at _combinedTickCallback (internal/process/next_tick.js:74:11)\n    at process._tickDomainCallback (internal/process/next_tick.js:122:9)"
    }

To replicate yourself, change the following on line 223

JSON.parse('make this bad json' + part.data.body);

Hope you able to fix. I can raise a PR if needed, but felt the change was simple enough to be done by established committers.

Regards

Dan

Updated to latest, getting error parsing batch response from gmail

Hey guys, I just switched to latest to get the concurrency fix, but getting this:

/server/auth/node_modules/batchelor/lib/batchelor.js:167
boundary = boundaryRegex.exec(responseObj.headers['content-type'])[2],
^
TypeError: Cannot read property '2' of null
at Request. (/server/auth/node_modules/batchelor/lib/batchelor.js:167:73)
at Request.emit (events.js:107:17)
at Request.onRequestResponse (/server/auth/node_modules/request/request.js:1139:10)
at ClientRequest.emit (events.js:107:17)
at HTTPParser.parserOnIncomingClient as onIncoming
at HTTPParser.parserOnHeadersComplete (_http_common.js:111:23)
at TLSSocket.socketOnData (_http_client.js:310:20)
at TLSSocket.emit (events.js:107:17)
at readableAddChunk (_stream_readable.js:163:16)
at TLSSocket.Readable.push (_stream_readable.js:126:10)

I have verfied the content-type header is present and valid. If I switch back to older version of Batchelor and make same request, it parses fine.

Getting response { parts: [], errors: 0 } regardless of what token we use for Bearer

This may not be a bug on your part, just wondering if you've seen this? We've been using the access_token acquired by the usual oauth2 exchange auth code for tokens flow, server side. Is this even the correct token to use here? It seems no matter what we put in for Bearer we get this response. I would expect a 400 or 401 response, not { parts: [], errors: 0 } Thanks for any info you can provide!

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: Cannot find preset's package (local>mixmaxhq/renovate-config)

Getting response { parts: [statusCode: '401', statusMessage: 'Unauthorized',], errors: 0 } with valid token for analytics

Hello,
If i'm using the following code, everything is ok:

const google = require('googleapis');
const OAuth2Client = google.auth.OAuth2;
const tokenGA = {
    "access_token": "ya29.XXXX",
    "expires_in": 3600,
    "refresh_token": "YYYY",
    "token_type": "Bearer",
    "created": 1485874552
};
const oAuth2ClientGA = new OAuth2Client(webGA.client_id, webGA.client_secret, webGA.redirect_uris[0]);
const analytics = google.analytics({version: 'v3',auth: oAuth2ClientGA});
oAuth2ClientGA.setCredentials(tokenGA);
analytics.data.ga.get({
        'ids': VIEW_ID,
        'start-date': '14daysAgo',
        'end-date': 'today',
        'metrics': 'ga:users,ga:sessions,ga:bounceRate,ga:avgSessionDuration,ga:goalCompletionsAll,ga:pageviewsPerSession,ga:uniquePageviews,ga:goalConversionRateAll',
        'dimensions': 'ga:date,ga:hour,ga:channelGrouping',
        'filters': 'ga:date=~' + _today + '|' + _1weekAgo + '|' + _2weeksAgo + ';ga:sessions>0'
    }, function (err, response) {
        if (err) {
            next(err);
        }
        res.send(response);
    });

But with Batchelor i always get an error
This is the code i used:
```
var Batchelor = require('batchelor');

var batch = new Batchelor({
    'uri':'https://www.googleapis.com/batch',
    'method':'POST',
    'auth': {
        'bearer': tokensGA.access_token
    },
    'headers': {
        'Content-Type': 'multipart/mixed'
    }
});

batch.add({
    'method':'GET',
    'path':'/analytics/v3/data/ga?ids='+ VIEW_ID+ '&start-date=14daysAgo&end-date=today&metrics=ga:users,ga:sessions,ga:bounces,ga:sessionDuration,ga:goalCompletionsAll,ga:pageviewsPerSession,ga:uniquePageviews,ga:pageViews&dimensions=ga:date,ga:hour&filters=ga:date=~' + _today + '|' + _1weekAgo + '|' + _2weeksAgo + ';ga:sessions>0'
});
I'm getting the following response:

{ parts:
[ { statusCode: '401',
statusMessage: 'Unauthorized',
headers: [Object],
body: [Object] } ],
errors: 0 }


Thanks in advance for any help you can provide, really appreciate it.

Concurrency issues?

I'm running a HTTP server that accepts user requests. So I'm trying to use multiple instances of Batchelor to service different users. However, the API is entirely singleton, so calling Batchelor.init() with a different user will overwrite the previous call.

What's your recommended way to create two instances of Batchelor at the same time?

Potential crashes with "dicer" dependency

Hello,

Using dicer dependency may result in crashes, according to npm when running npm install with batchelor 2.0.2 in package.json:

dicer  *
Severity: high
Crash in HeaderParser in dicer - https://github.com/advisories/GHSA-wm7h-9275-46v2
No fix available
node_modules/dicer
  batchelor  *
  Depends on vulnerable versions of dicer
  node_modules/batchelor

Please check the corresponding issue opened in dicer repo. A PR on dicer is opened since more than one year, a quick fix should not be expected on that side...

Mocking batchelor requests with Nock

Hey @jamesmhaley ๐Ÿ‘‹ thanks for your great work on this library!

The official Node JS Google client still doesn't support the batching endpoint ๐Ÿ˜ž so there's still a need for a lib such as this.

Currently, we use the Node JS Google client to interact with the the Google Calendar API. I've been using Nock to mock responses in our tests. Nock is also used for the tests in the official Google client too.

Unfortunately, Nock doesn't seem to pick up requests made by batchelor. I can see batchelor is using the request lib, which seems as though it should be compatible with nock.

Do you have any suggestions as to why Nock isn't picking up batchelor's requests?

I can provide an example to reproduce if that's helpful.

As an aside, how actively are you looking to maintain this lib? Would be keen to make some contributions if this is still being actively worked on! ๐Ÿ‘

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.