Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check Status Code of responses (especially for POST/PUT/DELETE requests) #367

Open
Art4 opened this issue Jan 23, 2024 · 0 comments
Open
Labels
enhancement pending: help wanted help and PRs are welcome
Milestone

Comments

@Art4
Copy link
Collaborator

Art4 commented Jan 23, 2024

After requesting the Redmine server in Redmine\Api\... classes we should check for the correct status code in the responses, see #341. Otherwise we should throw an UnexpectedResponseException, see #364.

Throwing an exception would be a breaking change, so this change has to be made in a forward compatible way, or in the next major version.

A FC way could look like this:

// in \Redmine\Api\Project
    public function update($id, array $params)
    {
        // ...

        $result = $this->put(
            '/projects/' . $id . '.xml',
            XmlSerializer::createFromArray(['project' => $params])->getEncoded()
        );

        $lastResponse = $this->getLastResponse();

        if ($lastResponse->getStatusCode() !== 204) {
            if (!defined('PHP_REDMINE_API_THROW_EXCEPTIONS_ON_STATUS_CODE_MISSMATCH') || PHP_REDMINE_API_THROW_EXCEPTIONS_ON_STATUS_CODE_MISSMATCH !== true) {
                trigger_error(
                    sprintf(
                        'The Redmine server replied with the status code %d in %s(), starting with v2.0.0 this will throw an %s. Define the constant `%s` to throw the exceptions now.',
                        $lastResponse->getStatusCode(),
                        __METHOD__,
                        UnexpectedResponseException::class,
                        'PHP_REDMINE_API_THROW_EXCEPTIONS_ON_STATUS_CODE_MISSMATCH'
                    ),
                    E_USER_DEPRECATED
                );

                return $result;
            }

            throw new UnexpectedResponseException($lastResponse);
        }

        return true;
    }

In v3 we could just remove the check.

// in \Redmine\Api\Project
    public function update($id, array $params)
    {
        // ...

        $result = $this->put(
            '/projects/' . $id . '.xml',
            XmlSerializer::createFromArray(['project' => $params])->getEncoded()
        );

        $lastResponse = $this->getLastResponse();

        if ($lastResponse->getStatusCode() !== 204) {
            throw new UnexpectedResponseException($lastResponse);
        }

        return true;
    }
@Art4 Art4 added this to the v2.6.0 milestone Jan 23, 2024
@Art4 Art4 added the pending: help wanted help and PRs are welcome label Jan 24, 2024
@Art4 Art4 modified the milestones: v2.6.0, v2.7.0 Feb 15, 2024
@Art4 Art4 modified the milestones: v2.7.0, v2.8.0 Jul 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement pending: help wanted help and PRs are welcome
Projects
None yet
Development

No branches or pull requests

1 participant