Skip to content

Commit

Permalink
Properly set the attributes of the Gist object;
Browse files Browse the repository at this point in the history
- Gist.files is now a list of File objects representing the files of that gist.
- Gist.created_at and Gist.updated_at now return datetime.datetime objects representing the dates of when the gist was created and when it was last updated, respectively.
  • Loading branch information
WitherredAway committed Apr 17, 2022
1 parent 2eacd35 commit 79c2398
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 6 deletions.
3 changes: 3 additions & 0 deletions gists/constants.py
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
API_URL = "https://api.github.com"


TIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ %z"
88 changes: 82 additions & 6 deletions gists/gist.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,98 @@
import typing
import datetime

from .file import File
from .constants import TIME_FORMAT


__all__ = ("Gist",)


class Gist:
"""The Gist object that represents a gist"""

__slots__ = (
"client",
"comments",
"comments_url",
"commits_url",
"_created_at",
"description",
"_files",
"forks",
"forks_url",
"git_pull_url",
"git_push_url",
"history",
"url",
"id",
"node_id",
"owner",
"public",
"truncated",
"_updated_at",
"api_url",
"user",
)

def __init__(self, data: typing.Dict, client: "Client"):
self.data = data
self.client = client
# Set the data dict's items as attributes

self._update_attrs(data)

def _update_attrs(self, data: typing.Dict):
"""Update the Gist object's attributes to the provided data"""
"""Update the Gist object's attributes with the provided data"""

self.comments: int = data.get("comments", None)
self.comments_url: str = data.get("comments_url", None)
self.commits_url: str = data.get("commits_url", None)
self._created_at: str = data.get("created_at", None)
self.description: str = data.get("description", None)
self._files: typing.Dict = data.get("files", None)
self.forks: typing.List = data.get("forks", None) # TODO Fork object
self.forks_url: str = data.get("forks_url", None)
self.git_pull_url: str = data.get("git_pull_url", None)
self.git_push_url: str = data.get("git_push_url", None)
self.history: typing.List = data.get("history", None) # TODO History object
self.url: str = data.get("html_url", None)
self.id: str = data.get("id", None)
self.node_id: str = data.get("node_id", None)
self.owner: typing.Dict = data.get("owner", None) # TODO User object
self.public: bool = data.get("public", None)
self.truncated: bool = data.get("truncated", None)
self._updated_at: str = data.get("updated_at", None)
self.api_url: str = data.get("url", None)
self.user: None = data.get("user", None)

def _get_dt_obj(self, time: str) -> datetime.datetime:
time = time + " +0000" # Tells datetime that the timezone is UTC
dt_obj: datetime.datetime = datetime.datetime.strptime(time, TIME_FORMAT)
return dt_obj

@property
def created_at(self) -> datetime.datetime:
return self._get_dt_obj(self._created_at)

@created_at.setter
def created_at(self, value: str):
self._created_at = value

@property
def files(self) -> typing.List[File]:
file_objs: typing.List[File] = File.from_dict(self._files)
return file_objs

@files.setter
def files(self, value: typing.Dict):
self._files = value

@property
def updated_at(self) -> datetime.datetime:
return self._get_dt_obj(self._updated_at)

# TODO use getter and setters to do this, the current way is not good practise
self.__dict__.update(data)
@updated_at.setter
def updated_at(self, value: str):
self._updated_at = value

async def update(self):
"""Fetch and update the Gist object with the gist"""
Expand All @@ -35,4 +112,3 @@ async def delete(self):
"""Delete the gist associated with the Gist object, then delete the Gist object itself"""

await self.client.delete_gist(self.id)
del self

0 comments on commit 79c2398

Please sign in to comment.