API with requests session

This commit is contained in:
localhost_frssoft 2022-11-16 04:42:54 +03:00
parent d4da5358af
commit 47f20fe4e8

View file

@ -9,9 +9,13 @@ instance_point = f"https://{instance}/api/v1"
with open(".auth", mode='rt') as auth: with open(".auth", mode='rt') as auth:
tkn = auth.read().replace('\n', '') tkn = auth.read().replace('\n', '')
headers= {
"Authorization": "Bearer " + tkn s = requests.Session()
} s.headers.update({
"Authorization": "Bearer " + tkn,
"Accept-encoding": 'gzip'
})
def get_notifications(): def get_notifications():
params = { params = {
@ -21,7 +25,7 @@ def get_notifications():
success = 0 success = 0
while success == 0: while success == 0:
try: try:
r = requests.get(instance_point + "/notifications", json=params, headers=headers) r = s.get(instance_point + "/notifications", json=params)
r.raise_for_status() r.raise_for_status()
success = 1 success = 1
return r.json() return r.json()
@ -36,7 +40,7 @@ def mark_as_read_notification(id_notification):
success = 0 success = 0
while success == 0: while success == 0:
try: try:
r = requests.post(instance_point + f"/notifications/{id_notification}/dismiss", headers=headers) r = s.post(instance_point + f"/notifications/{id_notification}/dismiss")
r.raise_for_status() r.raise_for_status()
success = 1 success = 1
return r.json() return r.json()
@ -50,7 +54,7 @@ def get_status_context(status_id):
success = 0 success = 0
while success == 0: while success == 0:
try: try:
r = requests.get(instance_point + f"/statuses/{status_id}/context", headers=headers) r = s.get(instance_point + f"/statuses/{status_id}/context")
r.raise_for_status() r.raise_for_status()
success = 1 success = 1
return r.json() return r.json()
@ -64,7 +68,7 @@ def get_status(status_id):
success = 0 success = 0
while success == 0: while success == 0:
try: try:
r = requests.get(instance_point + f"/statuses/{status_id}", headers=headers) r = s.get(instance_point + f"/statuses/{status_id}")
r.raise_for_status() r.raise_for_status()
success = 1 success = 1
return r.json() return r.json()
@ -96,7 +100,7 @@ def post_status(text, reply_to_status_id=None, poll_options=None, poll_expires=3
success = 0 success = 0
while success == 0: while success == 0:
try: try:
r = requests.post(instance_point + "/statuses", json=params, headers=headers) r = s.post(instance_point + "/statuses", json=params)
r.raise_for_status() r.raise_for_status()
success = 1 success = 1
return r.json() return r.json()
@ -115,7 +119,7 @@ def upload_attachment(file_path):
success = 0 success = 0
while success == 0: while success == 0:
try: try:
r = requests.post(instance_point + "/media", params, files=file, headers=headers) r = s.post(instance_point + "/media", params, files=file)
r.raise_for_status() r.raise_for_status()
success = 1 success = 1
return r.json()['id'] return r.json()['id']
@ -134,7 +138,7 @@ def mute_user(acct_id=str, acct=str, duration=None):
success = 0 success = 0
while success == 0: while success == 0:
try: try:
r = requests.post(instance_point + '/accounts' + f"/{acct_id}/mute", params, headers=headers) r = s.post(instance_point + '/accounts' + f"/{acct_id}/mute", params)
r.raise_for_status() r.raise_for_status()
logger.info(f'Пользователь {acct} был заглушен на {duration} secs') logger.info(f'Пользователь {acct} был заглушен на {duration} secs')
success = 1 success = 1