api-blueprint icon indicating copy to clipboard operation
api-blueprint copied to clipboard

Response based on parameter values

Open martijnschouwe opened this issue 10 years ago • 117 comments

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.

  • Parameters
    • where (string, optional) ...
      • Values
        • Name eq 'My Name'
        • Name ne 'My Name'
  • Response 200 (application/json) if Name eq 'My Name' response json x
  • Response 200 (application/json) if Name ne 'My Name' response json y

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

Cheers!

martijnschouwe avatar Feb 12 '14 11:02 martijnschouwe

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?

zdne avatar Feb 13 '14 08:02 zdne

@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:

martijnschouwe avatar Feb 14 '14 09:02 martijnschouwe

+1 I would love this feature

alyssaq avatar May 06 '14 09:05 alyssaq

need this pretty bad :+1:

sergiofbsilva avatar Jul 01 '14 01:07 sergiofbsilva

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 :/

gaffney avatar Jul 01 '14 02:07 gaffney

@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)?

zdne avatar Jul 01 '14 07:07 zdne

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:

  • We ended up hosting CORS-enabled assets to inject sections of the page so the apiary.apib wasn’t cluttered with ugly HTML. This relates to this issue.
  • Our main resource is polymorphic in both input and output, which naturally results in having multiple use cases. When we attempted to add multiple requests and responses directly from the .apib file, Apiary listed all requests first and then all responses. To work around this, we interleave "stories" (example descriptions) and URL-modified requests with the responses.
  • Our query parameter syntax does not follow that of a typical URI, and Apiary is currently not flexible enough to handle this. For example, 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. Query parameters may also have additional attributes, specified after a space (e.g. groupings=location;year+verbose=true). Together these customizations allow for concise, clean-looking JSON-less/flat queries.
  • "Try me" only works for 1:1 request/response resource examples, so we replaced all occurrences of the generated mock domain with some bogus domain, and removed the “Try me” button.

gaffney avatar Jul 01 '14 23:07 gaffney

@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!

zdne avatar Oct 30 '14 11:10 zdne

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.

gaffney avatar Oct 30 '14 17:10 gaffney

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?

margru avatar Dec 11 '14 12:12 margru

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.

zdne avatar Dec 11 '14 12:12 zdne

Great! I hope it arrives soon :)

margru avatar Dec 11 '14 12:12 margru

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

zdne avatar Dec 25 '14 16:12 zdne

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

richthegeek avatar Jan 10 '15 17:01 richthegeek

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

zdne avatar May 01 '15 18:05 zdne

@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.

netmilk avatar May 03 '15 08:05 netmilk

+1 Great feature. And we badly need it.

pit3k avatar May 05 '15 18:05 pit3k

+1 on this. It would be amazing!

altsanz avatar May 10 '15 11:05 altsanz

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

tuo avatar May 11 '15 15:05 tuo

+1 We need it too!

tony612 avatar May 21 '15 06:05 tony612

+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.

ifeins avatar May 31 '15 10:05 ifeins

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

zdne avatar Jun 01 '15 08:06 zdne

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

marko85to avatar Jun 15 '15 12:06 marko85to

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

jirikrepl avatar Jul 02 '15 09:07 jirikrepl

+1

shavo007 avatar Jul 03 '15 04:07 shavo007

+1

ghost avatar Jul 06 '15 16:07 ghost

@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?

ghost avatar Jul 06 '15 16:07 ghost

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

Thanks, Nestor

narg95 avatar Jul 14 '15 16:07 narg95

Hi,

Would love to see this too.

efavre avatar Jul 17 '15 12:07 efavre

+1

mlovretovich avatar Jul 18 '15 05:07 mlovretovich