GithubHelp home page GithubHelp logo

isabella232 / cloud-foundry-client-js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from 18f/cloud-foundry-client-js

0.0 0.0 0.0 211 KB

A JavaScript client for Cloud Foundry. Supports running in the browser and Node.js.

License: MIT License

JavaScript 94.21% HTML 5.79%

cloud-foundry-client-js's Introduction

Cloud Foundry Client

A JavaScript based Cloud Foundry Api client targeting the v2 api.

This client supports running in the browser via an AMD compatible loader (such as requirejs) and on the server under Node.js

Status

The client is under heavy development so expect some issues. It would be great to see it evolve in to a comprehensive client covering the entire Cloud Foundry Api but initially functionality will be added in the order that we need it for in other projects.

Dependencies

Browser Dependencies

  • RequireJS (or AMD compatible loader)
  • jQuery (exported as 'jquery')
  • Underscore (exported as 'underscore')
  • Event Emitter (exported as 'event-emitter')

Browser dependencies must be pulled in via your AMD loader e.g. if using requirejs you would configure your paths as:

paths:{
    jquery: 'cloud-foundry-client/vendor/jquery/jquery-1.10.1.min',
    underscore: 'cloud-foundry-client/vendor/underscore/underscore-1.4.4.min',
    'event-emitter': 'cloud-foundry-client/vendor/event-emitter/event-emitter.4.0.3.min',
}

You may point these paths at your own copies of the dependencies if you're already using them elsewhere in your project.

Browser dependencies are bundled in the /vendor directory.

Node.js Dependencies

  • Request
  • Underscore (or lodash)

Node.js dependencies are pulled in via NPM

Notes About Cross Domain Requests

Cloud Foundry v2 doesn't support cross domain requests out of the box. Until it does then there are a few ways to work around it:

  1. Run your browser with cross domain security disabled (not recommended outside of development). In Chrome this can be achieved with the --disable-web-security flag.

If you do this you'll notice that Cloud Foundry will throw a 500 internal server error when any API call is made with mismatched host and referrer headers, which makes it impossible to run cross domain requests (even if the browser allows them).

You can patch your cloud_controller_ng by inserting the following code in line 50 of cloud_controller_ng/lib/sinatra/vcap.rb:

# Allow browser based clients to talk to the CC Api across domains
# This is not a full cross domain solution but it prevents Sinatra throwing a 500 error
# when referrer and host headers do not match.
# See https://github.com/sinatra/sinatra/issues/747

app.set :protection, :except => [:remote_referrer, :json_csrf]

  1. Use a reverse proxy to avoid cross domain requests all together.

Tests

Browser Tests

Open tests/test.html

Node.js Tests

Run npm run-script test

Usage

Browser Usage

  1. Load the api object via an AMD module loader (requirejs example below):
    main.js
    
    {
        baseUrl: "js/",
        paths: {
            'cloud-foundry-client': 'lib/cloud-foundry-client'
        }
    }
    

2. Use the api object in your code - note you are responsible for providing the token as well as providing a page to redirect to
that is capable of pulling the token from the url (see ```/examples/oauth.html```):

    ```
    define([
        'cloud-foundry-client/api'],
        function(CFApi){

            var cf_api = new CFApi(api_endpoint, {
                token: Utils.getCookie('cf_token'),
                redirect_uri: html_root + 'oauth.html'
               });

            cf_api.apps.list({
                        paging: {
                            'results-per-page': 1
                        },
                        filter: {
                            name: "instances",
                            value: ">1"
                       }},
                    function (err, page) {
                       if (err) {return console.log(err);}

                        console.log(page.data);

                        if (page.hasNextPage()) {
                            page.getNextPage(function (err, next_page) {
                                ...
                           });
                       }
                    }
               );
    });
    ```

### Node.js Usage

1. ```npm install cloud-foundry``` (or add 'cloud-foundry' as a dependency in your package.json)

2. Use the api object in your code - note that authentication isn't yet supported for Node.js so an expired/invalid token
will bubble out as an error. We would like to add support for refresh tokens and authentication via client credentials
at some stage:

var CloudFoundryApi = require('cloud-foundry');

var cf_api = new CloudFoundryApi(api_endpoint, {token: 'foobar'});

cf_api.apps.list({ paging: { 'results-per-page': 1 }, filter: { name: "instances", value: ">1" }}, function (err, page) { if (err) {return console.log(err);}

        console.log(page.data);

        if (page.hasNextPage()) {
            page.getNextPage(function (err, next_page) {
                ...
           });
       }
    }

);


## TODO:

1. Better test coverage

2. Implement support for refresh tokens

3. Implement authentication via client credentials (for resource servers) when running under Node.js

## Contributing

Pull requests and issues welcome!

cloud-foundry-client-js's People

Contributors

activegregb avatar diggs avatar philwhln avatar shsu avatar tara-gibbs 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.