jmeyering / hal-explorer Goto Github PK
View Code? Open in Web Editor NEWA php API client for HAL formatted APIs.
License: MIT License
A php API client for HAL formatted APIs.
License: MIT License
Hi, it's me again ;-)
I just tried to do a multipart post on our client's HAL-API and had a really hard time setting the "Content-Type"-Header. It's set by default to "application/json" but for something like this to work:
$this->explorer->createRelation($this->enter(), 'documents', [
'multipart' => [
[
'name' => 'content',
'filename' => 'filename',
'contents' => $data,
]
]
]
...the "Content-Type" must be empty, so that Guzzle sets is on its own.
I accomplished that by calling:
$this->explorer->setDefaults(function ($original) {
$original["headers"]["Authorization"] = 'bearer '.$this->getAuthorizationToken();
unset($original["headers"]["Content-Type"]);
return $original;
});
As there's no "getConfiguredDefaultCallback" I couldn't write a workaround like:
So it is possible but very cumbersome and hard to reset afterwards.
Any ideas?
Like with Exploder's methods, it would be nice if AbstractAdapter::setClient()
could return $this
to make the interface fluent.
This would allow for a fluent initialization like this:
$this->explorer = (new Explorer())
->setAdapter((new Adapter())->setClient(new Client()))
->setBaseUrl('http://haltalk.herokuapp.com')
->setDefaults(function ($original) {
$original["headers"]["Authorization"] = 'bearer '.$this->getAuthorizationToken();
return $original;
})
;
First, thank you so much for putting this together.
In \HalExplorer\Explorer::followLink()
the HyperMediaParser()->getLink()
method can return an array of objects. So the property_exists()
check on line 242 can throw a few warnings such as Warning: First parameter must either be an object or the name of an existing class in ...
.
For my needs, I needed to grab $link[0]
before the property_exists()
call.
Now, I don't know if this is an issue with the API I'm using (It's a new, internal API at my university), or whether this is something that your library should account for. I'm new to HAL APIs.
If this is something that the HAL-explorer library should deal with, I'm happy to create a PR here, but would love some advice on how you think this should be handled.
Sorry to bother you again but, as stated in the issue's description, the Adapter currently creates PHP errors because it doesn't implement the patch() method.
When I'm getting more confident with Git(Hub) I will create pull requests but for now I think what is missing is only:
/**
* Perform a patch request
*
* @param string $uri
* @param array $options All options for the request, will be passed to the
* client
*
* @return ResponseInterface
*/
public function patch($uri, array $options = [])
{
return $this->getClient()->patch($uri, $options);
}
As in the HAL draft "_embedded" section the response is an object with link relation types as properties, but when you get one of them, it can contain either a resource object or an array of resource objects. I think that the function should be able to return a simple stdClass or an array of them (if it's correct, maybe using two functions with different return types?)
I'm working with an API that exposes absolute URLs instead of relative ones, like:
"doc:documents" : { "href" : "https://customer.com/rest/api/documents" }
The base URL is https://customer.com/rest/api
.
When I now call $explorer->getRelation($response, 'documents')
it creates the URI https://customer.com/rest/api//rest/api/documents
because of the way Exploder::makeRequest()
assumes that the path given is relative.
I would suggest something like:
// Remove the base url from the uri if it's an absolute one
$parsed = parse_url(str_replace($this->getBaseUrl(), '', $uri));
$path = $parsed["path"];
// Trim leading slash if it exists
$path = ltrim($path, "/");
Doing the ltrim
later makes sure that only slashes from the extracted path are removed. Also this way network paths like //github.com/rest/api
won't be broken.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.