Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re^3: DHL API Integration

by Marshall (Canon)
on Mar 29, 2022 at 01:25 UTC ( #11142473=note: print w/replies, xml ) Need Help??

in reply to Re^2: DHL API Integration
in thread DHL API Integration

Hi espero,

Thanks for more info that put your application into context. I don't know what the Postman application is, nor have I made any serious study of the DHL JSON API. However, my understanding is that they have a test URL, where you can "practice" sending requests without actually triggering a shipment. It also appears to me that you just need a very small subset of the DHL API's capabilities. It also appears that there is really only one "user" for your application which will be a custom deal for your company (no need to bullet proof a complete turnkey application for some unknown person).

I recommend starting simple. Program #1: Get the address (and perhaps package weight? or whatever else you need) from your existing order system and print that to the screen. Program #2: Take output of prog #1 and format it into the JSON format needed by DHL, send it as a shipment request and see what you get back (perhaps at first just print their JSON response to the screen). Get the "good machine" case working. Then intentionally booger the address like maybe no postal code or whatever you can ad hock dream up - see how "well behaved" the DHL I/F is in actual practice with "bad" input. You will need to handle all error possibilities from the DHL spec, but at first just hack around and discover things by playing...The purpose being to gain experience with Perl and looking at JSON.

The DHL CPAN module mentioned earlier is likely to be a lot more trouble than it is worth to you. When I briefly looked at it, one of its main purposes was to process what are called Asynchronous Requests. Basically this allows you to have multiple outstanding requests at a time. If you do that, you can cram more requests per second into DHL at the cost of an order of magnitude of complexity. Do not do that. Send a single request, wait until DHL has processed that request and replied with "the answer". Then do the next request, etc. You do not describe a "high volume" application.

I don't think the YAML spec will be of much use either. That is basically a general data description language which does not tell you exactly the JSON format that DHL expects for that data. This "I have a Postman JSON collection file which contains sample requests for all of the DHL services and I was able to get a response form the server." sounds a lot more promising.

You will need to do the homework on what parts of the DHL API you need to implement (and I suspect this subset is tiny).

The Monks can help with refining your Perl code. This is best done by starting to write some code yourself. Then when you get into trouble or just want coding advice, post a runnable code example on SOPW. It doesn't have to be completely working - but should be enough so that a Monk can just click on it and run it themselves to replicate your error condition. "use warnings;" and "use strict;" and get rid of all syntax errors. And of sourse should have the dummy credentials and test URL that you are using.

This is a big "first Perl project". Start with one simple piece at a time. I would look at Perl JSON module, encoding and decoding a Perl data structure into JSON. If you have it JSON::XS is faster. XS typically means a completely compatible version written in C so that it runs faster. However, in your case, the data structures that you need to send and receive are "small" and this probably makes no significant difference.

Update: Now that I think about it, you say you have some properly formatted JSON examples. Start by using the JSON module to translate one of your examples into a Perl data structure. You can use Perl Data Dumper to see what kind of thing you have. Then write your own Perl code to access the created Perl structure and print each element yourself. To send JSON to DHL, you will have to reverse the process, create Perl Data Structure, then encode it.

Update2: So I guess basic workflow is like this:
1. get a shipment request with address, etc. from your existing web system.
2. translate that data into a Perl data structure containing all of the elements that DHL will need for a valid shipping request.
3. encode that Perl Data Structure into JSON
4. send JSON formatted request to DHL
5. see what DHL has to say about that request (decode their JSON formatted response).

As first steps, (1) figure out how to get the "triggering event" from the existing system. (2) work on sending and receiving the JSON formatted info between your app and DHL.

If you are successful with this first part, requirements will expand to potentially "solving global hunger level". Don't start with the idea of "solving world hunger". Start with making your shipping process incrementally better.

Replies are listed 'Best First'.
Re^4: DHL API Integration
by espero (Novice) on Apr 08, 2022 at 13:49 UTC

    Hi Marshall,

    Thank you very much for your detailed reply and help, the above is very useful. With regards to “sending and receiving the JSON formatted info between your app and DHL” I was wondering if you could recommend any Perl books or documentation which would cover this?

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11142473]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2023-01-28 00:23 GMT
Find Nodes?
    Voting Booth?

    No recent polls found