About
Javascript SDK for api.six.se
Usage
var TOKEN = 'token-authenticated-in-backend'
var session = Six.connect(TOKEN)
var unsubscribe = session.subscribe('/listings/848',function(err,data) {
console.log('data',data,err)
})
Getting started
Installing from NPM
Install six-sdk
in project
Require the module as usual:
var Six = require('six-sdk')
var session = Six.connect('token-from-backend')
Installing from CDN
<html>
<body>
<script type="text/javascript" src="https://cdn.six.se/js/six-sdk/0/six-sdk.js"></script>
<script type="text/javascript">
var session = Six.connect('token-from-backend')
</script>
</body>
</html>
Global scope
When using the SDK from cdn.six.se the SDK is installed as Six
in the global scope. If there is a risk that this name will clash with some other var, you can change the name of the global by setting SIX_GLOBAL_NAMESPACE
before loading the SDK:
<html>
<body>
<script type="text/javascript">
window.SIX_GLOBAL_NAMESPACE = 'VendorSix'
</script>
<script type="text/javascript" src="https://cdn.six.se/js/six-sdk/0/six-sdk.js"></script>
<script type="text/javascript">
var session = VendorSix.connect('token-from-backend')
</script>
</body>
</html>
API
Before using the any of the methods in the SDK you must retrieve an authentication token from api.six.se (see the documentation for the API). This is usually done in some backend system and provided as an global var in the HTML document returned to the browser.
Session
All methods to retrive data from the API is attached to the Session object.
- subscribe(resource,callback(error,data,unsubsribe)) -> unsubscribeFn
- refresh(resource)
- create(resource,data) -> Promise
- update(resource,data) -> Promise
- remove(resource) -> Promise
- clearCache()
Create a session
To create a new session object:
var session = Six.connect(TOKEN)
Some of the functionality and analytics in the API requires some additional context (ie userid, entitlement groups etc).
To create a session with additional context:
var session = Six.connect(TOKEN).withContext({userId: '16ef65e39e6114fa6d9510042ad83472c9db756a'})
For more information about session contexts, see the API documentation.
Getting data from the API
The primary way to retrive data from api is via subscribe(resource,callbackFn)
The subscribe
method takes a resource (relative to the API endpoint, ie /markets/SEE
not https://api.six.se/v2/markets/SSE
) and a callback function.
The callback function will be called anytime there is new data available for the resource from the SDK (some other component have called refresh
etc). The callback may be called more than once.
The callback can also first be called with an error object, and later with data.
The callback will be called with the arguments:
- error is provided if the request for data couldn't be fullfilled (see section on errors below)
- data the data provided by the api
- unsubscribeFn a function that, when called, unsubscribes the callback from future updates
unsubscribeFn is also returned from the subscribe
call itself
var unsubscribeFn = session.subscribe('/listings/848',function(error,listing, unsubscribeFn) {
})
Error objects
Errors returned from the SDK methods is of the same structure as error responses from the API.
{
code: "AJAX_ERROR",
title: "Request to endpoint failed",
description: "The request failed. Check that the endpoint is valid?",
details: {
endpoint: 'https://api.six.se/v2/'
}
}
Caching
The SDK caches all resources fetched from the API, and all subscribers for the same resource will receive the same data when new data arrives.This means that multiple components on a page using the same underlying data will be kept in sync.
Data is cached until the session is destroyed (typically the next pageview) or session.clearCache()
is called.
Refreshing data
To populate the cache with new data, there is a method session.refresh(resource)
. The API will be called and all subscribers for the resource will have their callbacks called.
session.refresh('/listings/848')
Contact
License
The MIT License (MIT)
Copyright (c) 2016 SIX Financial Information
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.