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

Make headers of last response available trough (base) client #57

Merged
merged 6 commits into from
Jan 27, 2025

Conversation

sanderlissenburg
Copy link
Contributor

@sanderlissenburg sanderlissenburg commented Jan 24, 2025

resolves PIC-925

Voor het verkrijgen van de track and trace code van een verzend label moet je een header van het get shipment label endpoint uitlezen.

De client is zo gebouwd dat alleen de response body decoded en returned kan worden. Om het verkrijgen van de headers toch mogelijk te maken heb ik twee opties overwogen.

Zie comments hier onder. Uiteindelijk gekozen voor een oplossing waarbij er via de client de headers van de laatste response worden beschikbaar gesteld.

De eerste optie, die in deze pull request terug te zien is checkt op de $method === 'HEAD'. Zo ja/true, dan worden ALLEEN de headers returned. In het geval van het get shipment label endpoint is het namelijk mogelijk om met de method HEAD alleen de headers terug te krijgen. Wat handig is want je zal twee calls moeten doen bij het gebruik van deze client. Één voor de body, en één voor headers. Wil je dat in één call, dan zouden we de client grondiger moeten aanpassen. Zodat deze bijvoorbeeld de headers van de laatste call ergens beschikbaar maakt, of meer dan alleen de decoded body returned.

Een tweede optie die ik heb overwogen, maar uiteindelijk niet heb gekozen is het uitbreiden van de decodeResponse() method. Waarbij ik dan een nieuwe responseType zou introduceer genaamd headers.

    // return headers if response type is headers
    if ($responseType == 'headers') {
        return $response->getHeaders();
    }

Maar dit voelde weer niet goed omdat de decodeResponse method naam impliceert dat het om het decoden van de body gaat.

Twijfels:

Ik heb getShippingLabelHeaders toegevoegd aan de Client. Deze Client is echter auto generated, dus ik weet niet of het slim is om deze hier toe te voegen. Je zou ook in de applicatie (in ons geval Picqer) gewoon direct request() kunnen aanroepen.

… is needed for retrieving the track and trace.
…ng label endpoint. This contains the track and trace code.
@sanderlissenburg sanderlissenburg marked this pull request as ready for review January 24, 2025 10:55
@Majklas
Copy link

Majklas commented Jan 24, 2025

Hi there, sorry not to post to the needed thread, I'm new here on github.
Please, could you please tell me how can I send tracking numbers to the bol order via this API? If this client able to do so? If not, then is there any plans of making it happen?
Looking forward,

@casperbakker
Copy link
Member

Hi there, sorry not to post to the needed thread, I'm new here on github. Please, could you please tell me how can I send tracking numbers to the bol order via this API? If this client able to do so? If not, then is there any plans of making it happen? Looking forward,

@Majklas Yes, that is possible. The code is somewhat like this:

            $shipmentRequest = new Model\ShipmentRequest();
            $shipmentRequest->shipmentReference = $picklist->picklistid;
            $shipmentRequest->orderItems = $orderItems;
            $shipmentRequest->transport = $transportInstruction;
            $shipmentRequest->shippingLabelId = $shippingLabelId;

            $process = $client->createShipment($shipmentRequest);

But the required data is quite complex, so there is no easy example to give. Have a look at the Bol API docs, and then use the matching models in this client to create the shipment.

@casperbakker
Copy link
Member

@sanderlissenburg Als het werkt, dan kan het voor nu. Maar het is natuurlijk wel beroerd om geen headers te kunnen krijgen van het request wat je al gedaan hebt. Het kan zomaar dat deze HEAD request ook meetelt met je rate-limits, of andere vervelende side effects.

Nog een reden om een nieuwe (simpele) client te gaan maken.

@sanderlissenburg
Copy link
Contributor Author

Het kan zomaar dat deze HEAD request ook meetelt met je rate-limits, of andere vervelende side effects.

@casperbakker dat risico is wel groot bij bol. En extra vervelend als call 1 slaagde en 2 niet. Ik denk dat het dan slimmer is om de last response in memory (op een property) te cachen en dan via een 'lastResponse(): ?ResponseInterface' method op de client beschikbaar te maken. Kan daar de headers aan opvragen.

Copy link
Contributor

@ruudsnl ruudsnl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, dit is een betere oplossing dan een HEAD request. Voelt nog wel een beetje gek om de headers van een response aan de client te moeten vragen nadat de request al gedaan is, maar dit is wel de manier om het te doen zonder grote aanpassingen, dus lijkt me prima! 💪

@sanderlissenburg sanderlissenburg changed the title Voeg de mogelijkheid toe om (alleen) de headers van een response te verkrijgen. Make headers of last response available trough (base) client Jan 27, 2025
@sanderlissenburg sanderlissenburg merged commit 6892309 into main Jan 27, 2025
6 checks passed
@sanderlissenburg sanderlissenburg deleted the get-header-info-from-endpoint branch January 27, 2025 11:07
@Majklas
Copy link

Majklas commented Jan 27, 2025

Hi there, sorry not to post to the needed thread, I'm new here on github. Please, could you please tell me how can I send tracking numbers to the bol order via this API? If this client able to do so? If not, then is there any plans of making it happen? Looking forward,

@Majklas Yes, that is possible. The code is somewhat like this:

            $shipmentRequest = new Model\ShipmentRequest();
            $shipmentRequest->shipmentReference = $picklist->picklistid;
            $shipmentRequest->orderItems = $orderItems;
            $shipmentRequest->transport = $transportInstruction;
            $shipmentRequest->shippingLabelId = $shippingLabelId;

            $process = $client->createShipment($shipmentRequest);

But the required data is quite complex, so there is no easy example to give. Have a look at the Bol API docs, and then use the matching models in this client to create the shipment.

Ok, thank you, finally got it according to the needs. Please let me know how should I send that info? Which method?
Now I've tried using
$client = new \Picqer\BolRetailerV10\Client(); $client->authenticateByClientCredentials('45555', 'XXXX'); $process = $client->shipOrderItem($shipmentRequest); var_dump($process);
and got the error for :
Fatal error: Uncaught Picqer\BolRetailerV10\Exception\ResponseException: Unauthorized request in BOL/vendor/picqer/bol-retailer-php-client/src/BaseClient.php:526
Is there some kind of user permission limits or just wrong function/method call?
Some test practices would be nice to see for this one in readme too :)

@casperbakker
Copy link
Member

@Majklas There are no different permission roles in the Bol API as far as I know. So the auth credentials you provided are probably wrong. That is what "Unauthorized request" means.

Maybe easier to check the auth with a simple GET call, for example to get the open orders. Once the auth works with that endpoint, you can move over to this more complicated one.

@Majklas
Copy link

Majklas commented Jan 28, 2025

@Majklas There are no different permission roles in the Bol API as far as I know. So the auth credentials you provided are probably wrong. That is what "Unauthorized request" means.

Maybe easier to check the auth with a simple GET call, for example to get the open orders. Once the auth works with that endpoint, you can move over to this more complicated one.

Yeah, creds are ok, I can get the needed order list and orders accordingly with them. So maybe wrong method?
Any of your clients use this feature? Could you please post me with the solution?

@casperbakker
Copy link
Member

@Majklas I cannot help you further, sorry. You have to debug it yourself.

We use this feature ourselves, the method in the client works.

@Majklas
Copy link

Majklas commented Jan 31, 2025

funny story though.. I just wanted to post here. We had a server where all of apis are working, but seems admin installed the last version of your module which was capable on that server according to php version. Seems like it was missing in Client.php that createShipment method. Thank you, we moved to new fresh php and updated your module, now all is working.
Thank you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants