Comcast/eel

Remove object from every response

g10chy opened this issue · 5 comments

I have a very basic requirement for eel to act as a proxy and simply filter out a particular object for every JSON response from a server and pass it back to client.

Just like this:

./eel -in='{"items":[{"asset_id":8001,"db_id":111300,"pdb_id":1180,"pdb_name":"TEST1","purchased_gb":500,"allocated_gb":295,"used_gb":246},{"asset_id":7945,"db_id":111300,"pdb_id":1161,"pdb_name":"TEST2","purchased_gb":100,"allocated_gb":10,"used_gb":7}]}' -tf='{"{{/}}": "{{/items}}"}' -istbe=false

However, I can't see to get the configuration working. Appreciate any pointers, thanks.

When you launch eel out of the box it will start listening for events on port 8080 using a default handler stored here https://github.com/Comcast/eel/blob/master/config-handlers/tenant1/default.json

The easiest thing to do would be to modify this default handler so it performs your transformation. You also need to adjust the endpoint you want to forward the transformed event to. Something like this should do:

{
    "Version": "1.0",
    "Name": "Default",
    "Info": "Default handler for everything. Doesn't apply any transformation and forwards everything unchanged to Endpoint/Path.",
    "Active": true,
    "Match": null,
    "IsMatchByExample": false,
    "TerminateOnMatch": true,
    "Transformation": {
        "{{/}}": "{{/items}}"
    },
    "IsTransformationByExample": false,
    "Path": "",
    "Verb": "POST",
    "Endpoint": "http://myproxytarget",
	"Protocol": "http",
    "HttpHeaders": {
      "X-B3-TraceId": "{{traceid()}}",
      "X-Tenant-Id": "{{tenant()}}"
    }
}

Hope this helps!

Thanks Boris. Unfortunately this doesn’t help my required.

I need to:

Send GET request to eel and forward to target server as is.
Transform the JSON response from target server and pass onto the originating client.

Is this something eel can do?

Yes, you can do this with eel as well. First of all you need to hit a different endpoint of eel: use /v1/sync/events to receive a synchronous response from eel rather than the default /v1/events which is used for event forwarding.

Next you might find the various built-in functions eel has useful. They are documented here:
https://github.com/Comcast/eel/blob/master/doc/functions.md

I put together an example for you based on your requirements using the functions curl(), eval() and prop(). I'm also using a custom properties section in the handler to manage the request against your service.

{
    "Version": "1.0",
    "Name": "Default",
    "Info": "Hit a remote service with the original payload and synchronously return a transformed response to original requestor.",
    "Active": true,
    "Match": null,
    "IsMatchByExample": false,
    "TerminateOnMatch": true,
    "Transformation": {
        "{{/}}": "{{eval('/items','{{prop('resp')}}')}}"
    },
    "CustomProperties" : {
        "resp" : "{{curl('GET', 'http://foo.com/bar', '{{/}}')}}"
    },
    "IsTransformationByExample": false,
    "Protocol": "null",
    "HttpHeaders": {
      "X-B3-TraceId": "{{traceid()}}",
      "X-Tenant-Id": "{{tenant()}}"
    }
}

Give this handler a try. You will probably have to fine tune it a little.

Thanks for the pointers Boris.
I have configured as suggested above and it is working perfectly with POST to http://localhost:8080/v1/sync/events

However, my original requester can only issue GET (limitation in the application). But when I try using GET to get the response I have the following:

Browser output is:
{"error":"http post required","tx.traceId":"e47ead52-b2a9-40c1-8b11-2a4c10a2507e"}

Console output is:
{"action":"rejected","app.id":"eel","cause":"http_post_required","env.name":"default","error":"post required","error_type":"rejected","instance.id":"xxxxxxx","log.id":"e47ead52-b2a9-40c1-8b11-2a4c10a2507e","log.level":"error","log.timestamp":"2019-07-23T08:41:08.577866555Z","method":"GET","status":"400","tx.traceId":"e47ead52-b2a9-40c1-8b11-2a4c10a2507e"}

It is possible for eel to accept GET?

Interesting. Before I was still assuming that your original request was posting a payload you wish to forward to your target service. If this is not the case and you simply want to kick this off with a payload-free GET request you are out of luck. EEL is designed to work around an incoming http POST and a (possibly empty) payload that is sent along with it.