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

[feature-request] Allow usage of a response body as URI templates #42

Open
deedubs opened this issue Apr 2, 2015 · 2 comments
Open

Comments

@deedubs
Copy link

deedubs commented Apr 2, 2015

I would like to be able to use a response body as template parameters further down the chain.

An example api could be

Where `self' returns:

{
  id: 1,
  name: 'Agent 007'
}

and clients == 'http://host/agents/{agent_id}/clients'

  traverson
    .from('http://api.example.com')
    .follow({name: 'self', asTemplate:'agent'}, 'clients') 
    .getResource(function(error, document) {
      ...
    });
@basti1302
Copy link
Member

As said in this thread on Twitter, I think this is a good idea. I'll probably won't get around to implement this soon, because I'm still working on the traversal continuation feature whenever I have time to work on Traverson. If somebody else wants to take a shot at this, a PR would be most welcome.

I don't fully understand the example given here, though - I think it's a bit incomplete? I'll try to make up a more complete example, just to make sure we are on the same page:

Server Side Responses

http://api.example.com

{
  "agent_1": "007",
  "agent_2": "008",
  "agent_details": "http://api.example.com/agents/{agent_id}"
}

https://api.example.com/agents/007

{
  "id": "007",
  "name": "James Bond"
}

https://api.example.com/agents/008

{
  "id": "008",
  "name": "Mata Hari"
}

Traverson API

What we would like to do now is to fetch the first resource and use one of its attributes (for example, the value of the attribute agent_1, that is, "007") and feed this into the URI template agent_details to get to the resource at https://api.example.com/agents/007.

We need a way to tell Traverson about this. I don't think the follow method is what needs to change, this just tells Traverson which links are to be traversed, so in the example above it would still simply be follow('agent_details'). The proper place to tell Traverson about this is withTemplateParameters. It could look like this:

var traverson = require('traverson');

traverson
.from('http://api.example.com')
.follow('agent_details')
withTemplateParameters({
  agent_id: { 
    lookupStrategy: 'FROM_LAST_HTTP_RESPONSE',
    key: 'agent_1'
  }
})
.getResource(function(error, document) { ... });

This is to be read as: "Whenever you resolve a URI template and it has a URI template parameter agent_id, don't use a fixed substitution value but look up the value in the last HTTP response and use the value of the attribute agent_1. It would be nice if key could also be a JSONPath expression. The reason for the lookupStrategy thing: In future, we might conceive other sources for template substitution values.

@deedubs Is that roughly what you had in mind?

@basti1302
Copy link
Member

It's a pity that @deedubs never got back to me on this one... 😢 😏

@basti1302 basti1302 changed the title Allow usage of a response body as URI templates [feature-request] Allow usage of a response body as URI templates Feb 1, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants