Skip to content

Commit

Permalink
Optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
LucienShui committed Aug 24, 2020
1 parent 30bfccc commit bec6f29
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
15 changes: 11 additions & 4 deletions pasteme/pasteme.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import absolute_import, print_function

import requests
from .util import urljoin


class PasteMe(object):
Expand All @@ -9,6 +10,12 @@ def __init__(self, api_url: str = 'http://api.pasteme.cn/', token: str = ''):
self.api_url = api_url
self.token = token

response: dict = requests.api.get(url=self.api_url, params={'method': 'beat'}).json()
expected_response = {'status': 200}

if response != expected_response:
raise AssertionError(f"{expected_response} is expected, got {response}")

def get(self, key: [int, str], password: [int, str] = None, json: bool = False) -> [dict, str]:
"""
根据 key 读取 Paste
Expand All @@ -22,8 +29,8 @@ def get(self, key: [int, str], password: [int, str] = None, json: bool = False)
}
"""

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)
url: str = urljoin(self.api_url, f"{key}" if password is None else f"{key},{password}")
response: requests.Response = requests.api.get(url=url, params={'json': str(json).lower()})

return response.json() if json else response.content.decode('utf-8')

Expand All @@ -50,10 +57,10 @@ def create(self, content: str, lang: str, key: str = None, password: str = None,
json_param['password'] = password

if read_once:
response: requests.Response = requests.api.post(url=f'{self.api_url}once', json=json_param)
response: requests.Response = requests.api.post(url=f'{urljoin(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)
response: requests.Response = requests.api.put(url=f'{urljoin(self.api_url, key)}', json=json_param)

return response.json()
15 changes: 15 additions & 0 deletions pasteme/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from __future__ import absolute_import, print_function

from urllib.parse import urljoin as base_urljoin


def urljoin(*args):
if len(args) < 2:
raise AssertionError(f'len(args) must greater or equal than 2')

base: str = args[0] if args[0][-1] == '/' else f'{args[0]}/'
if len(args) > 2:
result = urljoin(base_urljoin(base, str(args[1])), *args[2:])
else:
result = base_urljoin(base, args[1])
return result
17 changes: 16 additions & 1 deletion pasteme_sdk_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from __future__ import absolute_import, print_function

import unittest

from pasteme import PasteMe
from pasteme.util import urljoin


class MyTestCase(unittest.TestCase):
Expand All @@ -17,15 +19,28 @@ def test_get(self):
def test_create(self):
pasteme: PasteMe = PasteMe(api_url='http://api.pasteme.cn/')

response: dict = pasteme.create('Test', 'plain')
# response: dict = pasteme.create('Test', 'plain')
# self.assertEqual(201, response['status'])
#
# response: str = pasteme.get(key=response['key'], password='password')
# self.assertEqual('Test', response)

response: dict = pasteme.create('Test', 'plain', password='password')
self.assertEqual(201, response['status'])

response: str = pasteme.get(key=response['key'], password='password')
self.assertEqual('Test', response)

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)

def test_urljoin(self):
result: str = urljoin("https://api.pasteme.cn/", "101", "123456")
self.assertEqual("https://api.pasteme.cn/101/123456", result)


if __name__ == '__main__':
unittest.main()

0 comments on commit bec6f29

Please sign in to comment.