patternphpMinor
Web dashboard using many REST API requests
Viewed 0 times
dashboardrestusingwebmanyapirequests
Problem
I have created REST API in codeigniter. REST sever created in codeigniter 3.0 and REST client created codeigniter 2.x.x.
I have wrote simple code for login. After login client created dashboard page. So for creating dashboard page I am sending so many request to REST server and it's taking too much time.
Below is the function for send request to server for dashboard. The function called after successfully login. So can please review my code and tell me is it correct way ? I have no idea about REST so and my system is too slow
```
public function show_dashboard()
{
$rest_url_module_names="http://MyAPI/Dashboard_api/module_names/token/".$this->session->userdata('userToken');
$data_module_names = $this->curl->simple_get($rest_url_module_names);
$data_module_names=json_decode($data_module_names,true);
if($data_module_names['status']=='success')
{
$data['module_names'] = $data_module_names['data'];
}
$rest_url_single_emp="http://MyAPI/Dashboard_api/single_emp/token/".$this->session->userdata('userToken');
$data_single_emp = $this->curl->simple_get($rest_url_single_emp);
$data_single_emp=json_decode($data_single_emp,true);
if($data_single_emp['status']=='success')
{
$data['single_emp'] = $data_single_emp['data'];
}
$rest_url_department_nm="http://MyAPI/Dashboard_api/department_nm/token/".$this->session->userdata('userToken');
$data_department_nm = $this->curl->simple_get($rest_url_department_nm);
$data_department_nm=json_decode($data_department_nm,true);
if($data_department_nm['status']=='success')
{
$data['department_nm'] = $data_department_nm['data'];
}
$rest_url_designation_nm="http://MyAPI/Dashboard_api/designation_nm/token/".$this->session->userdata('userToken');
$data_designation_nm = $this->curl->simple_get($rest_url_designation_nm);
$data_designati
I have wrote simple code for login. After login client created dashboard page. So for creating dashboard page I am sending so many request to REST server and it's taking too much time.
Below is the function for send request to server for dashboard. The function called after successfully login. So can please review my code and tell me is it correct way ? I have no idea about REST so and my system is too slow
```
public function show_dashboard()
{
$rest_url_module_names="http://MyAPI/Dashboard_api/module_names/token/".$this->session->userdata('userToken');
$data_module_names = $this->curl->simple_get($rest_url_module_names);
$data_module_names=json_decode($data_module_names,true);
if($data_module_names['status']=='success')
{
$data['module_names'] = $data_module_names['data'];
}
$rest_url_single_emp="http://MyAPI/Dashboard_api/single_emp/token/".$this->session->userdata('userToken');
$data_single_emp = $this->curl->simple_get($rest_url_single_emp);
$data_single_emp=json_decode($data_single_emp,true);
if($data_single_emp['status']=='success')
{
$data['single_emp'] = $data_single_emp['data'];
}
$rest_url_department_nm="http://MyAPI/Dashboard_api/department_nm/token/".$this->session->userdata('userToken');
$data_department_nm = $this->curl->simple_get($rest_url_department_nm);
$data_department_nm=json_decode($data_department_nm,true);
if($data_department_nm['status']=='success')
{
$data['department_nm'] = $data_department_nm['data'];
}
$rest_url_designation_nm="http://MyAPI/Dashboard_api/designation_nm/token/".$this->session->userdata('userToken');
$data_designation_nm = $this->curl->simple_get($rest_url_designation_nm);
$data_designati
Solution
As Pimgd stated, all your request are synchronous (and wait that the previous one terminates before executing further).
To make the calls asynchronous I see two possibilities:
If you know that your clients have Javascript activated, I would recommend you to go for the first version.
Regarding your edit:
Every time you perform:
You could create two functions for this:
But if you want to perform them in parallel, you will have to:
To make the calls asynchronous I see two possibilities:
- either the client (browser) does it: you send an almost empty page (with the token) and some Javascript code to perform the API calls (asynchronously).
- or the server does it and I recommend you to read about
curl_multi(https://github.com/joshfraser/rolling-curl of https://stackoverflow.com/q/26039848/3207406)
If you know that your clients have Javascript activated, I would recommend you to go for the first version.
Regarding your edit:
Every time you perform:
$rest_url=...;
$this->curl->simple_get...;
json_decode...;
if(..=='success')
...You could create two functions for this:
private function request_api_data($method)
{
$rest_url="http://MyAPI/Dashboard_api/".$method. "/token/".$this->session->userdata('userToken');
$data = $this->curl->simple_get($rest_url);
$data = json_decode($data, true);
if($data['status']=='success'){
return $data['data'];
} else {
return null;
}
}
private function gather_api_data($methods)
{
$all_data = [];
foreach($methods as $method){
$data = $this->request_api_data($method);
if(!empty($data)){
$all_data[$method] = $data;
}
}
return $all_data;
}But if you want to perform them in parallel, you will have to:
- generate all the URLs
- pass them to the
curl_multi
- have a feedback which updates the
$all_datadepending on the request which just completed.
Code Snippets
$rest_url=...;
$this->curl->simple_get...;
json_decode...;
if(..=='success')
...private function request_api_data($method)
{
$rest_url="http://MyAPI/Dashboard_api/".$method. "/token/".$this->session->userdata('userToken');
$data = $this->curl->simple_get($rest_url);
$data = json_decode($data, true);
if($data['status']=='success'){
return $data['data'];
} else {
return null;
}
}
private function gather_api_data($methods)
{
$all_data = [];
foreach($methods as $method){
$data = $this->request_api_data($method);
if(!empty($data)){
$all_data[$method] = $data;
}
}
return $all_data;
}Context
StackExchange Code Review Q#121772, answer score: 4
Revisions (0)
No revisions yet.