saloonphp / saloon

🤠 Build beautiful API integrations and SDKs with Saloon
https://docs.saloon.dev
MIT License
2.09k stars 107 forks source link

Added isJson and isXML functions to Response class #455

Open mstfblci opened 2 months ago

mstfblci commented 2 months ago

Two new functions have been added to check if the response return is json or xml

SRWieZ commented 2 months ago

This could be really useful; in a lot of my Connectors, I do things like this:

public function hasRequestFailed(Response $response): ?bool
{
    // if psr response is json
    $contentType = $response->getPsrResponse()->getHeader('Content-Type');

    if (in_array('application/json', $contentType)) {
        return ! empty($response->json('errors'));
    }

    return null;
}

This PR would simplify it like this:

public function hasRequestFailed(Response $response): ?bool
{
    if ($response->isJson()) {
        return ! empty($response->json('errors'));
    }

    return null;
}

I would argue, though, that I prefer a more strict comparison than str_contains() proposed in this PR.

mstfblci commented 2 months ago

@SRWieZ Thank you for your support.