How to use factory pattern to resolve multiple third party apis
Asked Answered
A

1

5

I have been working on an API which is based on Laravel framework in PHP, but this is not specific to the language. There is a number of third party APIs are used here in the API and have a number of multiple configurations. So I would like to create an HTTP client factory class, and in the code, I am planning to create an object for this and passing the API name as a parameter. The main issue is how can I resolve this to various classes according to the name of particular API? That is when I give Google, it needs to initialize the Google class and return the Google API client and for other APIs, it should respond with the corresponding client.

I have a confusion like this is a right use case for factory pattern and if it is not, is there any other patterns or standard methods for doing this instead of calling each of the API clients separately.?

Apfelstadt answered 9/8, 2017 at 10:2 Comment(1)
As of now, it is separate classes without any association. Yeah, all the classes should be used in the same manner. Once I get the client, I can call specific methods for the particular client.Apfelstadt
C
8

Here, you need to use a combination of Factory Method and Adapter pattern. Factory method will create your native API classes' objects(e.g. Google, etc.) and adapter will provide you a common interface. So basically you do the following:

  • Create an adapter interface for your API operations(list all the operations you need)
  • Implement the adapter interface for all different API's, by referring the third party API objects(You need to create different class for each API)
  • In your factory method, you return the type of your adapter interface.
  • Now, you can consume the object returned from factory method in the calling code.

Below is the sample code.This is using C#. You can do some modification of below code.

public interface IApiAdapter
{
    void Read(int id);
    void Write(string data);
}

public class GoogleApiAdapter : IApiAdapter
{
    private GoogleApiClass _googleApiClass;
    public void Read(int id)
    {
        //some additional work
        //call google api
        _googleApiClass.readSomeData(id);
    }
    public void Write(string data)
    {
        //some additional work
        //call google api
        _googleApiClass.writeSomeData(data);
    }
}

public class FacebookApiAdapter : IApiAdapter
{
    private FacebookApiClass _facebookApiClass;
    public void Read(int id)
    {
        //some additional work
        //call facebook api
        _facebookApiClass.readSomeData(id);
    }
    public void Write(string data)
    {
        //some additional work
        //call facebook api
        _facebookApiClass.writeSomeData(data);
    }
}

public class ApiFactory
{
    public static IApiAdapter GetApiFactory(string type)
    {
        if(type == "google")
        {
            return new GoogleApiAdapter();
        }
        if(type == "facebook")
        {
            return new FacebookApiAdapter();
        }
    }
}

//calling code
IApiAdapter api = ApiFactory.GetApiFactory("google");
api.Read(2);
Codd answered 9/8, 2017 at 10:42 Comment(1)
Thanks, Kunal. can you please provide some example code?Apfelstadt

© 2022 - 2024 — McMap. All rights reserved.