Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade and tests #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
language: node_js
node_js:
- "node"
43 changes: 25 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,40 @@
# gapi-client
[![Build Status](https://travis-ci.org/kevinschaich/gapi-client.svg?branch=master)](https://travis-ci.org/kevinschaich/gapi-client)

A *very* thin Node wrapper for Google's client-side javascript V3 API.
A *very* thin Node wrapper for Google's client-side javascript V3 API. For `gapi` documentation, see [Google API Client Reference](https://developers.google.com/api-client-library/javascript/reference/referencedocs) and for `API_KEY`, see [Google Developers Console](https://console.developers.google.com/).

## Example
## Install

```bash
npm install --save gapi-client
```

`index.js:`
## Example

```javascript
import gapi from 'gapi-client';

//On load, called to load the auth2 library and API client library.
gapi.load('client:auth2', initClient);

// Initialize the API client library
function initClient() {
gapi.client.init({
discoveryDocs: ["https://www.googleapis.com/discovery/v1/apis/drive/v3/rest"],
clientId: 'YOUR_CLIENT_ID',
scope: 'https://www.googleapis.com/auth/drive.metadata.readonly'
}).then(function () {
// do stuff with loaded APIs
console.log('it worked');
});
import gapi from 'gapi-client'

// Example : Auth2 (synchronous)
const init = () => gapi.client.init(
discoveryDocs: ['https://www.googleapis.com/discovery/v1/apis/drive/v3/rest'],
clientId: 'YOUR_CLIENT_ID',
scope: 'https://www.googleapis.com/auth/drive.metadata.readonly'
})

gapi.load('client:auth2', init)

// Example : Youtube (synchronous)
const init = {
client: () => gapi.client.load('youtube', 'v3', init.youtube),
youtube: () => gapi.client.setApiKey('YOUR_API_KEY')
}

gapi.load('client', init.client)

// Example : Youtube (promisified)
new Promise(resolve => gapi.load('client', resolve))
.then(() => new Promise(resolve => gapi.client.load('youtube', 'v3', resolve)))
.then(() => gapi.client.setApiKey('YOUR_API_KEY'))
```

## License
Expand Down
12 changes: 6 additions & 6 deletions index.js

Large diffs are not rendered by default.

14 changes: 12 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "A very thin Node wrapper for Google's client-side javascript V3 API",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"upgrade": "node upgrade.js",
"test": "./node_modules/mocha/bin/mocha -r jsdom-global/register"
},
"repository": {
"type": "git",
Expand All @@ -21,5 +22,14 @@
"bugs": {
"url": "https://github.com/kevinschaich/gapi-client/issues"
},
"homepage": "https://github.com/kevinschaich/gapi-client#readme"
"homepage": "https://github.com/kevinschaich/gapi-client#readme",
"devDependencies": {
"chai": "^3.5.0",
"jsdom": "^9.12.0",
"jsdom-global": "^2.1.1",
"md5": "^2.2.1",
"mocha": "^3.3.0",
"node-fetch": "^1.6.3"
},
"dependencies": {}
}
33 changes: 33 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const expect = require('chai').expect
const fetch = require('node-fetch')
const md5 = require('md5')
const fs = require('fs')
const gapi = require('./index.js')

describe('gapi', function(){
this.timeout(10000)

// impossible : travis-ci get different checksum as local : 272a52e59d1f98a4bf2409e77a029b62 - d3b6759a32936b3951340c194d448a70 (05/06/17)
xit('should have same md5 checksum as [distant](https://apis.google.com/js/api.js)', function(){
var local = fs.readFileSync('index.js')

// repeat n times because api.js can give different script versions (and so diffrent md5)
return Promise.all(Array.from(Array(10).keys())
.map(i => new Promise(resolve => setTimeout(resolve, i * 500))
.then(() => fetch('https://apis.google.com/js/api.js'))
)
)
.then(responses => Promise.all(responses.map(response => response.text())))
.then(contents => contents.map(content => content + "\n\nmodule.exports = gapi;"))
.then(distants => distants.map(distant => md5(distant)))
.then(checksums => expect(md5(local), JSON.stringify(checksums)).to.be.oneOf(checksums))
})

it('should have a load property', function(){
expect(gapi).to.have.property('load')
})

it('should be able to load client', function(done){
gapi.load('client', done)
})
})
19 changes: 19 additions & 0 deletions upgrade.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const fetch = require('node-fetch')
const md5 = require('md5')
const fs = require('fs')

fetch('https://apis.google.com/js/api.js')
.then(response => response.text())
.then(content => content + "\n\nmodule.exports = gapi;")
.then(distant => {
var local = fs.readFileSync('index.js')
var checksum = md5(distant)

if (checksum !== md5(local)) {
fs.writeFileSync('index.js', distant)
console.log('Writing new version :', checksum)
} else {
console.log('Current version is up to date !')
}
})
.catch(error => console.log('Error during download :', error.message))