How to subclass requests in python through inheritance

2024/9/20 5:32:24

I would like to specialize / subclass the requests package to add some method with custom functionality.

I tried to do this:

# concrete_requests.py
import requestsclass concreteRequests(requests):def __init__(self):super(concreteRequests, self).__init__() self.session()def login(self):payload = {'user': 'foo', 'pass': 'bar'}self.get('loginUrl', headers=header, data=payload)# more login stuff...# my_class.py
class MyClass:def __init__():self.requests = concreteRequests()self.requests.login()

This way I could still benefit from self.requests members + my concrete implementation. So I could do: self.requests.get(...) or print(self.requests.post(...).status_code) and so on.

I guess that this line super(concreteRequests, self).__init__() can be stupidly useless since requests doesn't have any class declaration inside just imports...

So, the requests package can be subclassed/specialized through inheritance ?

Answer

requests is a python module not a class. You can only subclass classes.

So basically you should just leverage its methods/functions inside your own custom class.

import requestsclass MyRequests:def __init__(self, username, passwd):self.username = usernameself.passwd = passwddef get(self, *args, **kwargs):# do your thingresp = requests.get(...)# do more processing

What I wrote above is just an example to get you going.

https://en.xdnf.cn/q/72530.html

Related Q&A

ipython debugger: full traceback on interactive pdb?

I recently switched from ipython0.10 to ipython0.11. In ipython0.11, I only see a small snippet of the full traceback when the python debugger engages (i.e. using %pdb), whereas in ipython0.10 Id see …

How to get all users in a list Twitter API?

Is there a way to access all members in a list? Currently, I can only see the first 20 members? Specifically, Im using python and tweepy.

Python adding a blank/empty column. csv

Hello I have a database that i am trying to make a .csv file quickly from.my data looks like this.Song_Name,File_Name,Artist_Name,Artist_ID Song1,filename1,artistname,artist001 Song1,filename1,artistna…

Displaying Radio buttons horizontally in matplotlib

I am using the matplotlib.widgets to create radio buttons in my widgets, the buttons coming are stacked vertically, I would like them to be stacked horizontally.MVCE:import matplotlib.pyplot as plt fro…

Python MemoryError on large array

This is the python script that Im trying to run:n = 50000000000 ##50 billion b = [0]*n for x in range(0,n):b[x] = random.randint(1,899999)... But the output Im getting is:E:\python\> python sort.py…

How to generate a PDF with non-ascii characters using from_string from python-pdfkit

Im struggling to generate just a simple PDF with non-ascii characters using Python 3.5.2, python-pdfkit and wkhtmltox-0.12.2.This is the easiest example I could write:import pdfkit html_content = u<…

Minimum window of days required to travel all cities

This is an interesting question that I came across in a coding challenge:There are k cities and n days. A travel agent is going to show you city k on day n. Youre supposed to find the minimum number of…

Force Nosetests to Use Python 2.7 instead of 3.4

Ive been learning Python using version 3.4. I recently started learning Web.py so have been using Python 2.7 for that, since web.py not supported in Python 3.4. I have nose 1.3.4 module installed for …

Python regex not to match http://

I am facing a problem to match and replace certain words, not contained in http:// Present Regex: http://.*?\s+This matches the pattern http://www.egg1.com http://www.egg2.com I need a regex to matc…

how can I maintain sequence of my list using set?

In [1]: l1 = [a,2,3,0,9.0,0,2,6,b,a]In [2]: l2 = list(set(l1))In [3]: l2 Out[3]: [a, 0, 2, 3, 6, 9.0, b]Here you can see the the list l2 is falling with different sequence then the original l1, I need …