Created
June 25, 2019 21:17
-
-
Save evanostroski/d0b07b6af32c8d0a812d5216d666937f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import date, datetime | |
import json | |
from requests import sessions | |
_session = sessions.Session() | |
class Serializer(json.JSONEncoder): | |
def default(self, obj): | |
if isinstance(obj, (date, datetime)): | |
return obj.isoformat() | |
return json.JSONEncoder.default(self, obj) | |
class APIError(Exception): | |
def __init__(self, status, code, message): | |
self.message = message | |
self.status = status | |
self.code = code | |
def __str__(self): | |
msg = "[Error] {0}: {1} ({2})" | |
return msg.format(self.code, self.message, self.status) | |
def patch(convo_id, tags, write_key): | |
"""Post the kwargs to the API""" | |
data = { 'tags': tags } | |
url = 'https://api2.frontapp.com/conversations/' + convo_id | |
data = json.dumps(data, cls=Serializer) | |
headers = { | |
'Content-type': 'application/json', | |
"Accept": "application/json", | |
"Authorization": 'Bearer ' + write_key } | |
res = _session.patch(url, data=data, headers=headers, timeout=15) | |
if res.status_code == 200 or res.status_code == 204: return "[Response]: OK" | |
try: | |
payload = res.json() | |
raise APIError(res.status_code, payload['code'], payload['message']) | |
except: | |
raise APIError(res.status_code, 'unknown', res.text) | |
res = patch(input_data['convo_id'], input_data['tags'].split(","), input_data['write_key']) | |
output = [{'res': res}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment