Feature #7338
REST webservice for personal messages reading
Description
In order to develop the mobile app to get your personal Chamilo messages on your smartphone, we need a series of webservices as described below. The objective of this task is to develop these web services.
File¶
In order to develop REST webservices (which don't really exist in any coordinated fashion in Chamilo), we will create a main/webservices/rest.php file which contains the main controller and the available webservices, and will call Chamilo functions/methods as needed
Authentication¶
We want to ensure an authentication that is as secure as possible, but this will require HTTPS (we cannot ensure security in the communication with a non-HTTPS Chamilo portal).
The first connection with any Chamilo portal, in the mobile app, will require the URL of the portal and the introduction of the login/password information.
This will generate a new API key (a new user field of type "text" called api_key_message, or the existing one if one already exists). The API key is a random SHA1 key.
The SHA1 key is returned by the webservice, so the mobile application can store it.
So, something like this (all the function names are invented):
if (isValidPassword($username, $password)) { $userInfo = api_get_user_info($username); if (UserField::fieldExists('api_key_message')) { $apiKey = UserField::get_field_value('api_key_message', $username); return json_encode('result' => $apiKey); } else { UserField::createUserField('api_key_message'); $apiKey = generateApiKey(); $result = UserField::create('api_key_message', $username, $apiKey); if ($result) { return json_encode('result' => $apiKey); } else { return json_encode('result' => false); } } } else { return json_encode('result' => false); }
Later on, all other webservices will only work with the API key.
Web services¶
Get new messages count¶
public function getNewMessagesCount($username, $apiKey, $last = 0) { $userInfo = api_get_user_info_from_username($username); // get count of new messages for this user, since the last message already received return json_encode('result' => $count); }
Get new messages¶
public function getNewMessages($username, $apiKey, $last = 0) { $userInfo = api_get_user_info_from_username($username); // get new messages for this user, since the last message already received $messages = ... // $messages should be an array something like this: // 0 => array('id' => $row['id'], 'title' => $row['title'], 'sender' => $row['sender'], 'content' => $row['content']), // 1 => array('id' => ...), // 2 => array('id' => ...), ... return json_encode( 'result' => $count, 'messages' => $messages ); }
Associated revisions
Count the messages from the last received message - refs #7338
Minor - Update PHPDoc - refs #7338
Get messages data from the last received message - refs #7338
Minor - Update PHPDoc - refs #7338
Generate api keys - refs #7338
Update the getNewMessages method - refs #7338
Minor - Fix PHPDoc - refs #7338
Filter before calling a database method - refs #7338
Update the API key generator method - refs #7338
Remove unnecessary filters - refs #7338
Add links to the portal and messaging tool - refs #7338
Add webservices classes for autload - refs #7338
Fix undefined index E_NOTICE - refs #7338
Add status for right response - refs #7338
Fix define a method like abstract static E_STRICT - refs #7338
Add information about whether there is attachments - refs #7338
Sort messages by recent date - refs #7338
Add sent date to message - refs #7338
Add content type header to JSON response - refs #7338
Add CORS header to JSON response - refs #7338
Fix get message sender - refs #7338
Minor - Fix rest webservice PHPDoc - refs #7338
Move REST webservice to main directory - refs #7338
Merge pull request #453 from AngelFQC/7338
Fix PHPDoc and move REST webservice to main directory - refs #7338
Fix require libs and classes - refs #7338
Fix valid user for web service - refs #7338
Fix valid user password when username doesn't exists - refs #7338
History
Updated by Angel Quiroz over 6 years ago
- % Done changed from 10 to 20
At the moment
I started with create the rest.php and classe for the web service
Add methods for get the user api key, check if the api is valid for a user, check whether the username and password are valid
Updated by Angel Quiroz over 6 years ago
POST Request to /webservices/rest.php
To get api key
Params
action = loginNewMessages
username
password
Success return
{"status": "true", "apiKey":"The api key"}
Failed return
{"status":false}
To count new messages
Params
action = countNewMessages
username
api_key
Success return
{"status": "true", "count":"12"}
Failed return
{"status":false}
To get new messages data
Params
action = getNewMessages
username
api_key
Success return
{"status": "true", "messages": [{id: 0, title: '', sender: {id: 0, lastname: '', firstname: '', completeName: ''}, content: ''}, {...}]}
Failed return
{"status":false}
Updated by Angel Quiroz over 6 years ago
- % Done changed from 20 to 50
What I need to do is check for attachments
Updated PR
Updated by Angel Quiroz over 6 years ago
- Assignee changed from Angel Quiroz to Yannick Warnier
Add information about whether there is attachments
Updated by Angel Quiroz over 6 years ago
- Status changed from Assigned to Needs testing
- Assignee deleted (
Yannick Warnier)
Updated by Yannick Warnier over 6 years ago
- Status changed from Needs testing to Feature implemented
- Assignee set to Angel Quiroz
- % Done changed from 50 to 100
This is a new feature that does not appear directly to the normal final users, so it can be closed without specific testing.
Good job!
Updated by Angel Quiroz about 6 years ago
Receiver information is sent but not the sender
Updated by Angel Quiroz about 6 years ago
Move the REST webservice to main directory and fix the PHPDoc package name
https://github.com/chamilo/chamilo-lms/pull/453 and https://github.com/chamilo/chamilo-lms/pull/454
Add REST web service for get personal messages - refs #7338