Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two seperate API call values in one array

Situation

I have two API calls, one to get all the devices and one to get all the groups. What I would like to accomplish is to have one call that would fetch the groups with its devices.

This is the function to get the devices

public function getAll($environment, $options)
{
    // Get group ids from database, used for pairing groups with environments
    $groups = Device::where('source_id', $options['deviceSourceId'])->get();
    $groupIds = [];
    foreach ($groups as $group) {
        $groupIds[] = $group['group_id'];
    }

    // Parameters
    $apiparams = [
        'headers' => $this->getHeaders($environment)
    ];

    $devices = [];

    foreach($groupIds as $groupId)
    {
        $responseAPI = $this->getClient()->request('GET', 'devices?groupid=' . $groupId, $apiparams);
        $responseAPI = json_decode($responseAPI->getBody()->getContents(), true);
        $devices[] = $responseAPI['devices'];
    }

    return $devices;
}

This is the function to get the groups

public function getGroups($environment, $options)
{
    // Get group ids from database, used for pairing groups with environments
    $groups = Device::where('source_id', $options['deviceSourceId'])->get();
    $groupIds = [];
    foreach ($groups as $group) {
        $groupIds[] = $group['group_id'];
    }

    // Parameters
    $apiparams = [
        'headers' => $this->getHeaders($environment)
    ];

    $groups2 = [];

    foreach($groupIds as $groupId)
    {
        $responseAPI = $this->getClient()->request('GET', 'groups/' . $groupId, $apiparams);
        $responseAPI = json_decode($responseAPI->getBody()->getContents(), true);

        $groups2[] = $responseAPI;
    }
    return $groups2;
}

Current response

This is the response I get from these.

// Device response
[
    {
        "remotecontrol_id": "r5674567"
        "device_id": "d871212",
        "alias": "PC-01",
        "groupid": "g9873491",
        "online_state": "Online",
        "assigned_to": false
    },
    {
        "remotecontrol_id": "r8370129"
        "device_id": "d091231",
        "alias": "PC-02",
        "groupid": "g9873491",
        "online_state": "Offline",
        "assigned_to": false
    }
]

// Group response
{
    "id": "g9873491"
    "name": "companyname",
    "permissions": "owned"
}

Needed response

The response I would like to get is:

[{id: "g9873491",
  name: "companyname",
  devices: [{device_id: "d871212",
             alias: "PC-01",
             online_state: "Online"},
            {device_id: "d091231",
             alias: "PC-02",
             online_state: "Offline"}
            }],
}]

How would I need to do this?

Sevavietl edit

public function getAll($environment, $options)
{
    // Get group ids from database, used for pairing groups with environments
    $groups = Device::where('source_id', $options['deviceSourceId'])->get();
    $groupIds = [];
    foreach ($groups as $group) {
        $groupIds[] = $group['group_id'];
    }

    // Parameters
    $apiparams = [
        'headers' => $this->getHeaders($environment)
    ];

    foreach($groupIds as $groupId)
    {

        $responseAPI1 = $this->getClient()->request('GET', 'groups/' . $groupId, $apiparams);
        $responseAPI2 = $this->getClient()->request('GET', 'devices?groupid=' . $groupId, $apiparams);

        $groups2 = json_decode($responseAPI1->getBody()->getContents(), true);
        $devices = json_decode($responseAPI2->getBody()->getContents(), true);

        $result = array_values(array_reduce(
            $devices,
            function ($carry, $device) {
                if (isset($carry[$device['groupid']])) { // Undefined index: groupid
                    // Add device to group.
                    $carry[$device['groupid']]['devices'][] = [
                        'device_id' => $device['device_id'],
                        'alias' => $device['alias'],
                        'online_state' => $device['online_state']
                    ];
                }

                return $carry;
            },
            // Reindex groups by id.
            array_column($groups2, null, 'id')
        ));
    }

    return $result;
}
like image 286
idontunderstandarrays Avatar asked Mar 15 '18 09:03

idontunderstandarrays


People also ask

Can a API call another API?

Like telephones, API calls enable APIs to talk to one another and exchange information.

How many API calls can I make?

In the API Console, there is a similar quota referred to as Requests per 100 seconds per user. By default, it is set to 100 requests per 100 seconds per user and can be adjusted to a maximum value of 1,000. But the number of requests to the API is restricted to a maximum of 10 requests per second per user.


1 Answers

If you don't have control over the API, I guess you cannot reduce the number of calls, as you have only calls defined by the API itself. So one solution would be to prepare the response in needed format yourself. You can do the following:

$devices = json_decode($devices, true);
$groups = json_decode($groups, true);

$result = array_values(array_reduce(
    $devices,
    function ($carry, $device) {
        if (isset($carry[$device['groupid']])) {
            // Add device to group.
            $carry[$device['groupid']]['devices'][] = [
                'device_id' => $device['device_id'],
                'alias' => $device['alias'],
                'online_state' => $device['online_state']
            ];
        }

        return $carry;
    },
    // Reindex groups by id.
    array_column($groups, null, 'id')
));

Here is the demo.

As you are using Laravel, another approach would be to take advantage of Laravel Collections. But in this particular situation, I guess, unless you are the collections guru (and read "Refactoring to Collections" from cover to cover:)) the plain PHP array functions is easier to understand and work with.

like image 182
sevavietl Avatar answered Oct 27 '22 04:10

sevavietl