Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Design Pattern for Creating API URL

I am building a class library that interacts with various 3rd party API's. I have used an facade pattern to provide simplified access to complicated and confusing calls, and a factory pattern to return the correct implementation. I am now trying to build one of the implementation but cant think of an elegant design.

The implementation i am building requires a URL to be constructed (which i am doing via URIBuilder). I then need to "execute" the url. I then deserialize the Xml result into a class.

I am planning on using HttpClient to call the api with the URI i built, but am not sure on how to structure the class. The options i have thought of are:

  1. A base class of my implementation so can call it via base.InvokeURI(Uri myUri).

  2. A seperation class so it can be used by multiple implementations

I am also unsure where the deserialization should reside.

like image 289
Ketchup Avatar asked Jan 21 '13 08:01

Ketchup


People also ask

Which design pattern is used in REST API?

RESTful APIs should take advantage of HTTP methods, or verbs, such as GET, PUT, and POST. RESTful API Design Patterns: API design patterns provide a description or templates to solve specific, recurring API design problems that any software architects and API designers would like to adopt in their API designs.

What is API URL structure?

The API URL is structured as follows: {tenant url}/{base api url}/{business object name}({'business object unique key'})?$ {query parameter and value}

What design pattern is an API?

APIs are contracts that define how applications, services, and components communicate. API design patterns provide a shared set of best practices, specifications and standards that ensure APIs are reliable and simple for other developers to use.


2 Answers

I think using Interface in this case is more suitable:

public interface IURLInvoke 
{
    string InvokeURI(Uri myUri);
}

// some implementation
public class YourURLInvoker: IURLInvoke 
{
    public string InvokeURI(Uri myUri)
    {
         // do something
    }
}

public class YourClass 
{
    public IURLInvoke Invoker {get; set;}

    public void InvokeURI(Uri myUri)
    {
         if(Invoker == null)
              return; 

         string xml = Invoker.InvokeURI(Uri myUri);
         // put your code for deserialization here
    }
}

// here is an usage example:
YourClass a = new YourClass();
// set an Invoker, choose a strategy to invoke url
a.Invoker = new YourURLInvoker();
a.InvokeURI(url);

This approach is also called Strategy Pattern

like image 141
phnkha Avatar answered Nov 06 '22 22:11

phnkha


Pls see dummy code using adapter pattern and dependency injection. Idea is to create a interface and pass it around

public class Adapter{
 public void processRequest(){
   RequestProcessor processor = new RequestProcessor();
   processor.processRequest();
 }
}

public class RequestProcessor{
  public void procesRequest(){
    Irequest request = new HTTPRequest();
    HTTPService service = new HTTPService();
    // fetch the uri from builder class
    URI url = URIBUIlder();
    string response = service.sendRequest(request,url);
    // now fetch type from just 
    Type t = Serializer.searialize<T>(response);

  }
}

public Class Serializer{
  public static  T searialize<T>(string xml){
  }
}

public interface IRequest{
 public string sendRequest(uri url);
}

public class HTTPRequest:IRequest{
 public string sendRequest(uri url){
  // instantiate actual http request here and return response
 }
}

//This will act as controller
public class HTTPService{
 public string sendRequest(IRequest request,uri url) {
  return request.sendRequest(url);
 }
}
like image 42
Ajay Beniwal Avatar answered Nov 06 '22 21:11

Ajay Beniwal