apiaryio / api-blueprint

API Blueprint
https://apiblueprint.org
MIT License
8.65k stars 2.14k forks source link

Response based on parameter values #58

Open martijnschouwe opened 10 years ago

martijnschouwe commented 10 years ago

Hi

I don't know if it is already possible as i could not find something in the examples/documentation. Can a response be conditional based on a parameter value in the request? e.g.

If it is not possible this would be a nice feature.

Cheers!

zdne commented 10 years ago

Hey @martijnschouwe

thanks for sharing the idea! Actually something very similar is planned as it is much needed for full support of the testing #21 (also refer to #40 )

Like your idea by appending if ... after the response definition, not sure how it would work for more thank one or two parameters tho.

I am thinking more like pairing request with responses to form a transaction pair / examples.

Something along these lines:

# Resource [/resource/{param1}/{?param2,param3}]

+ Parameters
    + param1 ... bla
    + param2 ... bla bla
    + param3 ... bla bla bla

## Retrieve [GET]

+ Request 1
    + Parameters
        + param1: 42
        + param2: A 
        + param3: X

+ Response 200 

        ...

Thoughts?

martijnschouwe commented 10 years ago

@zdne Pairing is always a nice way indeed as it is clear at one glance rather then to interpret a if statement, good one! :+1:

alyssaq commented 10 years ago

+1 I would love this feature

sergiofbsilva commented 10 years ago

need this pretty bad :+1:

gaffney commented 10 years ago

Ditto, we had to put quite a bit of JavaScript hackery to work around this feature deficiency.

... which will likely break as soon as we upgrade versions :/

zdne commented 10 years ago

@sergiofbsilva @Gaffney Could you please elaborate a bit on our use cases? Why do you need it and / or what JS hackery you had to use?

Also is the proposed syntax OK (feel free to throw in anything else)?

gaffney commented 10 years ago

Hey @zdne,

tl;dr

#js-hacks #multiple-examples #custom-urls

Syntax looks fine.

I can give you access to the aforementioned API documentation if you email me.


Summary of some issues we ran into (most of which are related to this one) plus our work-arounds below:

zdne commented 9 years ago

@Gaffney

When we attempted to add multiple requests and responses directly from the .apib file, Apiary listed all requests first and then all responses.

I believe this should be now fixed in Apiary.

Apiary’s “explode modifier” is of no use to us as we use semicolon-delimited arrays instead of the common repeating parameter x=v1&x=v2&...&x=n format.

Can you elaborate on this? I am trying to find out whether this is the limitation of API Blueprint or Apiary (or both)

Query parameters may also have additional attributes, specified after a space (e.g. groupings=location;year+verbose=true).

After a space? Again can you give me an example?

Thanks!

gaffney commented 9 years ago

Apiary’s “explode modifier” is of no use to us as we use semicolon-delimited arrays instead of the common repeating parameter x=v1&x=v2&...&x=n format.

For example, using commas, user_ids=1,2,3,4 instead of user_ids=1&user_ids=2&user_ids=3&user_ids=4

After a space? Again can you give me an example?

The above groupings=location;year+verbose=true was an example (+ is a url-encoded space). There are many other possible scenarios, such as the query structure the Facebook's Graph API uses, which would not fit into the Apiary model.

margru commented 9 years ago

This will be a great feature but will it cover also POST requests, i.e. so that it would be possible to define different responses on the basis of the POST payload?

zdne commented 9 years ago

Hey @BlackMan82, this is currently in the development as #25 – you will be able to describe request (or any other) attributes and the follow with appropriate response.

margru commented 9 years ago

Great! I hope it arrives soon :)

zdne commented 9 years ago

What @arekkas proposes here:

## The translation API [/i18n/{id}{?locale}]

+ Parameters

    + locale (required, string, `de_DE`)
    + id (required, int, 1)

### Retrieves a translation [GET]

+ Request Volkswagen
    + Parameters

        + locale: de_DE
        + id: 1

+ Response 200 (application/json)

    { "message": "Volkswagen" }

+ Request Ford
    + Parameters

        + locale: de_DE
        + id: 2

+ Response 200 (application/json)

    { "message": "Ford" }

Is another good example how this should be implemented

richthegeek commented 9 years ago

