Skip to content

Responses

Jay edited this page Oct 22, 2020 · 8 revisions

For successful calls, depending on the function called, the response from the client will either be a single model or a \SupportPal\ApiClient\Model\Collection\Collection object, which is a collection of models.

Models

If we fetch a single user, our response will be a User model:

$user = $api->getUser(123);

Model data can be accessed via getters methods:

$firstname = $user->getFirstname(); // 'John'
$lastname = $user->getLastname(); // 'Doe'

$formattedName = $user->getFormattedName(); // 'John Doe'

$active = $user->getActive(); // 1

* The attributes returned from the API use snake_case while the getters defined in this library use camelCase.

Collections

If we fetch multiple users, our response will be a collection which contains an array of User models:

$users = $api->getUsers();

The collection class contains the following attributes:

// The total number of records that were found for this endpoint, not considering 
// the `limit` or `start` options. Useful for pagination.
$count = $users->getCount(); // 1000

// The total number of models included in the response data.
$modelsCount = $users->getModelsCount(); // 50

// An array of models.
$models = $users->getModels();
foreach ($models as $model) {
    ...
}

Each model in the array can be handled in the same way as described above.

Exception Handling

For API calls that fail or error for some reason, a \SupportPal\ApiClient\Exception\HttpResponseException exception is raised. The exception message will contain the error message delivered from the API.