-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dcdba6b
commit 30bfccc
Showing
4 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -127,3 +127,9 @@ dmypy.json | |
|
||
# Pyre type checker | ||
.pyre/ | ||
|
||
# macOS | ||
.DS_Store | ||
|
||
# pycharm | ||
.idea |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from __future__ import absolute_import, print_function | ||
|
||
from .pasteme import PasteMe |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from __future__ import absolute_import, print_function | ||
|
||
import requests | ||
|
||
|
||
class PasteMe(object): | ||
|
||
def __init__(self, api_url: str = 'http://api.pasteme.cn/', token: str = ''): | ||
self.api_url = api_url | ||
self.token = token | ||
|
||
def get(self, key: [int, str], password: [int, str] = None, json: bool = False) -> [dict, str]: | ||
""" | ||
根据 key 读取 Paste | ||
:param key: Paste ID | ||
:param password: 密码,没有则留空 | ||
:param json: 是否请求 json response,如果为 false 则只返回 content 字段 | ||
:return: { | ||
"status": 200, | ||
"lang": "bash", | ||
"content": "echo Hello" | ||
} | ||
""" | ||
|
||
url: str = f'{self.api_url}{key}{"" if password is None else ",{}".format(password)}?json={str(json).lower()}' | ||
response: requests.Response = requests.api.get(url=url) | ||
|
||
return response.json() if json else response.content.decode('utf-8') | ||
|
||
def create(self, content: str, lang: str, key: str = None, password: str = None, read_once: bool = False) -> dict: | ||
""" | ||
创建一个 Paste | ||
:param content: Paste 的内容 | ||
:param lang: Paste 的高亮类型 | ||
:param key: Paste ID,如果留空则自动生成,非空的时候会自动变为阅后即焚的 Paste | ||
:param password: 密码,留空则不设置 | ||
:param read_once: 阅后即焚 | ||
:return: { | ||
"status": 201, | ||
"key": <paste id> | ||
} | ||
""" | ||
|
||
json_param = { | ||
'content': content, | ||
'lang': lang | ||
} | ||
|
||
if password is not None and len(password) != 0: | ||
json_param['password'] = password | ||
|
||
if read_once: | ||
response: requests.Response = requests.api.post(url=f'{self.api_url}once', json=json_param) | ||
elif key is None or len(key) == 0: | ||
response: requests.Response = requests.api.post(url=self.api_url, json=json_param) | ||
else: | ||
response: requests.Response = requests.api.put(url=f'{self.api_url}{key}', json=json_param) | ||
|
||
return response.json() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from __future__ import absolute_import, print_function | ||
|
||
import unittest | ||
from pasteme import PasteMe | ||
|
||
|
||
class MyTestCase(unittest.TestCase): | ||
|
||
def test_get(self): | ||
pasteme: PasteMe = PasteMe(api_url='http://api.pasteme.cn/') | ||
response: str = pasteme.get('101', '123456') | ||
self.assertEqual('加密测试', response) | ||
|
||
response: dict = pasteme.get(101, 123456, json=True) | ||
self.assertEqual('加密测试', response['content']) | ||
|
||
def test_create(self): | ||
pasteme: PasteMe = PasteMe(api_url='http://api.pasteme.cn/') | ||
|
||
response: dict = pasteme.create('Test', 'plain') | ||
self.assertEqual(201, response['status']) | ||
|
||
response: dict = pasteme.create('Test', 'plain', 'create', 'password') | ||
self.assertEqual(201, response['status']) | ||
|
||
response: str = pasteme.get(key='create', password='password') | ||
self.assertEqual('Test', response) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |