Papi.js offers a Promise based abstraction for accessing the Pressly API.
Papi.js will work on all modern browsers.
Papi.js uses CORS to make cross-origin requests. Only IE10+ have a proper implementation of CORS.
Included is an XDomain distribution of Papi.js which will work on older browsers that do not properly support CORS (IE8 and IE9) located at dist/papi.xdomain.js
.
NOTE Because IE8 is ECMASCRIPT version 3 You will need to include es5-shim and es5-sham as well https://github.com/es-shims/es5-shim
Releases can be downloaded here https://github.com/pressly/papi.js/releases you can then script include /dist/papi.js in your project
<script type="text/javascript" src="papi.js"></script>
This will expose the Papi
class globally.
- Login to https://beta.pressly.com
- View docs at: https://beta-api.pressly.com/docs
- Login to https://beta.pressly.com
- Visit https://beta-api.pressly.com/auth/session and copy the
jwt
string
- options* (optional) | Object | see below
Most of the time you can simply create a new api instance from Papi without specifying any options:
var api = new Papi();
You can also optionally specify options.
- host | String | defaults to 'https://beta-api.pressly.com'
- timeout | Integer | Global timeout in ms for all requests
var api = new Papi({ host: 'https://dev-api.pressly.com', timeout: 20000 });
All authentication is handled under the Papi.auth module.
- email (required) | String
- password* (required) | String
Returns Promise which resolves session response.
api.auth.login(email, password).then(function(session) {
// Returns logged in session
});
Returns promise for successful logout.
api.auth.logout(email, password).then(function() {
// User is successfully logged out
});
Returns promise which resolves the current session.
This will attempt to pass an authentication cookie if one exists to retreive the logged in session.
api.auth.get().then(function(session) {
// Returns current session if already logged in
});
- session (required) | Object | a stored session object.
You may wish to store the api.auth.session
object on the client and restore it at a later time.
api.auth.set(session);
Before we can fetch data we need to set up the appropriate resource we wish to query.
- key (required) | String | Key of the resource eg. 'hubs.assets'
- params (optional) | Object | eg.
{ hubId: 123 }
Returns a prepared Resource.
var resource = api.$resource('hubs'); // returns a Hubs Resource
When directly specifying nested resources we use the format parent.child
starting from the root.
var resource = api.$resource('hubs.assets'); // returns an Assets Resource
In cases where we are creating a nested resource we will need to specify the ids of the parents. This can be done at the time you request data from the resource via the all
or find
methods or you can prepare the resource with default params when you create it.
Note The param names for parent ids take the form {singular parent name }Id
ie. hubs -> hubId
var resource = api.$resource('hubs.assets', { hubId: 123 }); // returns an Assets Resource with set hub id
Now this resource will be setup to return assets from hub '123'.
Here is an example of preparing a resource that is nested 3 deep. The styles resource.
var resource = api.$resource('hubs.apps.styles', { hubId: 123, appId: 456 }); // returns a Styles Resource with set hub and app id
You can additionally set modifiers on the resource like limiting the number of results, or setting query params.
- rpp (required) | Integer | Requests per page, Number or results to return for
all
and custom actions
Returns the resource.
resource.limit(15);
- params (required) | Object | Query params that will be set on the request ie.
{ q: 1, b: 2 }
->?q=1&b=2
Returns the resource.
resource.query({ q: 1, b: 2 });
Because modifiers return the current resource you can chain them like so:
resource.limit(15).query({ q: 1, b: 2 });
Specify a maximum timeout before an error is triggered for requests on this resource
resource.timeout(20000);
Requesting data is done by the all
and find
methods on a Resource.
- params (optional) | Object | ex.
{ hubId: 123 }
params will override anything set in the resource.
Returns a Promise
which resolves an Array of result models.
resource.$all().then(function(hubs) {
...
});
- params (optional) | Object | ex.
{ slug: 'some-article-slug' }
params will override anything set in the resource.
returns a Promise
which resolves a result model.
resource.$get({ slug: 'some-article-slug' }).then(function(hub) {
...
});
- id (optional) | Integer | ex
123
- params (optional) | Object | ex.
{ hubId: 123, id: 1 }
params will override anything set in the resource.
returns a Promise
which resolves a result model.
resource.$find(123).then(function(hub) {
...
});
Result models are extended with the resource that generated it so you can
access $resource
to generate child resources.
api.$resource('hubs').$find(123).then(function(hub) {
hub.$resource('apps').$all().then(function(apps) {
..
});
});
Notice that when you chain queries you specify the child name hubs
rather than the full resource key hubs.apps
.
This is equivalent to:
api.$resource('hubs').$find(123).then(function(hub) {
api.$resource('hubs.apps', { hubId: hub.id }).$all().then(function(apps) {
..
});
});
Result models have methods to facilitate saving, deleting, updating, etc.
api.$resource('hubs').$find(123).then(function(hub) {
hub.name = "My First Hub";
hub.$save().then(() => {
});
});
Collections are Arrays of Models which are extended with helper functions.
Collections are produced by the Resource::$all
method.
var collection;
api.$resource('hubs').$all().then(function(hubs) {
collection = hubs.findWhere({ name: "My First Hub"});
..
});
- id (optional) | Integer | ex
123
returns a Model
.
api.$resource('hubs').$all().then(function(hubs) {
var model = hubs.findWhere({ name: "My First Hub"});
..
});
Below outlines the schema of all the RESTful resources, their routes, associated model, and child resources.
###Account
accounts
#####Children
#####REST Endpoints
GET
/accountsPOST
/accountsGET
/accounts/:idPUT
/accounts/:idDELETE
/accounts/:id
Member Actions
POST
/accounts/:id/become
###User
accounts.users
#####Parent
- Account
accounts
#####REST Endpoints
GET
/accountsPOST
/accountsGET
/accounts/:accountId/users/:idPUT
/accounts/:accountId/users/:idDELETE
/accounts/:accountId/users/:id
###Hub
accounts.hubs
#####Parent
- Account
accounts
See Hub hubs
#####REST Endpoints
GET
/accountsPOST
/accountsGET
/accounts/:accountId/hubs/:idPUT
/accounts/:accountId/hubs/:idDELETE
/accounts/:accountId/hubs/:id
###Hub
hubs
#####Children
- App
hubs.apps
- Model
hubs.analytics
- Feed
hubs.feeds
- Invite
hubs.invites
- Recommendation
hubs.recommendations
- User
hubs.users
- Collection
hubs.collections
- Tag
hubs.tags
- Asset
hubs.assets
- Draft
hubs.drafts
#####REST Endpoints
GET
/hubsPOST
/hubsGET
/hubs/:idPUT
/hubs/:idDELETE
/hubs/:id
Collection Actions
GET
/hubs/search
Member Actions
POST
/hubs/:id/upgradePOST
/hubs/:id/accept_invitePOST
/hubs/:id/reject_invite
###App
hubs.apps
#####Parent
- Hub
hubs
#####Children
- Style
hubs.apps.styles
#####REST Endpoints
GET
/hubsPOST
/hubsGET
/hubs/:hubId/apps/:idPUT
/hubs/:hubId/apps/:idDELETE
/hubs/:hubId/apps/:id
Collection Actions
GET
/hubs/current
Member Actions
GET
/hubs/:hubId/apps/:id/buildGET
/hubs/:hubId/apps/:id/status
###Style
hubs.apps.styles
#####Parent
- App
hubs.apps
#####REST Endpoints
GET
/hubsPOST
/hubsGET
/hubs/:hubId/apps/:appId/styles/:idPUT
/hubs/:hubId/apps/:appId/styles/:idDELETE
/hubs/:hubId/apps/:appId/styles/:id
###Model
hubs.analytics
#####Parent
- Hub
hubs
#####REST Endpoints
GET
/hubsPOST
/hubsGET
/hubs/:hubId/analytics/:idPUT
/hubs/:hubId/analytics/:idDELETE
/hubs/:hubId/analytics/:id
Collection Actions
GET
/hubs/summaryGET
/hubs/visitorsGET
/hubs/pageviewsGET
/hubs/duration
###Feed
hubs.feeds
#####Parent
- Hub
hubs
#####Children
- FeedAsset
hubs.feeds.assets
#####REST Endpoints
GET
/hubsPOST
/hubsGET
/hubs/:hubId/feeds/:idPUT
/hubs/:hubId/feeds/:idDELETE
/hubs/:hubId/feeds/:id
###FeedAsset
hubs.feeds.assets
#####Parent
- Feed
hubs.feeds
#####REST Endpoints
GET
/hubsPOST
/hubsGET
/hubs/:hubId/feeds/:feedId/assets/:idPUT
/hubs/:hubId/feeds/:feedId/assets/:idDELETE
/hubs/:hubId/feeds/:feedId/assets/:id
###Invite
hubs.invites
#####Parent
- Hub
hubs
#####REST Endpoints
GET
/hubsPOST
/hubsGET
/hubs/:hubId/invites/:idPUT
/hubs/:hubId/invites/:idDELETE
/hubs/:hubId/invites/:id
Collection Actions
POST
/hubs/bulk_invite
Member Actions
POST
/hubs/:hubId/invites/:id/resendPOST
/hubs/:hubId/invites/:id/acceptPOST
/hubs/:hubId/invites/:id/reject
###Recommendation
hubs.recommendations
#####Parent
- Hub
hubs
#####REST Endpoints
GET
/hubsPOST
/hubsGET
/hubs/:hubId/recommendations/:idPUT
/hubs/:hubId/recommendations/:idDELETE
/hubs/:hubId/recommendations/:id
###User
hubs.users
#####Parent
- Hub
hubs
#####REST Endpoints
GET
/hubsPOST
/hubsGET
/hubs/:hubId/users/:idPUT
/hubs/:hubId/users/:idDELETE
/hubs/:hubId/users/:id
Collection Actions
POST
/hubs/grant_access
Member Actions
DELETE
/hubs/:hubId/users/:id/revoke_access
###Collection
hubs.collections
#####Parent
- Hub
hubs
#####REST Endpoints
GET
/hubsPOST
/hubsGET
/hubs/:hubId/collections/:idPUT
/hubs/:hubId/collections/:idDELETE
/hubs/:hubId/collections/:id
Collection Actions
PUT
/hubs/reorder
###Tag
hubs.tags
#####Parent
- Hub
hubs
#####REST Endpoints
GET
/hubsPOST
/hubsGET
/hubs/:hubId/tags/:idPUT
/hubs/:hubId/tags/:idDELETE
/hubs/:hubId/tags/:id
###Asset
hubs.assets
#####Parent
- Hub
hubs
#####Children
#####REST Endpoints
GET
/hubsPOST
/hubsGET
/hubs/:hubId/stream/:idPUT
/hubs/:hubId/stream/:idDELETE
/hubs/:hubId/stream/:id
Member Actions
PUT
/hubs/:hubId/stream/:id/featurePUT
/hubs/:hubId/stream/:id/unfeaturePUT
/hubs/:hubId/stream/:id/hidePUT
/hubs/:hubId/stream/:id/unhidePUT
/hubs/:hubId/stream/:id/lockPUT
/hubs/:hubId/stream/:id/unlock
###Like
hubs.assets.likes
#####Parent
- Asset
hubs.assets
#####REST Endpoints
GET
/hubsPOST
/hubsGET
/hubs/:hubId/stream/:assetId/likes/:idPUT
/hubs/:hubId/stream/:assetId/likes/:idDELETE
/hubs/:hubId/stream/:assetId/likes/:id
###Comment
hubs.assets.comments
#####Parent
- Asset
hubs.assets
#####REST Endpoints
GET
/hubsPOST
/hubsGET
/hubs/:hubId/stream/:assetId/comments/:idPUT
/hubs/:hubId/stream/:assetId/comments/:idDELETE
/hubs/:hubId/stream/:assetId/comments/:id
###Draft
hubs.drafts
#####Parent
- Hub
hubs
#####REST Endpoints
GET
/hubsPOST
/hubsGET
/hubs/:hubId/drafts/:idPUT
/hubs/:hubId/drafts/:idDELETE
/hubs/:hubId/drafts/:id
Member Actions
PUT
/hubs/:hubId/drafts/:id/publish
###CodeRevision
code_revisions
#####Children
- Hub
code_revisions.hubs
#####REST Endpoints
GET
/code_revisionsPOST
/code_revisionsGET
/code_revisions/:idPUT
/code_revisions/:idDELETE
/code_revisions/:id
Member Actions
GET
/code_revisions/:id/fetch_repo
###Hub
code_revisions.hubs
#####Parent
- CodeRevision
code_revisions
See Hub hubs
#####REST Endpoints
GET
/code_revisionsPOST
/code_revisionsGET
/code_revisions/:code_revisionId/hubs/:idPUT
/code_revisions/:code_revisionId/hubs/:idDELETE
/code_revisions/:code_revisionId/hubs/:id
###Model
signup
#####REST Endpoints
GET
/signupPOST
/signupGET
/signup/:idPUT
/signup/:idDELETE
/signup/:id
Member Actions
GET
/signup/:id/account_uid_availableGET
/signup/:id/account_email_available
###User
users
#####REST Endpoints
GET
/usersPOST
/usersGET
/users/:idPUT
/users/:idDELETE
/users/:id
Collection Actions
GET
/users/roles