Giter VIP home page Giter VIP logo

node-sfu-photos-client's Introduction

node-sfu-photos-client

node-sfu-photos-client is a module to interface with the SFU ID Photos API.

Access

Access to the SFU Photos API requires a username and password. Contact Danny Louie <[email protected]> for access.

Installation

npm install "git+https://github.com/sfu/node-sfu-photos-client.git"  --save

Tests

To run tests: npm test

Tests require:

  • a ./test/config.json file
  • fixture data:
    • 1user.json
    • 2users.json
    • 10users.json
    • 11users.json

Fixture data is not checked into the project because it contains personal information. The JSON files are simply the result of calling the API for x users (1, 2, 10 or 11).

Usage

node-sfu-photos-client provides both a node-style (err, results) callback interface, as well as a promise interface (using Q).

Callback-style interface

// require the module in your application
var PhotoClient = require('node-sfu-photos-client');

// create a new instance of the client
var client = new PhotoClient({
    username: "photoapiuser",
    password: "supersecretpassword",
    endpoint: "https://photos-api-server",
    maxPhotosPerRequest: 10
});

// retrieve the photo for a single SFU ID
client.getPhoto(["123456789"], function(err, photo) {
    console.log(photo);
});

// retrieve the photos for multiple SFU IDs
client.getPhoto(["123456789", "987654321"], function(err, photos) {
    console.log(photos);
});

Promise-style interface

// require the module in your application
var PhotoClient = require('node-sfu-photos-client');

// create a new instance of the client
var client = new PhotoClient({
    username: "photoapiuser",
    password: "supersecretpassword",
    endpoint: "https://photos-api-server",
    maxPhotosPerRequest: 10
});

// retrieve the photo for a single SFU ID
client.getPhoto(["123456789"]).then(function(photos) {
    console.log(photos);
});

// retrieve the photos for multiple SFU IDs
client.getPhoto(["123456789", "987654321"]).then(function(photos) {
    console.log(photos);
});

Configuration Options

Client configuration is handled by a configuration object passed when initializing a new PhotoClient. See config.json.example for a sample configuration.

Required Options

  • username: your username for the SFU Photos API
  • password: your password for the SFU Photos API
  • endpoint: the base URL for the SFU Photos API
  • maxPhotosPerRequest: Photos API tokens are limited to a certain number of photos per GET request. This number will be provided to you when you are granted access to the API.

Image Resizing

Images returned by the API are of variable dimensions. You can pass a maxWidth property in the configuration; if present, the images will be scaled down to that width (using the lwip module).

Caching

Photos can be cached to help alleviate load on the Photo API server. By default, no caching is performed.

A redis caching library is included with this module. To use redis caching, include the following in your configuration (see config.json.example for a full configuration):

"cache": {
    "store": "redis",
    "options": {
        // redis options
    }
}

By default, the following options are used. They can all be overridden, and additional node-redis connection options included in redisOptions:

    "options": {
        "redisHost": "localhost",
        "redisPort": 6379,
        "redisDatabase": 0,
        "redisOptions": {
          "detect_buffers": true
        },
        "photoExpiry": 3600,
        "tokenExpiry": 86400,
        "photoPrefix": "photo:",
        "tokenKey": "token"
    }
}

If a redis password is required, specify it with redisPassword: "password".

If caching is enabled, the authentication token will also be cached. It is recommended to cache the token for a duration less than its actual lifetime. For example, if your token is good for one hour, you may want to cache the token for 55 minutes. The redis cache store uses expiring keys (SETEX) for both tokens and photos.

Implementing alternative cache stores

You can implement and use alternative cache stores. For example, a fictional MongoDB store:

var config = {
    cache: {
        store: require('mongo-cache-store'),
        options: { /* cache-specific options... */ }
    }
}
var client = new PhotoClient(config);

Cache stores return promises (using Q) from their public APIs.

All schemes must implement the following public APIs:

CacheStore#getPhotos(ids)

Retreive one or more photos from the cache.

  • ids: an array of SFU IDs

CacheStore#setPhotos(photos)

Store one or more photos in the cache.

  • photos: an array of photo data to cache

CacheStore#getToken(callback)

Retrieve the authentication token from the cache.

CacheStore#setToken(token)

Store the authentication token in the cache.

  • token: the auth token

CacheStore#flush()

Remove all entries from the cache

Refer to cache_stores/redis.js for an example.

node-sfu-photos-client's People

Contributors

grahamb avatar greenkeeperio-bot avatar

Stargazers

Diliara avatar

Watchers

 avatar James Cloos 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.