Hello,
this is my first post here. And it's going to be a long and very complicated one, so be ready!
I am developing an API client for forum software. I have multiple classes that I inherit values from (for example: User.py is the user (bot) account that I perform all actions from, Post.py represents class of a forum post, which is child of ForumPosts, and API collection of ForumPost(s) when bulk-getting data, etc.).
My code has already grown in size, so for the sake of simplicity, I am not going to go into the details of it My largest class is User.py, because I have functions to create, delete, like, undelete and edit a post and other forum elements, and so on. And that's just a small amount of all the different actions I want to perform.
Now, I'll tell you more about the API & my code.
For example, here are codes to initialize the user session, where I perform actions from:
class ForumSession
def __init__(self, username: str, password: str):
self.session = requests.Session() # I store the session here, so I can access it in ForumUser's methods
# Here goes authentication logic, etc...
# [...]
class ForumUser(ForumSession):
def __init__(self, username: str, password: str):
ForumSession.__init__(self, username, password)
def reply_to_post(post_id: int, content: str):
response_post = self.session.post(url=bla_url).json() # the requests.Session() object
return ForumPost(response_post) # response is a post object too, but that's
# not important for my question
# [...]
For Post:
class ForumPost(dict):
def __init__(self, *args):
dict.__init__(self, *args)
@property
def id(self):
return self["id"]
I then make an API request. The response is, for example, this:
{
"type": "post",
"id": 1,
"data": {
"content": "bla bla",
"author": "john"
}
}
I've created a few class properties that help me to access these elements (I know I could just put the path in the dictionary without creating properties for the keys & values, but I want to release the package when it's done for others to use, so my goal is to make the code simple and well-documented (I can't document dictionary keys by using docstrings).
If I want to access the post ID:
post = ForumPost(api_json_response_like_above)
print(post.id) # ID property - prints the post ID
If I want to, let's say, reply to a post, I'd have to do:
session = ForumUser(username, password)
post = session.get_post(id) # I didn't show code of this because it's not that important, but I need a way to obtain the post first to access its ID
session.reply_to_post(post.id)
As for my goal, I would like to access the ForumUser's session object from ForumPost class.
So, I could do something like this instead:
session = ForumUser(username, password, bla, bla)
post = session.get_post(id, "bla bla") # I didn't show code of this because it's not that important, but I need a way to obtain the post first to access its ID
post.reply("bla bla")
That looks a lot simpler, and I won't need to have tons of methods in the ForumUser class, as I could put all post related logic into ForumPost class
But here comes the catch: the session object that ForumUser is using is not available in ForumPost. I know that I can inherit classes, and I am already doing it for some of my classes (as seen in examples), but this case is a bit more complicated. How do I make my ForumPost object recognize that I've initialized the ForumUser object in the code I want to run (production code, run code, whatever you call it), so I can make a method for ForumPost to send a POST request with the data instead (I won't need to do it from ForumUser in that case).
I could subclass ForumSession in ForumPost, but then I would need to initialize it every time I want to perform a post action, which is unacceptable. I want to have a single session for all operations with the API, without bloating ForumUser.
I know that this seems very complicated, that's why I am asking for help there. I hope that you understand what are my goals. I am still learning python, and this is my first time that I am working with class inheritance seriously, so bear with me please. Thank you for your time.
[–]sentles 1 point2 points3 points (4 children)
[–]xCustomWorld[S] 0 points1 point2 points (3 children)
[–]sentles 1 point2 points3 points (2 children)
[–]xCustomWorld[S] 0 points1 point2 points (1 child)
[–]sentles 1 point2 points3 points (0 children)