Comments (15)
oh damn, you are right. sorry about that - i did not look closely enough.
sorry for the false trail, i think then the http_build_query really is the way to go. you could still do your own variant of client wrapper like i suggested in my comment above but have it use http_build_query and decorate the HttpMethodsClient (or you request factory, as you prefer)
or you can just leave it as it was with the http_build_query in all the places.
from geocaching-php-sdk.
Hello @dbu, thanks for your feedback! I have updated #14
php-http/curl-client
is installed for the unit tests.
from geocaching-php-sdk.
i don't see you explicitly using the curl client in any tests. if you remove the requirement, the discovery composer plugin should now automatically install a psr client if needed.
oh, i tried this in the code and noticed you use the old httplug instead of psr18 client. HttpClientDiscovery is deprecated in favor of Psr18ClientDiscovery.
php-http/httplug is the predecessor of psr/http-client. the authors of httplug recommend to use the psr18 http client interface (which happens to have compatible signatures, so the upgrade path should be easy).
from geocaching-php-sdk.
Oh, thanks! I have fixed the dependency : 51101f2
It's not really easy to understand, I read an good but old article about in order to help me: https://madewithlove.com/blog/building-an-sdk-with-php-part-1/
from geocaching-php-sdk.
yay, that looks right to me.
the tutorial seems good to me, only unfortunately while it talks about Psr18 client, the code uses the old httplug discovery which likely is what mislead you.
from geocaching-php-sdk.
What do you think about these repetitions of http_build_query
: https://github.com/Surfoo/geocaching-php-sdk/blob/feat-v4/src/GeocachingSdk.php#L70 How can I avoid them?
from geocaching-php-sdk.
I rewrote the code, but the API have a swagger, what do you recommend to generate the code?
from geocaching-php-sdk.
for the query: you could use the psr client directly and the psr17 request factory to build a request object. then you can use $request->withQueryParams. the HttpMethodsClient is a convenience wrapper but currently does not offer dedicated support for building the query string.
for generating code from openapi spec, i used janephp once. the nice thing is that it builds you models for all the data. the not so nice thing is that the generated code is on the verbose side and if the openapi spec is not super nice the resulting sdk will also not be nice...
from geocaching-php-sdk.
for generating code from openapi spec...
Thanks, I think it's better to write my own code so...
for the query: you could use the psr client directly and the psr17 request factory to build a request object. then you can use $request->withQueryParams. the HttpMethodsClient is a convenience wrapper but currently does not offer dedicated support for building the query string.
Thanks for the help, but I think I don't get it... I tried not to use the ClientBuilder and Options class, but I missed something in the process.. My commit is here: 46728ff do you think you can help me?
from geocaching-php-sdk.
i don't think the http_build_query is bad, you asked about alternatives ;-)
i think you would keep the structure the same with options and all, just change for the ClientInterface & the RequestFactoryInterface of psr17 (php-http/discovery has a Psr17FactoryDiscovery.php to find the request and uri factories). you then build the request with the factory, set the uri on it and have withQueryParams on that. the ClientInterface only has a method to send a request, no shortcuts like the HttpMethodsClient.
but imho it was alright before, with the methods client.
from geocaching-php-sdk.
Ok, I changed to the ClientInterface in c305830
But for the remaining changes, Psr17FactoryDiscovery is already used by default in the constructor : https://github.com/Surfoo/geocaching-php-sdk/blob/feat-v4/src/ClientBuilder.php#L32 and if I skip the HttpMethodsClient, how to use the authentication middleware ?
It's very interesting for me, I want to go further to understand it better but without a real example of code it's very difficult to understand π²
from geocaching-php-sdk.
the PluginClient is a decorator on the psr client, so you can return that one, to use the authentication middleware. when you look at ClientInterface, you can see that it only has the method sendRequest(RequestInterface $request): ResponseInterface
. so in the places where you send requests, you use the request factory to build a request object, and use the request->withQueryParams to set your parameters.
i would create getter methods getRequestFactory(): RequestFactoryInterface
and getStreamFactory(): StreamFactoryInterface
on the ClientBuilder
from geocaching-php-sdk.
alternatively, you could do your own variant of the HttpMethodClient and use that in the application.
class HttpClient
{
public function __construct(
ClientInterface $httpClient = null,
RequestFactoryInterface $requestFactoryInterface = null,
StreamFactoryInterface $streamFactoryInterface = null
) {
$this->httpClient = $httpClient ?: Psr18ClientDiscovery::find();
$this->requestFactory = $requestFactory ?: Psr17FactoryDiscovery::findRequestFactory();
$this->streamFactory = $streamFactory ?: Psr17FactoryDiscovery::findStreamFactory();
}
public function get(string $uri, array $query, array $headers): ResponseInterface
{
$request = $this->requestFactory->createRequest('GET', $uri)
->withQueryParams($query)
;
foreach ($headers as $key => $value) {
$request = $request->withHeader($key, $value);
}
return $this->httpClient->sendRequest($request);
}
}
from geocaching-php-sdk.
and analog methods for the other HTTP verbs that you use in the sdk.
from geocaching-php-sdk.
Hum, withQueryParams
in on ServerRequestInterface
according to the PSR 7: https://www.php-fig.org/psr/psr-7/
I can't use it on RequestInterface
π€
And VSCode tell me the same thing:
from geocaching-php-sdk.
Related Issues (6)
- Not sure what i'm doing wrong HOT 1
- GeoCaching no longer accepting new requests for API access HOT 1
- Class OAuth not found HOT 5
- Site not working HOT 3
- API v1 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from geocaching-php-sdk.