Multiple response for a request based on a value


#1

Hi all,

I am relatively new to RAML just 4 days old.But i have a task to be implemented.

I have an offer API which accepts the offerType as request,and it should return the response offers based on the offerType.

  1. If offertype is discount1,it should return the response corresponding to discount1.
  2. If offertype is discount2,it should return the response corresponding to discount2.

Scenario 1:

Request :

{“OfferType”: “discount1”}

Response:

{“DiscountOffers1” : “10%”}

Scenario 2:

Request :

{“OfferType”: “discount2”}

Response:

{“DiscountOffers2” : “50%”}

Is it possible to achieve this?

This should be achieved with a single API.

Thanks in Advance.


#2

You can easily design the API in RAML, yes… but what you talk about is an implementation detail on your service. As long as you are returning the same (or subset) response type, you can implement it however you like in your service. If however you are talking about returning completely different response types from the same URL, I would say that is a bad resource design. From your example, you are just returning different values for the same response key DiscountOffers. In the service code you would add some logic like if (offerType == discount1) { return {DiscountOffers: 10%} } else if (offerType == discount2) { return {DiscountOffers: 50%} };

Of course your implementation will vary, but something along those lines. At any rate it is up to your implementation to solve that.

Now, if you have specific offer types that are always part of your API and you want to document those for API consumers, you can provide multiple response examples in RAML 1.0 (not RAML 0.8) but it would seem odd to me in this scenario because I would suspect under the context, a discount voucher may change much more rapidly than you would want to change your API.


#3

Thanks for the reply Kevin.

Here is what i have done so far.But when i test the service in API Designer,it is showing "body":"!$$$novalue"
and

{
  "upselloffers": "!$$$novalue"
}

api5.raml:

#%RAML 1.0
title: OfferService5
version: 1.0
#baseUri: http://www.offer5.com/api
#baseUri: https://mocksvc.mulesoft.com/mocks/48992175-43c0-4798-82fc-e93beeef4b19/api
baseUri: https://mocksvc.mulesoft.com/mocks/f00bf919-e1aa-418a-a3df-1a7076d6b5d4/mocks/48992175-43c0-4798-82fc-e93beeef4b19/api
/offers:
  displayName: offers
  post:
    description: abc
    body:
      application/json:
        examples: 
          - upsellreq: 
              body: !include upsellreq.json
              description: An upsell example
              responseRef: upselloffers
          - crosssellreq:
              body: !include crosssellreq.json
              description: A crosssell example
              responseRef: crossselloffers
        
          
    responses:
      200:
        body:
          application/json:
            examples: 
              - upselloffers: !include upselloffers.json
              - crossselloffers: !include crossselloffers.json
            
      500:
        body:
          application/json:
            example: |
                 {
                  "errorMessage": "The offer couldn't be found."
                 }

crosssellreq.json:

{
 "offerType":"crosssell"
} 

upsellreq.json:

{
"offerType":"upsell"
} 

upselloffers.json:

{
  "successFlag": true,
  "offerType": "upsell",
  "upsellOffer":"10%", 
  "crosssellOffer": null
}

crossselloffers.json:

{
	"successFlag": true,
	"offerType": "crosssell",
	"crosssellOffer":"10%",
	"upsellOffer": null
}