gpt4free icon indicating copy to clipboard operation
gpt4free copied to clipboard

refractoring suggestion

Open keenborder786 opened this issue 1 year ago • 4 comments

I love this project and therefore would like to contribute. I noticed that Bing is missing a lot of things:

  • No support for chrome
  • HardCode Edge Support
  • HardCode Prompt

Therefore, I would be working to refactor the Bing package and make it more optimized.

keenborder786 avatar Apr 25 '23 00:04 keenborder786

PR full send

repollo avatar Apr 25 '23 07:04 repollo

you guys are free to improve whatever u want, go ahead

xtekky avatar Apr 25 '23 09:04 xtekky

import requests

class API:
    def __init__(self, api_key):
        self.api_key = api_key
    
    def get_data(self, url):
        headers = {'Authorization': f'Bearer {self.api_key}'}
        response = requests.get(url, headers=headers)
        return response.json()

class BingAPI(API):
    def __init__(self, api_key):
        super().__init__(api_key)
    
    def search(self, query):
        url = f'https://api.bing.com/v7.0/search?q={query}'
        data = self.get_data(url)
        return data['webPages']['value']

class GoogleAPI(API):
    def __init__(self, api_key):
        super().__init__(api_key)
    
    def search(self, query):
        url = f'https://www.googleapis.com/customsearch/v1?q={query}&key={self.api_key}'
        data = self.get_data(url)
        return data['items']

we have a parent class called API that handles the basic functionality of making HTTP requests to an API using an API key. We then have two child classes called BingAPI and GoogleAPI that inherit from the API class and implement specific functionality for the Bing and Google APIs.

To refactor this code, we can follow the steps I outlined in my previous answer. Here's an example of how we can refactor this code:

import requests

class API:
    def __init__(self, api_key):
        self.api_key = api_key
    
    def get_data(self, url):
        headers = {'Authorization': f'Bearer {self.api_key}'}
        response = requests.get(url, headers=headers)
        return response.json()

class SearchAPI(API):
    def __init__(self, api_key, url_format):
        super().__init__(api_key)
        self.url_format = url_format
    
    def search(self, query):
        url = self.url_format.format(query=query, api_key=self.api_key)
        data = self.get_data(url)
        return self.extract_results(data)

    def extract_results(self, data):
        raise NotImplementedError()

class BingAPI(SearchAPI):
    def __init__(self, api_key):
        url_format = 'https://api.bing.com/v7.0/search?q={query}'
        super().__init__(api_key, url_format)
    
    def extract_results(self, data):
        return data['webPages']['value']

class GoogleAPI(SearchAPI):
    def __init__(self, api_key):
        url_format = 'https://www.googleapis.com/customsearch/v1?q={query}&key={api_key}'
        super().__init__(api_key, url_format)
    
    def extract_results(self, data):
        return data['items']

n this refactored code, we have created a new class called SearchAPI that replaces the BingAPI and GoogleAPI classes. The SearchAPI class takes in an api_key and a url_format as arguments. The api_key argument is passed to the API parent class, and the url_format argument is used to construct the URL for the search API.

We have also implemented a new method called extract_results() that extracts the relevant search results from the API response. This method is implemented differently in the BingAPI and GoogleAPI classes, depending on the structure of the API response.

Ranu2001 avatar Apr 26 '23 19:04 Ranu2001

this looks interesting, will be working on using this to dev the main class ; )

xtekky avatar Apr 26 '23 21:04 xtekky