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

Support for PATCH in save #2

Closed
aaronweatherall opened this issue Mar 5, 2018 · 1 comment
Closed

Support for PATCH in save #2

aaronweatherall opened this issue Mar 5, 2018 · 1 comment
Assignees
Labels

Comments

@aaronweatherall
Copy link

Hey Team, I'm attempting to implement this library and am having trouble with the fact that it doesn't seem to support PATCH for updating records.

As per the JsonAPI documentation, updates should be done via a PATCH request and not a POST.
See http://jsonapi.org/format/#crud-updating

Here's what I mean.

vendor/enm/json-api-client/src/JsonApiClient.php

    public function save(SaveRequestInterface $request): DocumentInterface
    {
        $uri = $this->buildUri($this->buildPath($request));
        return $this->handleResponse(
            $this->httpClient()->post(
                $uri,
                $this->buildRequestContent($request->document()),
                $request->headers()->all()
            )
        );
    }

When using Guzzle, this generates a POST request for update or create - which fails on our API.

I'n not sure if this is an issue with the code or a documentation issue?

Here's the implementation code (Note this also fails without setting the Content Type manually).

$resource = $this->getApiClient()->resource($this->getDefaultResource(), $id);
$resource->attributes()->set('global_tracked_status', $status);

$request = $this->getApiClient()->createSaveSingleResourceRequest($resource, true);
$request->headers()->set('Content-Type', 'application/json');

$result = $this->getApiClient()->save($request);

Obviously this can be fixed with something like the following, but this would probably be a breaking change without refactoring save entirely.

$method = $request->containsId() ? 'patch' : 'post';
return $this->handleResponse(
    $this->httpClient()->$method(
        $uri,
        $this->buildRequestContent($request->document()),
        $request->headers()->all()
    )
);

Thoughts?

@pmarien
Copy link
Member

pmarien commented Mar 6, 2018

Hi @aaronweatherall,
you're right! I have fixed this issue.
To support POST requests with client generated id's I have added a new optional parameter "forceCreate".
The new tag is 1.2.0

@pmarien pmarien closed this as completed Mar 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants