Skip to content

cosmic-api/hello-world.py

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Example Cosmic API

hello_world_api.py is a demo application created to demonstrate basic features of Cosmic.py. The demo API remembers upto 10 names submitted by the user, and it can return list of the names.

Visit GitHub page to read annotated source of this project.

Setup

Get source.

git clone git://github.com/cosmic-api/hello-world.py.git
cd hello-world.py

Install Cosmic with pip

pip install -r requirements.txt

Optionally, if you use virtualenv

virtualenv venv
source venv/bin/activate
pip install -r requirements.txt

Start the server

python hello_world_api.py

Now you have a Cosmic web API running at port 8080, listening to http requests.

Play with the API

Using Cosmic client

Our client can load any Cosmic APIs via /spec.json endpoint, which is generated automatically.

>>> from cosmic.api import API
>>> client = API.load('http://localhost:8080/spec.json')
>>> client.actions.remember_me("Cosmic")
u"Hello, Cosmic! I'll remember you."
>>> client.actions.remember_me("Cosmic")
u'Welcome back, Cosmic.'
>>> client.actions.remember_me("Hello")
u"Hello, Hello! I'll remember you."
>>> client.actions.list_people()
[u'Cosmic', u'Hello']

Using raw HTTP request

(You can use Postman for Chrome as well, it's an excellent app for playing with web APIs)

You can inspect how the API's spec looks like.

curl http://localhost:8080/spec.json
{
  "name": "cosmic_hello_world",
  "actions": [
    {
      "name": "remember_me",
      "accepts": {"type": "string"},
      "returns": {"type": "string"}
    }, 
    {
      "name": "list_people",
      "returns": {
        "type": "array",
        "items": {"type": "string"}
      }
    }
  ],
  "models": []
}

Let's add a name.

curl -X POST \
  --header "Content-Type: application/json" \
  -d "\"Cosmic\"" \
  http://localhost:8080/actions/remember_me
"Hello, Cosmic! I'll remember you."

Try adding the same name again.

curl -X POST \
  --header "Content-Type: application/json" \
  -d "\"Cosmic\"" \
  http://localhost:8080/actions/remember_me
"Welcome back, Cosmic."

Add another name.

curl -X POST \
  --header "Content-Type: application/json" \
  -d "\"Hello\"" \
  http://localhost:8080/actions/remember_me
"Hello, Hello! I'll remember you."

Now let's see the list of people -

curl -X POST \
  --header "Content-Type: application/json" \
  http://localhost:8080/actions/list_people
["Cosmic", "Hello"]

Visit Cosmic for more information.


License

The MIT License (MIT)

Copyright (C) 2013 8313547 Canada Inc.

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.

Releases

No releases published

Packages

No packages published

Languages