DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Delegated Transport, Forked Authorization: Where Chat and AI-Agent Integrations Actually Break
  • Contract-First Integration: Building Scalable Systems With Flyway, OpenAPI, and Kafka
  • The "Zombie API" Attack: Why Your Old Integrations Are Your Biggest Security Risk
  • 6 Integration Patterns That Look Good on Paper and What Happens When They Hit Production

Trending

  • Beyond Manual Mapping: AI-Orchestrated Governance for Hybrid Cloud
  • Build a GitHub Contribution City With Java and Quarkus
  • The Man Who Invented Selenium Also Built Its Replacement
  • Every Generation Fears the Machine. Here Is How the AI Job Panic Usually Ends

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Managing Multiple APIs Using an Adapter Pattern

Managing Multiple APIs Using an Adapter Pattern

Short guide on how to optimise HTTP API integrations in B2B business.

By 
Mykyta Khomenko
·
Mar. 07, 23 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
4.3K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Let's imagine the problem: Your company creates a B2B product that provides a service for some other companies. This service helps client-company to resolve very specific problems of their customers. This customer can communicate with our company web app and do some operations. The results of these operations should be reported to the other company side. 

business relations showcase

Times go on, and now you have a few more clients that work by the same scheme. At first, you connect small companies one by one, but when you get to much bigger client companies, you discover that they use third-party API service providers and delegate all the integration problems to them. These API service providers act as a proxy for this toil of service reverse integration, which unifies the logic of cross-company communication on both sides. Due to the structure of presented business relationships, our company almost inevitably start to use a set of similar actions to communicate with dozens of different APIs for hundreds of clients in the future. 


B2B communication as is

To make things easier, we should unify the communication logic between our services and external APIs.

The main idea is to create a single scalable transaction service and use it to transform some kind of unified DTOs into API-specific information and vice versa.

scalable transaction service

For HTTP, our adapters should be able to:

  • Transform unified request DTO into API-specific request DTO
  • Transform API-specific response DTO and HTTP status code into unified response DTO

To do so, firstly, we need to understand the building blocks of our communication.

HTTP request consists of:

  • HTTP method;
  • URL path;
  • URL params;
  • headers structure;
  • request body (any format: JSON, XML, plain text).

HTTP response consists of:

  • Response headers;
  • Response body (may also contain an error code that must be combined with the HTTP status code and translated into a unified error code format).

All items listed above are illustrated on the next image: 


request & response structure

It's important to understand which elements of communication may vary between API providers and which are client-specific. For example, in API URL: protocol, domain name, and port are Client specific parts, but the request URL path and URL params are API-specific.

After we know what elements should be unified in our adapters, we need to make sure we always use the right adapter for our request. The easiest way to do so is to use some unique ClientId to identify an API unique keyword from the database. After getting one, we can simply use static Map<API_KEY, ApiAdapter> to pass our data further. 

It is highly recommended for unified request data to have full information about entities. This way, you will never experience a lack of information on the API adapter's side. For example, if a request into your transaction service contains the 'ClientId' field, consider getting full ClientEntity [clientId, apiKey, mainCurrency, URL_protocol, domain, ... ] from the database and pass it into your adapter (do it any way you are comfortable with).

Adapter holder

After combining these 2 simple steps, we got the next code structure:

Now, whenever you need to do API reverse integration, you simply need to add new request and response adapters and do all necessary API-specific transformations in one place.

API Adapter pattern Integration

Opinions expressed by DZone contributors are their own.

Related

  • Delegated Transport, Forked Authorization: Where Chat and AI-Agent Integrations Actually Break
  • Contract-First Integration: Building Scalable Systems With Flyway, OpenAPI, and Kafka
  • The "Zombie API" Attack: Why Your Old Integrations Are Your Biggest Security Risk
  • 6 Integration Patterns That Look Good on Paper and What Happens When They Hit Production

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook