Accessing Web Resources using Factory Method Design Pattern in Python


The Factory Method Design Pattern of Python is a creational pattern that provides an interface for creating objects in a superclass but allows subclasses to alter the type of objects that will be created. It is used to define a generic interface for creating objects while allowing subclasses to decide which class to instantiate. In this article, we will explore how to use the Factory Method Design Pattern to access web resources in Python.

Accessing Web Resources

Python provides several libraries for accessing web resources such as HTTP requests, urllib, and Requests. These libraries allow us to send HTTP requests to a server and receive a response. However, choosing the right library can be daunting, especially when working on large projects with multiple dependencies. To solve this problem, we can use the Factory Method Design Pattern to create a common interface for accessing web resources and let subclasses decide which library to use.

Let's start by defining the abstract class for our factory −

from abc import ABC, abstractmethod

class WebResourceFactory(ABC):

   @abstractmethod
   def get(self, url):
      pass

   @abstractmethod
   def post(self, url, data):
      pass

The WebResourceFactory class is an abstract class that defines the interface for accessing web resources. It has two abstract methods: get() and post(), which will be implemented by the concrete subclasses.

Next, let's create two concrete subclasses that implement the WebResourceFactory interface using the Requests and urllib libraries −

import urllib.request
import requests

class RequestsWebResourceFactory(WebResourceFactory):

   def get(self, url):
      return requests.get(url)

   def post(self, url, data):
      return requests.post(url, data)

class UrllibWebResourceFactory(WebResourceFactory):

   def get(self, url):
      response = urllib.request.urlopen(url)
      return response.read()

   def post(self, url, data):
      data = data.encode('utf-8')
      req = urllib.request.Request(url, data=data)
      response = urllib.request.urlopen(req)
      return response.read()

The RequestsWebResourceFactory and UrllibWebResourceFactory classes are concrete subclasses that implement the WebResourceFactory interface using the Requests and urllib libraries respectively.

Now we can use the WebResourceFactory interface to access web resources uniformly, without worrying about the underlying library. In the below Code, we are accessing the Google Homepage web resources.

The access_web_resource() function takes a factory object and a URL and uses the factory object to access the web resource at the specified URL. We can create a factory object using either the RequestsWebResourceFactory or UrllibWebResourceFactory class, depending on the library we want to use.

def access_web_resource(factory, url):
    web_resource = factory.get(url)
    print(web_resource)

factory = RequestsWebResourceFactory()
access_web_resource(factory, 'https://www.google.com')

factory = UrllibWebResourceFactory()
access_web_resource(factory, 'https://www.google.com')

The complete code and the output for accessing the Google Homepage is given below −

import urllib.request
import requests
from abc import ABC, abstractmethod

class WebResourceFactory(ABC):

   @abstractmethod
   def get(self, url):
        pass

   @abstractmethod
   def post(self, url, data):
      pass

class RequestsWebResourceFactory(WebResourceFactory):

   def get(self, url):
      return requests.get(url)

   def post(self, url, data):
      return requests.post(url, data)

class UrllibWebResourceFactory(WebResourceFactory):

   def get(self, url):
      response = urllib.request.urlopen(url)
      return response.read()

   def post(self, url, data):
      data = data.encode('utf-8')
      req = urllib.request.Request(url, data=data)
      response = urllib.request.urlopen(req)
      return response.read()

def access_web_resource(factory, url):
   web_resource = factory.get(url)
   print(web_resource)

factory = RequestsWebResourceFactory()
access_web_resource(factory, 'https://www.google.com')

factory = UrllibWebResourceFactory()
access_web_resource(factory, 'https://www.google.com')

Output

<Response [200]>
b'<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" 
lang="en-IN"><head><meta content="text/html; charset=UTF-8" http-
equiv="Content-Type"><meta 
content="/images/branding/googleg/1x/googleg_standard_color_128dp.png" 
itemprop="image"><title>Google</title><script …

Conclusion

In this article, we explored how to use the Factory Method Design Pattern to create a common interface for accessing web resources using the Requests and urllib libraries in Python. However, this pattern can be applied to any scenario where we need to create objects of different types based on a common interface.

Updated on: 06-Jul-2023

80 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements