Five Stars

Check the tRestClient response

Hello everybody!

 

I'm a newbie in Talend ESB Studio and I have a doubt.

 

I use the tRestClient component for call a REST api. The api always status code 200 (ok) and the return message where I found the result of the operation (sucess or fault).

 

So, what's the best way to check this conditions end returns the result to use of my job? 

 

Anybody can help me please?

 

Thank's in advance!

  • ESB
1 ACCEPTED SOLUTION

Accepted Solutions
Eleven Stars

Re: Check the tRestClient response

I didn't realise you have a Java background. That does change things a little. Unfortunately a lot of people asking questions in here do not have Java, so I generally start with non-Java solutions.

Given what you have said, I would achieve you goal in this way....

 

1) Connect a tFlowToIterate to your tRestClient. This will create globalMap (HashMap) variables out of the columns returned. The key to the values will be in the format {row}.{column}. So if you row is called "row1" and your column name is "body", the key to the value would be "row1.body". You would get to it using ...

globalMap.get(row1.body)

You will need to cast it to the correct type to use it.

2) You have a choice here. Either you can connect from the tFlowToIterate to a tJava (using the Iterate link) and do some Java String manipulation to get the success or failure status THEN load that to the globalMap and use in RunIf conditional logic. OR connect 2 RunIf connectors to your tFlowToIterate, then carry out the String manipulation AND conditional logic within your RunIf conditions. All Java String manipulation functionality is open to you, it is just a choice of which way you want to do it.

3) Connect your RunIfs to appropriate tRestResponse components

 

Rilhia Solutions
10 REPLIES
Community Manager

Re: Check the tRestClient response

Hi
tRestClient returns a Document, link it to tXMLMap and extract the status data (success or fault).
tRestClient--main--tXMLMap--out1--->tLogRow

Regards
Shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
Five Stars

Re: Check the tRestClient response

Hi Shong, thanks for your reply!

 

OK, but how can I implement rules? In other words, how can I implement IF's inside the flow to decide what I'll return to client?

 

Thank you!

Eleven Stars

Re: Check the tRestClient response

There are a couple of ways. You can use a tMap to look at the data, make a decision, and route to a particular output. You can also use RunIf conditional connectors. There are potentially other ways as well. However it would be easier to point you in the right direction for your job if we could see a screenshot of it.

Rilhia Solutions
Five Stars

Re: Check the tRestClient response

Thank you rhall,

 

This is the job:

image.png

 

At the tRestClient_1 I need to get the JSON came from the Response, extract some variables from that, and then apply the business logic based on the values. Depending on this conditions, I have to response 200 or 400 with a JSON with error code and message.

 

What's the best way do do it?

 

Thank's again!

Eleven Stars

Re: Check the tRestClient response

OK, it looks like you may be on the right track, but you may need to a third scenario. You have your scenario for a web service error. However you need a scenario for a successful run which returns a good business result and a successful run which returns a bad business result.To cover these I would use a tMap connected to your tRestClient's success output. Look at the data, decide upon the success/failure logic and output to the appropriate tRestResponse

Rilhia Solutions
Five Stars

Re: Check the tRestClient response

Hi rhall! Thank's for you reply!

 

Yes, I got at this point! So, I don't know how to do these logic to decide what return to send because tMap component doesn't have trigger RunIF.

 

Have you any tip?

 

Thank you!

Eleven Stars

Re: Check the tRestClient response

For the tMap you simply need to set up two output tables. Then use conditional logic to decide which to output to. I'm a bit busy so can't give you a pictorial example relating to this, but take a look at step 3) (tMap_10) of this tutorial (https://www.rilhia.com/tutorials/vigen%C3%A8re-cipher). It shows how to go about optionally filtering which output from a tMap to use.

 

 

Rilhia Solutions
Five Stars

Re: Check the tRestClient response

Hello rhall, 

 

I hava Java background and I'm a little bit disappointed. To do a simple IF I'll have to map and filter the result into tMap component?

 

The REST call return this string case succeful execution:

success=true&token=7970654b694645475a776b66506a476749654547437a6a45686a7565516f6444506442515a645a6576496d51&id=da25ac1b20a02789f025b5db8f64eba3

 

Or in case of error: 

success=false&token=7970654b694645475a776b66506a476749654547437a6a45686a7565516f6444506442515a645a6576496d511&reason=Bad+Request%3A+Invalid+or+Expired+Token

 

If "success=true" I have to response 200 without body. In case of error ("success=false"), the return must be (400 - Bad request) with a field indicating the reason of the error.

 

After the tRestClient I put a tExtractJSONFields to extract success, token and id. Now I have to decide, based on success field which status to return.

 

Thanks agains for your help!

 

Luciano

Eleven Stars

Re: Check the tRestClient response

I didn't realise you have a Java background. That does change things a little. Unfortunately a lot of people asking questions in here do not have Java, so I generally start with non-Java solutions.

Given what you have said, I would achieve you goal in this way....

 

1) Connect a tFlowToIterate to your tRestClient. This will create globalMap (HashMap) variables out of the columns returned. The key to the values will be in the format {row}.{column}. So if you row is called "row1" and your column name is "body", the key to the value would be "row1.body". You would get to it using ...

globalMap.get(row1.body)

You will need to cast it to the correct type to use it.

2) You have a choice here. Either you can connect from the tFlowToIterate to a tJava (using the Iterate link) and do some Java String manipulation to get the success or failure status THEN load that to the globalMap and use in RunIf conditional logic. OR connect 2 RunIf connectors to your tFlowToIterate, then carry out the String manipulation AND conditional logic within your RunIf conditions. All Java String manipulation functionality is open to you, it is just a choice of which way you want to do it.

3) Connect your RunIfs to appropriate tRestResponse components

 

Rilhia Solutions
Five Stars

Re: Check the tRestClient response

Thank you very much!