saloon icon indicating copy to clipboard operation
saloon copied to clipboard

Added isJson and isXML functions to Response class

Open mstfblci opened this issue 1 year ago • 2 comments

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

mstfblci avatar Sep 12 '24 13:09 mstfblci

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.

SRWieZ avatar Sep 17 '24 09:09 SRWieZ

@SRWieZ Thank you for your support.

mstfblci avatar Sep 17 '24 11:09 mstfblci

I'm going to have a Saloon day this weekend so I'll merge it then, thanks for the patience!

Sammyjo20 avatar Feb 28 '25 14:02 Sammyjo20

Glad to see you back!

And thank you for all the work you put into Saloon.

SRWieZ avatar Mar 01 '25 10:03 SRWieZ