3

We have set(6-8) of API and it will be invoked in a sequence . there are cases, want to use output of first/second API in the third/fourth API .

We have request object which is passed as input to all the APIs . we have defined all the response object in the same request and after each API execution corresponding response object will be set. what is the right way of passing the API outputs to the other API?

  public interface CeaseHandler<Request, Response> {

        public Response cease(final Request request);

    }

    First API

    public class CircuitCeaseHandler implements CeaseHandler<DeletionRequest, GenericApiResponse> {
      public GenericApiResponse cease(final DeletionRequest request) {
      }
    }

    Fifth API

    public class PhysicalPortCeaseApiHandler implements CeaseHandler<DeletionRequest, GenericApiResponse>{
     public GenericApiResponse cease(final DeletionRequest request) {
      }
    }

Here DeletionRequest has set attr which is output of first api and will be used in fifth api. ideally request shouldn't hold any api response.

1

You could mimic the middleware pattern here like, having a context object that lives throughout the sequence of API calls and store all the intermediate results there along with request and response objects.

Or,if you are ok with bloating the response request object you could simply piggy-back the Intermediate results within the response request object too.

This technique is commonly used in Golang and NodeJS web frameworks that use the middleware-style of creating web apps(not to chain APIs but to chain functionality),But you could copy the technique and apply it for chaining API calls too.

UPDATE : You could also use a map to hold your intermediates within the response object. So you wouldn't have to worry much about changing your code much to accommodate your newer apis.

I am not a java expert but you should have Thread safe Map implementation on JAVA (ConcurrentHashMap?) that you can utilize to go the thread safe way.

Here's a link that you might use as reference

http://crunchify.com/hashmap-vs-concurrenthashmap-vs-synchronizedmap-how-a-hashmap-can-be-synchronized-in-java/

  • Thanks Naresh. Either way ,I have to keep context or api response instance variable in Request object. Is that a correct way of designing an API. What I am thinking is request shouldn't hold anything about response attribute .if one more api is getting added later,changes must be minimal. – Selva Dec 5 '16 at 5:46
  • I'm sorry my bad. I was supposed to mean the response object and not request (I'll update my answer). Yep.Handling Changes would be a pain. You could use a Map that can hold various intermediates for you. This way you wouldn't have to change your code much to support adding new functionality. – Naresh Kumar Dec 5 '16 at 8:24

protected by gnat Feb 23 '18 at 6:51

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.