+1 on this, seems like a very obvious missing feature! Let me know what I can do to help get it working.

zdne commented 9 years ago

Updated proposal (to latest URI params syntax):

# Collection [/collection{?limit}]
## List [GET]

+ Parameters
    + limit (number) - Maximum number of entries returned

+ Request Limit to One Entry
    + Parameters
        + limit: 1

+ Response 200 (application/json)            

        [ {} ]

+ Request Limit to Three Entries
    + Parameters
        + limit: 3

+ Response 200 (application/json)

        [ {}, {}, {} ]

Please let me know if this will work for you @netmilk

netmilk commented 9 years ago

@zdne Thank you for the proposal! It perfectly works for me for adding or changing parameters and its values. More general question is, and maybe it's more MSON related, is how to dereference (exclude,remove) some property.

So, related to your example, question is how to remove the limit parameter from the Request Limit to Three Entries to not be sent in the URI even if it is discussed under the Action section.

pit3k commented 9 years ago

+1 Great feature. And we badly need it.

altsanz commented 9 years ago

+1 on this. It would be amazing!

tuo commented 9 years ago

+1. it is huge pain for us, hopefully it could get implemented asap

tony612 commented 9 years ago

+1 We need it too!

ifeins commented 9 years ago

+1 Would be super helpful to have this feature. In addition to pairing responses to requests by parameters it would be also useful to do the pairing according to headers.

zdne commented 9 years ago

@ifeins fairly high on our list – will start working on it soon™ :dancers:

marko85to commented 9 years ago

+1 Super feature, really needed!! Would be great to have it!

jirikrepl commented 9 years ago

Yes yes this would be awesome for me to http://stackoverflow.com/questions/31161915/more-get-request-response-examples-with-different-uri-parameters

shavo007 commented 9 years ago

+1

ghost commented 9 years ago

+1

ghost commented 9 years ago

@netmilk

how to dereference (exclude,remove) some property

Maybe by considering that Parameters list is not inherited and has to be redefined for each Request... What do you think?

narg95 commented 9 years ago

Hello all, What is the status of this nice feature ? Is it plan to be released soon ?

Thanks, Nestor

efavre commented 9 years ago

Hi,

Would love to see this too.

mlovretovich commented 9 years ago

+1

lyttlegy commented 9 years ago

+1

edude03 commented 9 years ago

:+1: any movement on this?

zdne commented 9 years ago

No updates, besides the fact it is currently at the top of our todo list together with authentication syntax.

dysbulic commented 9 years ago

:+1: I'd love to be able to mock enough responses the teams using the API can work as I implement.

hendrikmaus commented 9 years ago

+1

ruipinge commented 9 years ago

+1

jcolaco-nmz commented 9 years ago

+1

jdduarte commented 9 years ago

+1

craigryan commented 9 years ago

+1

poshaughnessy commented 9 years ago

:+1:

vatson commented 9 years ago

:+1:

dysbulic commented 9 years ago

I put down some money for whoever does this: https://www.bountysource.com/issues/2007613-response-based-on-parameter-values

SukitZhang commented 9 years ago

+1

nicstrong commented 9 years ago

+1

makowskid commented 9 years ago

+1 ???

zdne commented 9 years ago

Big thanks to everyone expressing your support on this issue! I want to ensure you we still have this on the top of our feature list.

I feel a status update is appropriate here: At the moment, we are all working on radically changes in the parser toolchain so everyone can build new tools on the freshly introduced MSON. Once we clean & tighten up our parser toolchain we will get back to introducing new, much needed, features.

Please bear with us! Thank you 👍

PS: Of course any contributions are welcome!

OrCharles commented 9 years ago

@zdne I would like to help. Can you point me to where you're looking for help specifically? Anything troubling?

zdne commented 9 years ago

Hey @OrCharles, thanks for offering hand!

In order to move this forward – 3 things are needed:

  1. Verify and formally (Pull request) propose the syntax including examples
  2. Make sure API Blueprint AST and API Description Refract namespace can hold the information proposed in 1.
  3. Implement parsing in the API Blueprint parser – Snow Crash

I guess we have to start from step 1 – so if you are up for it to formalize the subject including some examples it would be great !

tomasz-scislo-xstream commented 9 years ago

+1

khaliddermoumi commented 8 years ago

+1