saintsystems / odata-client-php

OData Client Library for PHP
MIT License
141 stars 103 forks source link

how to build this url? #125

Closed baradhili closed 1 year ago

baradhili commented 1 year ago

I am accessing an API that returns OData

https://mri.cts-mrp.eu/portal/v1/odata/ProductSearch?$filter=(UpdatedAt%20ge%202022-12-27T18%3A54%3A09%2B08%3A00)%20and%20(UpdatedAt%20le%202023-01-27T18%3A54%3A09%2B08%3A00)%20and%20(domainKey%20eq%20%27h%27)&$expand=activeSubstances,atcCodes,doseForms,rms&$count=true&$skip=10&$top=10

I'd like to access it via the OData client - how do I build the filters etc via the OData client? or should I just build the URL as a base?

Also I was wondering the process of pulling all the data down based on the value in "@odata.count"?

Thanks

baradhili commented 1 year ago

the things that would be good in the docs:

Also nice to have - a way to dump the generated URL for debugging

Involture commented 1 year ago

To dump the url associated to a request, use

$request->getHttpRequestMessage()->requestUri;

To get a request from a builder use

$builder->toRequest();
n8rowley commented 1 year ago

What about in the scenario where you are posting information @Involture? I'm trying to create a new Business Central sales quote by sending a post request to the salesQuote entity. I'm getting a 404 not found in response. I'm able to successfully request data from BC using the salesQuote entity. I would like to compare the route that ODataClient is posting to with the documentation, but I can't capture the URL before it's sent off.

$client = new ODataClient($this->url, function($request)  {
    $request->headers['Authorization'] = 'Bearer '.$this->getToken();
});

$response = $client->post('salesQuotes', $dataObject);

When you say "To dump the url associated to a request..." is the post() function returning a request object, or do I get that some other way. I tried tacking on the call to getHttpRequestMessage() after the post function, but it doesn't work because it as already sent the request.

anderly commented 1 year ago

@n8rowley,

The post() method just forwards to the request() method which is just doing the following:

    $request = new ODataRequest($method, $this->baseUrl.$requestUri, $this, $this->entityReturnType);

    if ($body) {
        $request->attachBody($body);
    }

    return $request->execute();

So, it's going to use the base url you initialized on your ODataClient and concatenate the uri you pass to the post method.

So, as long as you're passing a valid Uri to the Dynamics instance, you shouldn't get a 404. The 404 indicates there is most likely something incorrect in the passed Uri and Dynamics is balking at it.

You may need a forward slash before 'salesQuotes` or at the end of your baseuri.

Try that and see if you get past the 404.

anderly commented 1 year ago

I am accessing an API that returns OData

https://mri.cts-mrp.eu/portal/v1/odata/ProductSearch?$filter=(UpdatedAt%20ge%202022-12-27T18%3A54%3A09%2B08%3A00)%20and%20(UpdatedAt%20le%202023-01-27T18%3A54%3A09%2B08%3A00)%20and%20(domainKey%20eq%20%27h%27)&$expand=activeSubstances,atcCodes,doseForms,rms&$count=true&$skip=10&$top=10

I'd like to access it via the OData client - how do I build the filters etc via the OData client? or should I just build the URL as a base?

Also I was wondering the process of pulling all the data down based on the value in "@odata.count"?

Thanks

After initializing a new ODataClient with your base Uri of `https://mri.cts-mrp.eu/portal/v1/odata'.

Then, you should be able to build that Url using the Query Builder methods like so:

    // Retrieve a subset of entities from the "People" Entity Set
    $products = $odataClient->from('ProductSearch')
                          ->where('UpdatedAt', '>=', '2022-12-27')
                          ->where('UpdatedAt', '<=', '2023-01-27')
                          ->where('domainKey', 'h')
                          ->expand(['activeSubstances','atcCodes','doseForms','rms'])
                          ->skip(10)
                          ->take(10)
                          ->get();

    // $products->count(); // <-- This will give you your count

See example calls here and test examples here.