Code Monkey home page Code Monkey logo

divshot-api's Introduction

Divshot API

Wrapper for the Divshot API.

Contents

Install

NPM

npm install divshot-api --save

Bower

bower install divshot --save

Usage

Refer to the Narrator api for a more in depth understanding of all available methods.

CommonJS (Node/Browserify)

var Divshot = require('divshot');

Standalone

<script src="/path/to/divshot.standalone.min.js"></div>

###Instantiate

var api = Divshot.createClient({
  client_id: '123abc', // MUST be specified
  
  email: '[email protected]',
  password: 'somepassword123!',
  
  // OR
  
  token: 'your divshot access token'
});

###Browser Authentication

The Divshot API wrapper provides a simple, popup-based authentication mechanism for easily authenticating a user without requiring the handling of usernames or passwords. To use browser authentication, simply instantiate a client and call the auth method:

divshot.auth(type, options, callback)

This method returns a promise but takes an optional callback. type should be either password or github (defaults to password).

var api = Divshot.createClient({client_id: 'abc123'});

divshot.auth().then(function(response) {
  response.user; // user details
  response.access_token; //access token
}, function (error) {
  // do something with an error
});

Password Authentication

If you need to accept an email and password directly, you can do so as in the following example. Passwords MUST NOT be stored and should only come from direct user input (e.g. a form field).

divshot.auth('password', {email: '[email protected]', password: 'test123'}).then(function(response) {
  // works just as above
}, function (error) {
  // ditto
});

Cookie Token Storage

For convenience, the auth method allows you to store a cookie with an encoded access token to keep the user logged into Divshot. Simply pass the store: true option to auth:

api.auth({store: true}, function(error, token, user){
  // ...
});

This will automatically create a cookie on the current domain to store the access token for one week. On subsequent page loads you can use the authWithCookie() method to authenticate a client based on the cookie. This method will return true if a cookie was found and false otherwise.

if (api.authWithCookie()) {
  // cookie found, api is now authenticated and can
  // make calls to retrieve protected resources
} else {
  // no cookie found, display an auth button etc
}

###Angular Module

Located at /dist/divshot.angular.js

angular.module('myApp', ['divshot'])
  .config(function (divshotProvider) {
    divshotProvider.configure({
      token: 'divshot_api_access_token'
    });
  }).
  controller('SomeCtrl', function ($scope, divshot) {
    
    $scope.apps = divshot.apps.list();
    
  });

Custom XHR arguments

Custom XHR arguments can be set to be sent with each request. Refer to Angular's $http documentation for which arguments are supported.

angular.module('myApp', ['divshot'])
  .config(function (divshotProvider) {
    divshotProvider.configure({
      token: 'divshot_api_access_token'
    });
  }).
  controller('SomeCtrl', function ($scope, divshot) {
    divshot.withCredentials(true);
    // or
    divshot.xhr('withCredentials', true);
  });

###User

By default, the authenticate method will be called on each request as a pre hook. If a token is provided, this does not create another http request.

api.user.authenticate(function (err, token) {
  
});

api.user.setCredentials({
  email: '[email protected]',
  password: 'somepassword123!',
  
  // OR
  
  token: 'some_really_long_access_token_from_divshot'
});

// User data
api.user.self().then(function (user) {
  
});
// OR
api.user.id(userId).get().then(function (user) {
  
});

// Update user
var user = api.user.id(userId);
user.update({
  name: 'First Last'
}).then(function (user) {
  
});

// Set welcomed for new users
api.user.setWelcomed().then(function () {

});

// Delete account
api.user.deleteAcccount(user@email.com).then(function () {
  
});

User password

// Change password
divshot.user.password.update({
  password: 'Password123',
  password_confirm: 'Password123'
}).then(function (res) {
  
});

// Reset password if forgotten
divshot.password.reset(userId).then(function (res) {
  
});

User Emails

// Add email
divshot.user.emails.add('[email protected]').then(function (res) {
  
});

// Set primary email
divshot.user.emails.primary('[email protected]').then(function (res) {
  
});

// Remove email
divshot.user.emails.remove('[email protected]').then(function (res) {
  
});

// Resend email
divshot.user.emails.resend('[email protected]').then(function (res) {
  
});

Organizations

// Users orgs
divshot.organizations.list().then(function (orgs) {
  
});

// A single organization
divshot.organizations.id(someOrgId).get().then(function (org) {
  
});

// Apps from an organization
divshot.organizations.id(someOrgId).apps.list().then(function (apps) {
  
});

// Create organization
divshot.organizations.create({
  name: 'name',
  nick: 'nick',
  billing_email: '[email protected]',
  gravatar_email: '[email protected]'
}).then(function (res) {
  
});

// Update organization
divshot.organizations.id(someOrgId).update({
  name: 'name',
  billing_email: '[email protected]',
  gravatar_email: '[email protected]',
  etc: 'other stuff'
}).then(function (res) {
  
});

Organization Members

// Get org members
divshot.organizations.id(someOrg).members.list().then(function (members) {
  
});

// Invite members to organization
divshot.organizations.id(someOrg).members.create({
  name: email,
  email: email
}).then(function (res) {
  
});

// Update a member in organiztion
divshot.organizations.id(someOrg).members.id(memberid).update({
  admin: false // or true
}).then(function (res) {
  
});

// Remove a member from an organization
divshot.organizations.id(someOrg).members.id(memberId).remove().then(function () {
  
});

Apps

// List apps
api.apps.list().then(function (apps) {
  
});

// Create an app
api.apps.create('app-name').then(function (app) {
  
});

// Create an app from object
api.apps.createFromObject(params).then(function (app) {
  
});

// A specific app
var app = api.apps.id('app name');
app.get().then(function (app) {
  
});

// Delete an app
app.remove().then(function (res) {

});

Builds

app.builds.list(function (err, builds) {
  
});

app.builds.id('build id').get(function (err, build) {
  // Specific build by id
});

app.builds.id('build id').finalize(function (err, response) {
  
});

app.builds.id('build id').release('production', function (err, response) {
  
});

###Releases

app.releases.list(function (err, releases) {
  
});

app.releases.env('production').get(function (err, release) {
  // Release by environment
});

app.releases.env('production').rollback(function (err, response) {
  
});

// Rollback to specific version
app.releases.env('production').rollbackTo('v12').then(function () {
  
});

app.releases.env('production').promote('staging', function (err, callback) {
  // Here, "staging" is the from environment
  // and "production" is the to-environment
});

###Domains

// List domains for an app
app.domains.list().then(function(domains) {
  
});

// Add a domain
app.domains.add('www.domain.com').then(function (response) {
  
});

// Remove a domain
app.domains.remove('www.domain.com').then(function (response) {
  
});

Subscription

Update data associated with an App's subscription

app.subscription.update('card number').then(function (res) {
  
});

###App Environment Configuration

app.env('development').config({
  auth: 'username:password'
}, function (err, response) {
  
});

Build

grunt build

divshot-api's People

Contributors

collin avatar fastdivision avatar mbleigh avatar scottcorgan avatar

Watchers

 avatar  avatar

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.