0
RESTful API
Tamim Shahriar Subeen
Mukto Software Ltd.
In Ancient Days
http://example.com/?command=command_name&param1=p&param2=q...
command = form['command']
if command == 'get...
Framework!
http://example.com/list/?
if request.method == 'GET':
...
if request.method == 'POST':
...
Time passes on ...
HTTP Methods
GET : Read A Resource
POST : Create a Resource
PUT : Update a Resource
DELETE : Delete a R...
Searching ...
Flask-RESTful
class Todo(Resource):
def get(self, todo_id):
abort_if_todo_doesnt_exist(todo_id)
return TODOS[todo_id]
def ...
Test Your REST API
Write tests for successful failure!
GET http://example.com/todos/
It should successfully return 404 sta...
Test Your REST API
Write tests for successful success!
GET http://example.com/todos/1
It should successfully return the to...
REST API Testing Methods
● Browser Addons
● CURL
● Write Your Own Code
○ requests module
requests.get
requests.put
request...
Use a Framework
unittest
Unit testing framework in Python
Code in Action
import requests
import unittest
class TestShopolotApi(unittest.TestCase):
def setUp(self):
print " in setup...
Rest API in my experience
Upcoming SlideShare
Loading in...5
×

Rest API in my experience

783

Published on

Sharing my experience with REST API over the years in PyCon Dhaka 2014

Published in: Software, Technology, Design
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
783
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
22
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Transcript of "Rest API in my experience"

  1. 1. RESTful API Tamim Shahriar Subeen Mukto Software Ltd.
  2. 2. In Ancient Days http://example.com/?command=command_name&param1=p&param2=q... command = form['command'] if command == 'get_list': ... elif command == 'create_list': param1 = form['param1'] param2 = form['param2'] ... elif command == 'update_list': ... elif command == 'delete_list': ... else: print 'Unknown Command'
  3. 3. Framework! http://example.com/list/? if request.method == 'GET': ... if request.method == 'POST': ...
  4. 4. Time passes on ... HTTP Methods GET : Read A Resource POST : Create a Resource PUT : Update a Resource DELETE : Delete a Resource Response Status Code, Content
  5. 5. Searching ...
  6. 6. Flask-RESTful class Todo(Resource): def get(self, todo_id): abort_if_todo_doesnt_exist(todo_id) return TODOS[todo_id] def delete(self, todo_id): abort_if_todo_doesnt_exist(todo_id) del TODOS[todo_id] return '', 204 def put(self, todo_id): args = parser.parse_args() task = {'task': args['task']} TODOS[todo_id] = task return task, 201
  7. 7. Test Your REST API Write tests for successful failure! GET http://example.com/todos/ It should successfully return 404 status code DELETE http://example.com/todos/-1 It should successfully return 404 status code Check the status code and also the error message!
  8. 8. Test Your REST API Write tests for successful success! GET http://example.com/todos/1 It should successfully return the to_do item with task id 1 and 200 status code DELETE http://example.com/todos/1 It should successfully delete the task with id 1 return 204 status code Check the status code first, then validate the response data.
  9. 9. REST API Testing Methods ● Browser Addons ● CURL ● Write Your Own Code ○ requests module requests.get requests.put requests.post requests.delete
  10. 10. Use a Framework unittest Unit testing framework in Python
  11. 11. Code in Action import requests import unittest class TestShopolotApi(unittest.TestCase): def setUp(self): print " in setup " def tearDown(self): print " in teardown " def test_login_wrong(self): print "testing login 1" url = 'http://example.com/' + 'login' data = {'email': email, 'password': password_hash} r = requests.post(url, data) self.assertEqual(r.status_code, 400) if __name__ == '__main__': unittest.main()
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×