Skip to content

Commit 53cc2e1

Browse files
authored
Main API Development (#13)
* feat: config, jinja2 and requests, baseclass * chore: move class modules to classes subpackage * fix: move config.py to classes subpackage * fix(flake8): change max line length to 88 * fix: change _config to property * feat: add working user and post classes + api changes * chore: add newline at end of .flake8 file Authored: BD103 <dont@stalk.me>
1 parent 3d9fc37 commit 53cc2e1

File tree

11 files changed

+279
-24
lines changed

11 files changed

+279
-24
lines changed

.flake8

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
[flake8]
2-
indent-size = 2
2+
indent-size = 2
3+
max-line-length = 88

poetry.lock

Lines changed: 139 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ repository = "https://github.com/ReplAPI-it/Python-ReplAPI-It"
99

1010
[tool.poetry.dependencies]
1111
python = "^3.8"
12+
jinja2 = "^3.0.1"
13+
requests = "^2.25.1"
1214

1315
[tool.poetry.dev-dependencies]
1416
isort = "^5.8.0"

replapi_it/classes/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from .base import BaseClass # noqa: F401
2+
from .config import config # noqa: F401
3+
from .post import Post # noqa: F401
4+
from .user import User # noqa: F401

replapi_it/classes/base.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import json
2+
3+
import requests
4+
from jinja2 import Template
5+
6+
from .config import config
7+
8+
_query_template = """
9+
query Main({% for i in query_args %}{{ i }}: {{ query_args[i] }}, {% endfor %}) {
10+
{% for i in types %}
11+
{{ i }} {
12+
{% for field in types[i] %}
13+
{{ field }}
14+
{% endfor %}
15+
}
16+
{% endfor %}
17+
}
18+
""".strip()
19+
20+
_default_headers = {
21+
"X-Requested-With": "ReplAPI-It-Python",
22+
"referrer": config["graphql_url"],
23+
}
24+
25+
26+
class BaseClass(object):
27+
def __init__(self, vars: dict = {}, **kwargs):
28+
self.vars = {**vars, **kwargs}
29+
self._query_args = {"$username": "String!"}
30+
self._types = {"userByUsername(username: $username)": ["fullName", "karma"]}
31+
32+
@property
33+
def config(self) -> dict:
34+
return config
35+
36+
def query(self) -> str:
37+
tm = Template(_query_template)
38+
39+
return tm.render(query_args=self._query_args, types=self._types)
40+
41+
def collect_json(self):
42+
req = requests.post(
43+
self.config["graphql_url"],
44+
data={"query": self.query(), "variables": json.dumps(self.vars)},
45+
headers=_default_headers,
46+
)
47+
48+
return req.text
49+
50+
def collect(self):
51+
return json.loads(self.collect_json())

replapi_it/classes/config.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import json
2+
import os
3+
4+
default = {"graphql_url": "https://staging.replit.com/graphql"}
5+
6+
7+
def _config(filename: str = "replapi_it.json") -> dict:
8+
if os.path.isfile(filename):
9+
with open(filename, "rt") as fp:
10+
return json.load(fp)
11+
else:
12+
return {}
13+
14+
15+
config = {**default, **_config()}

replapi_it/classes/post.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from .base import BaseClass
2+
3+
4+
class Post(BaseClass):
5+
def __init__(self, id: int, **kwargs):
6+
self.vars = {**{"id": id}, **kwargs}
7+
self._query_args = {"$id": "Int!"}
8+
self._types = {
9+
"post(id: $id)": [
10+
"id",
11+
"title",
12+
"body",
13+
"showHosted",
14+
"voteCount",
15+
"commentCount",
16+
"isPinned",
17+
"isLocked",
18+
"timeCreated",
19+
"timeUpdated",
20+
"url",
21+
"isAnnouncement",
22+
"isAuthor",
23+
"canEdit",
24+
"canComment",
25+
"canVote",
26+
"canPin",
27+
"canSetType",
28+
"canChangeBoard",
29+
"canLock",
30+
"hasVoted",
31+
"canReport",
32+
"hasReported",
33+
"isAnswerable",
34+
"tutorialPages",
35+
]
36+
}

replapi_it/classes/user.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from .base import BaseClass
2+
3+
4+
class User(BaseClass):
5+
def __init__(self, username: str, **kwargs):
6+
self.vars = {**{"username": username}, **kwargs}
7+
self._query_args = {"$username": "String!"}
8+
self._types = {
9+
"userByUsername(username: $username)": [
10+
"id",
11+
"username",
12+
"firstName",
13+
"lastName",
14+
"bio",
15+
"isVerified",
16+
"displayName",
17+
"fullName",
18+
"url",
19+
"isLoggedIn",
20+
"isSubscribed",
21+
"timeCreated",
22+
"isBannedFromBoards",
23+
"karma",
24+
"isHacker",
25+
"image",
26+
]
27+
}

replapi_it/core.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
from .post import _Post
2-
from .user import _User
1+
from . import classes
32

43
defaultInitVars = {"username": None}
54

@@ -12,5 +11,5 @@ class ReplAPI(object):
1211
def __init__(self, initVars: dict = {}):
1312
self.vars = {**defaultInitVars, **initVars}
1413

15-
self.User = _User
16-
self.Post = _Post
14+
self.User = classes.User
15+
self.Post = classes.Post

replapi_it/post.py

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)