Implementing It In: Rest Api
Implementing It In: Rest Api
Implementing It In: Rest Api
Implementing it in
CodeIgniter
Who Am I
• PHP Developer & Consultant
• Reviewed “Testing with Qunit”
• Helped to bring out thesis on “Business
Prospective of cloud computing”
• Founder of Website “WebGunny.com”
Finally Format:
https://www.YourApp.com/Api/ResourceName
Identifying resources
• You can make the resources more sensible
based on your product
• For example
– Tasks
– Comments
– Notifications
– Users
– Projects
– Files
JSON Everywhere
Make Use of HTTP Verbs
• GET /task - Retrieves a list of task
• GET /task/12 - Retrieves a specific task
• POST /task - Creates a new task
• PUT /task/12 - Updates task #12
• PATCH /task/12 - Partially updates task #12
• DELETE /task/ - Deletes all task
• DELETE /task/12 - Deletes task #12
Map the relationships
• GET /task/12/comments - Retrieves list of comments
for task #12
• GET /task /12/comments/5 - Retrieves comment #5 for
task #12
• POST /task /12/comments - Creates a new comments
in task #12
• PUT /task /12/comments/5 - Updates comments #5 for
task #12
• PATCH /task /12/comments/5 - Partially updates
comment #5 for task #12
• DELETE /task/12/comments/5 - Deletes comment #5
for task #12
Search Sort & Filter
• GET /tasks?sort=-priority - Retrieves a list of
task in descending order of priority
• GET /tasks?sort=-priority,created_at -
Retrieves a list of tasks in descending order of
priority then by date created
Aliases for common queries
To make the API experience more pleasant for
the average consumer
GET /tasks?status=completed
GET /tasks/recently_completed
Allow the fields to be selected
The API consumer doesn't always need the full
representation of a resource.
GET /task?fields=id,title,updated_at
Paging of data
Paging makes the API fast & responsive
GET /notification?page=1&per_page=50
Return full resource after action
• A PUT, POST or PATCH call may make
modifications to fields
• Return the updated (or created)
representation as part of the response.
• Prevent an API consumer from having to hit
the API again
Auto loading related
resources
{ "id" : 12,
“TaskName" : "I have a question!",
"summary" : "Hi, ....",
"customer" : { "name" : "Bob" },
assigned_user: { "id" : 42, "name" : "Jim", }
}
Make Error Message Friendly
• The API should always return sensible HTTP
status codes
• 400 series status codes for client issues & 500
series status codes for server issues
• API should standardize that all 400 series errors
come with consumable JSON error
representation
{ "code" : 1234,
"message" : “task field validation failed ",
"description" : “Due date is not set"
}
Authentication
API Status History
Documentation
REST API in Codeigniter
UI/ Controller
Your API
Rest Client
Your App
What we need
• Codeigniter
• chriskacerguis/codeigniter-restserver
• Router implementation
Structuring the project
/application
/controller/
api/ //For all api controllers
/libraries //For the third-party libraries
REST_server.php
Format.php
/config //For all config files
Router.php
Rest_server.php
Router Implementation
//res/id/function/id --> res/function/id/num/sid/num
$route['api/([a-z_]+)/(:any)/([a-z_]+)/(:any)'] = 'api/$1/$3/id/$2/rid/$4';
Website:SachinGKulkarni.com
Twitter:@sachingk30
Email:[email protected]