diff --git a/memos/__init__.py b/memos/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/memos/memos.py b/memos/memos.py new file mode 100644 index 0000000..15d154a --- /dev/null +++ b/memos/memos.py @@ -0,0 +1,26 @@ +import requests +import logging +import json + +MEMOS_VISIBILITY_UNSPECIFIED = 'VISIBILITY_UNSPECIFIED' +MEMOS_VISIBILITY_PRIVATE = 'PRIVATE' +MEMOS_VISIBILITY_PROTECTED = 'PROTECTED' +MEMOS_VISIBILITY_PUBLIC = 'PUBLIC' + + +class Memos: + def __init__(self, token: str, base_url: str) -> None: + self.token = token + self.base_url = base_url + self.custom_header = dict(Authorization=f'Bearer {token}') + self.create_memo_url = base_url + '/api/v1/memos' + self.logger = logging.getLogger('memos') + + def create_memo(self, content:str, visibility: str, **kwargs) -> None: + body = json.dumps(dict(content=content, visibility=visibility) | kwargs) + r = requests.post(self.create_memo_url, data=body, headers=self.custom_header) + if r.status_code == 200: + result = r.json() + self.logger.info("Memo created: " + result['name']) + else: + self.logger.error("Failed to create memo: " + r.text) diff --git a/test_memo.py b/test_memo.py new file mode 100644 index 0000000..92309d6 --- /dev/null +++ b/test_memo.py @@ -0,0 +1,12 @@ +from memos import memos +import logging +import time + +logging.basicConfig(level=logging.INFO) + +m = memos.Memos( + token='eyJhbGciOiJIUzI1NiIsImtpZCI6InYxIiwidHlwIjoiSldUIn0.eyJuYW1lIjoidHJhZGVCb3QiLCJpc3MiOiJtZW1vcyIsInN1YiI6IjMiLCJhdWQiOlsidXNlci5hY2Nlc3MtdG9rZW4iXSwiaWF0IjoxNzMxNDIzNjU4fQ.cWQ4Z6WqTjlXPJxIfG7kjZhOO1GTTKiu4FuUIyfE0Ac', + base_url='https://memos.gao7.cc') + +m.create_memo(f'**Test**\n*{time.time()}*\n#api-test', memos.MEMOS_VISIBILITY_PROTECTED) +