Skip to content

maryayi/restfulpy-client.js

Repository files navigation

restfulpy-client.js

Javascript client for restfulpy

NPM Build Status Coverage Status License

Install

npm install restfulpy
sudo apt-get install redis-server

Create a postgres database named: restfulpymockupserver

Usage

import { BrowserSession, Authenticator, httpClient, Response } from 'restfulpy'

class LocalAuthenticator extends Authenticator {
  login(credentials) {
    // Add your login method for example:
    return httpClient(
      `http://example.org/api/v1/sessions`,
      {
        verb: 'POST',
        payload: this.constructor.validateCredentials(credentials)
      },
      (...args) => {
        return new Response(null, ...args)
      }
    )
      .then(resp => {
        this.token = resp.json.token
        return Promise.resolve(resp)
      })
      .catch(resp => {
        this.deleteToken()
        return Promise.reject(resp)
      })
  }
}

let authenticator = new LocalAuthenticator()

const errorHandler = {
  401: (status, redirectUrl) => {
    // Your handler for 401 error
    // You can add handler for each status code
  }
}

let client = new BrowserSession(
  'http://example.org/api/v1',
  undefined,
  authenticator,
  errorHandlers
)

// Login
client
  .login({ email: '[email protected]', password: '123456' })
  .then(resp => {
    // Authentication success, so
    console.log(resp.json.token) // is not null
    console.log(client.authenticator.authenticated) // is true
  })
  .catch(resp => {
    throw resp.error
  })

// Posting some data to `http://example.org/api/v1/echo`
client
  .request('echo', 'POST')
  .addParameters({ item1: 'Value1' })
  .send()
  .then(resp => {
    console.log(resp.status) // Is 200
    console.log(resp.json)
    console.log(resp.getHeader('Content-Type'))
  })
  .catch(resp => {
    console.log(resp.status)
    throw resp.error
  })

// Logout
client.logout()

Metadata


client.metadata.ModelName.get(1).done(models => {
  // Use model
})

client.metadata.ModelName.load().done(models => {
  // Use models
})

Development Environment Setup

git clone <repo> && cd into/cloned/repo
npm install
sudo apt-get install redis-server

Build

npm run build

Running tests

An instance of the restfulpy is required as a mock-up server before running tests, see the mockup-server-karma.py and tests directory to understand why you need to setup a python virtual env to run tests.

Adding some stuff into the ~/.bashrc file.

alias v.activate="source /usr/local/bin/virtualenvwrapper.sh"
export VIRTUALENVWRAPPER_PYTHON="$(which python3.6)"

Re-source the ~/.bashrc

source ~/.bashrc

Sourcing the virtualenv wrapper.

v.activate

Creating a dedicated virtualenv for this project.

mkvirtualenv --python=$(which python3.6) restfulpy

Installing the python dependencies

pip install "restfulpy >= 1.1.2a1"

Finally! Running the tests.

npm run test

Or,

npm run test -- --no-single-run

To keep the browser open for more investigation and watch for changes. see karma documents for more info.

Browser Support

Firefox Chrome IE/Edge Safari Opera
17 41 12.0 9 28