0.22.7 • Published 4 years ago

restfulpy v0.22.7

Weekly downloads
9
License
MIT
Repository
github
Last release
4 years ago

restfulpy-client.js

Javascript client for restfulpy

NPM npm.io 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: 'user1@example.com', 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

FirefoxChromeIE/EdgeSafariOpera
174112.0928
0.22.7

4 years ago

0.22.6

4 years ago

0.22.5

5 years ago

0.22.4

5 years ago

0.22.2

5 years ago

0.21.16

5 years ago

0.21.15

5 years ago

0.21.14

5 years ago

0.21.13

5 years ago

0.21.12

5 years ago

0.21.11

5 years ago

0.21.10

5 years ago

0.21.9

5 years ago

0.21.8

5 years ago

0.21.7

5 years ago

0.21.6

5 years ago

0.21.5

5 years ago

0.21.3

5 years ago

0.21.2

5 years ago

0.21.1

5 years ago

0.21.0

5 years ago

0.20.0

5 years ago

0.19.5

5 years ago

0.19.4

5 years ago

0.19.3

5 years ago

0.19.1

5 years ago

0.19.0

5 years ago

0.18.4

5 years ago

0.18.3

5 years ago

0.18.2

5 years ago

0.18.1

5 years ago

0.18.0

5 years ago

0.17.0

5 years ago

0.16.2

5 years ago

0.16.1

5 years ago

0.16.0

5 years ago

0.15.1

6 years ago

0.15.0

6 years ago

0.14.1

6 years ago

0.14.0

6 years ago

0.13.1

6 years ago

0.13.0

6 years ago

0.12.2

6 years ago

0.12.1

6 years ago

0.12.0

6 years ago

0.11.2

6 years ago

0.11.1

6 years ago

0.11.0

6 years ago

0.10.1

6 years ago

0.10.0

6 years ago

0.9.2

6 years ago

0.9.1

6 years ago

0.9.0

6 years ago

0.8.0

6 years ago

0.7.10

6 years ago

0.7.9

6 years ago

0.7.8

6 years ago

0.7.7

6 years ago

0.7.6

6 years ago

0.7.5

6 years ago

0.7.4

6 years ago

0.7.0

6 years ago

0.6.1

7 years ago

0.6.0

7 years ago

0.5.6

7 years ago

0.5.5

7 years ago

0.5.4

7 years ago

0.5.2

7 years ago

0.5.1

7 years ago

0.5.0

7 years ago

0.4.2

7 years ago

0.4.1

7 years ago

0.4.0

7 years ago

0.3.1

7 years ago

0.3.0

7 years ago

0.2.0

7 years ago

0.1.0

7 years ago