Rest API in my experience
Upcoming SlideShare
Loading in...5
×
 

Rest API in my experience

on

  • 532 views

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

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

Statistics

Views

Total Views
532
Views on SlideShare
507
Embed Views
25

Actions

Likes
2
Downloads
16
Comments
0

3 Embeds 25

https://twitter.com 21
https://www.linkedin.com 2
http://www.linkedin.com 2

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Rest API in my experience Rest API in my experience Presentation Transcript

  • 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_list': ... elif command == 'create_list': param1 = form['param1'] param2 = form['param2'] ... elif command == 'update_list': ... elif command == 'delete_list': ... else: print 'Unknown Command'
  • 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 Resource Response Status Code, Content
  • Searching ...
  • 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
  • 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!
  • 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.
  • REST API Testing Methods ● Browser Addons ● CURL ● Write Your Own Code ○ requests module requests.get requests.put requests.post requests.delete
  • 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 " 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()