├── .gitignore ├── .travis.yml ├── LICENSE.TXT ├── MANIFEST ├── README.md ├── medium └── __init__.py ├── requirements.txt ├── setup.cfg ├── setup.py └── tests ├── requirements.txt ├── test.png └── test.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | 5 | # C extensions 6 | *.so 7 | 8 | # Distribution / packaging 9 | .Python 10 | env/ 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | *.egg-info/ 23 | .installed.cfg 24 | *.egg 25 | 26 | # PyInstaller 27 | # Usually these files are written by a python script from a template 28 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 29 | *.manifest 30 | *.spec 31 | 32 | # Installer logs 33 | pip-log.txt 34 | pip-delete-this-directory.txt 35 | 36 | # Unit test / coverage reports 37 | htmlcov/ 38 | .tox/ 39 | .coverage 40 | .coverage.* 41 | .cache 42 | nosetests.xml 43 | coverage.xml 44 | *,cover 45 | 46 | # Translations 47 | *.mo 48 | *.pot 49 | 50 | # Django stuff: 51 | *.log 52 | 53 | # Sphinx documentation 54 | docs/_build/ 55 | 56 | # PyBuilder 57 | target/ 58 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | python: 3 | - "2.7" 4 | - "3.5" 5 | install: 6 | - pip install -r tests/requirements.txt 7 | - pip install -e . 8 | script: python tests/test.py 9 | -------------------------------------------------------------------------------- /LICENSE.TXT: -------------------------------------------------------------------------------- 1 | Copyright 2015 A Medium Corporation. 2 | http://medium.com/ 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | 16 | 17 | ------------------------------------------------------------------------- 18 | Apache License 19 | Version 2.0, January 2004 20 | http://www.apache.org/licenses/ 21 | 22 | 23 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 24 | 25 | 1. Definitions. 26 | 27 | "License" shall mean the terms and conditions for use, reproduction, 28 | and distribution as defined by Sections 1 through 9 of this document. 29 | 30 | "Licensor" shall mean the copyright owner or entity authorized by 31 | the copyright owner that is granting the License. 32 | 33 | "Legal Entity" shall mean the union of the acting entity and all 34 | other entities that control, are controlled by, or are under common 35 | control with that entity. For the purposes of this definition, 36 | "control" means (i) the power, direct or indirect, to cause the 37 | direction or management of such entity, whether by contract or 38 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 39 | outstanding shares, or (iii) beneficial ownership of such entity. 40 | 41 | "You" (or "Your") shall mean an individual or Legal Entity 42 | exercising permissions granted by this License. 43 | 44 | "Source" form shall mean the preferred form for making modifications, 45 | including but not limited to software source code, documentation 46 | source, and configuration files. 47 | 48 | "Object" form shall mean any form resulting from mechanical 49 | transformation or translation of a Source form, including but 50 | not limited to compiled object code, generated documentation, 51 | and conversions to other media types. 52 | 53 | "Work" shall mean the work of authorship, whether in Source or 54 | Object form, made available under the License, as indicated by a 55 | copyright notice that is included in or attached to the work 56 | (an example is provided in the Appendix below). 57 | 58 | "Derivative Works" shall mean any work, whether in Source or Object 59 | form, that is based on (or derived from) the Work and for which the 60 | editorial revisions, annotations, elaborations, or other modifications 61 | represent, as a whole, an original work of authorship. For the purposes 62 | of this License, Derivative Works shall not include works that remain 63 | separable from, or merely link (or bind by name) to the interfaces of, 64 | the Work and Derivative Works thereof. 65 | 66 | "Contribution" shall mean any work of authorship, including 67 | the original version of the Work and any modifications or additions 68 | to that Work or Derivative Works thereof, that is intentionally 69 | submitted to Licensor for inclusion in the Work by the copyright owner 70 | or by an individual or Legal Entity authorized to submit on behalf of 71 | the copyright owner. For the purposes of this definition, "submitted" 72 | means any form of electronic, verbal, or written communication sent 73 | to the Licensor or its representatives, including but not limited to 74 | communication on electronic mailing lists, source code control systems, 75 | and issue tracking systems that are managed by, or on behalf of, the 76 | Licensor for the purpose of discussing and improving the Work, but 77 | excluding communication that is conspicuously marked or otherwise 78 | designated in writing by the copyright owner as "Not a Contribution." 79 | 80 | "Contributor" shall mean Licensor and any individual or Legal Entity 81 | on behalf of whom a Contribution has been received by Licensor and 82 | subsequently incorporated within the Work. 83 | 84 | 2. Grant of Copyright License. Subject to the terms and conditions of 85 | this License, each Contributor hereby grants to You a perpetual, 86 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 87 | copyright license to reproduce, prepare Derivative Works of, 88 | publicly display, publicly perform, sublicense, and distribute the 89 | Work and such Derivative Works in Source or Object form. 90 | 91 | 3. Grant of Patent License. Subject to the terms and conditions of 92 | this License, each Contributor hereby grants to You a perpetual, 93 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 94 | (except as stated in this section) patent license to make, have made, 95 | use, offer to sell, sell, import, and otherwise transfer the Work, 96 | where such license applies only to those patent claims licensable 97 | by such Contributor that are necessarily infringed by their 98 | Contribution(s) alone or by combination of their Contribution(s) 99 | with the Work to which such Contribution(s) was submitted. If You 100 | institute patent litigation against any entity (including a 101 | cross-claim or counterclaim in a lawsuit) alleging that the Work 102 | or a Contribution incorporated within the Work constitutes direct 103 | or contributory patent infringement, then any patent licenses 104 | granted to You under this License for that Work shall terminate 105 | as of the date such litigation is filed. 106 | 107 | 4. Redistribution. You may reproduce and distribute copies of the 108 | Work or Derivative Works thereof in any medium, with or without 109 | modifications, and in Source or Object form, provided that You 110 | meet the following conditions: 111 | 112 | (a) You must give any other recipients of the Work or 113 | Derivative Works a copy of this License; and 114 | 115 | (b) You must cause any modified files to carry prominent notices 116 | stating that You changed the files; and 117 | 118 | (c) You must retain, in the Source form of any Derivative Works 119 | that You distribute, all copyright, patent, trademark, and 120 | attribution notices from the Source form of the Work, 121 | excluding those notices that do not pertain to any part of 122 | the Derivative Works; and 123 | 124 | (d) If the Work includes a "NOTICE" text file as part of its 125 | distribution, then any Derivative Works that You distribute must 126 | include a readable copy of the attribution notices contained 127 | within such NOTICE file, excluding those notices that do not 128 | pertain to any part of the Derivative Works, in at least one 129 | of the following places: within a NOTICE text file distributed 130 | as part of the Derivative Works; within the Source form or 131 | documentation, if provided along with the Derivative Works; or, 132 | within a display generated by the Derivative Works, if and 133 | wherever such third-party notices normally appear. The contents 134 | of the NOTICE file are for informational purposes only and 135 | do not modify the License. You may add Your own attribution 136 | notices within Derivative Works that You distribute, alongside 137 | or as an addendum to the NOTICE text from the Work, provided 138 | that such additional attribution notices cannot be construed 139 | as modifying the License. 140 | 141 | You may add Your own copyright statement to Your modifications and 142 | may provide additional or different license terms and conditions 143 | for use, reproduction, or distribution of Your modifications, or 144 | for any such Derivative Works as a whole, provided Your use, 145 | reproduction, and distribution of the Work otherwise complies with 146 | the conditions stated in this License. 147 | 148 | 5. Submission of Contributions. Unless You explicitly state otherwise, 149 | any Contribution intentionally submitted for inclusion in the Work 150 | by You to the Licensor shall be under the terms and conditions of 151 | this License, without any additional terms or conditions. 152 | Notwithstanding the above, nothing herein shall supersede or modify 153 | the terms of any separate license agreement you may have executed 154 | with Licensor regarding such Contributions. 155 | 156 | 6. Trademarks. This License does not grant permission to use the trade 157 | names, trademarks, service marks, or product names of the Licensor, 158 | except as required for reasonable and customary use in describing the 159 | origin of the Work and reproducing the content of the NOTICE file. 160 | 161 | 7. Disclaimer of Warranty. Unless required by applicable law or 162 | agreed to in writing, Licensor provides the Work (and each 163 | Contributor provides its Contributions) on an "AS IS" BASIS, 164 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 165 | implied, including, without limitation, any warranties or conditions 166 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 167 | PARTICULAR PURPOSE. You are solely responsible for determining the 168 | appropriateness of using or redistributing the Work and assume any 169 | risks associated with Your exercise of permissions under this License. 170 | 171 | 8. Limitation of Liability. In no event and under no legal theory, 172 | whether in tort (including negligence), contract, or otherwise, 173 | unless required by applicable law (such as deliberate and grossly 174 | negligent acts) or agreed to in writing, shall any Contributor be 175 | liable to You for damages, including any direct, indirect, special, 176 | incidental, or consequential damages of any character arising as a 177 | result of this License or out of the use or inability to use the 178 | Work (including but not limited to damages for loss of goodwill, 179 | work stoppage, computer failure or malfunction, or any and all 180 | other commercial damages or losses), even if such Contributor 181 | has been advised of the possibility of such damages. 182 | 183 | 9. Accepting Warranty or Additional Liability. While redistributing 184 | the Work or Derivative Works thereof, You may choose to offer, 185 | and charge a fee for, acceptance of support, warranty, indemnity, 186 | or other liability obligations and/or rights consistent with this 187 | License. However, in accepting such obligations, You may act only 188 | on Your own behalf and on Your sole responsibility, not on behalf 189 | of any other Contributor, and only if You agree to indemnify, 190 | defend, and hold each Contributor harmless for any liability 191 | incurred by, or claims asserted against, such Contributor by reason 192 | of your accepting any such warranty or additional liability. 193 | 194 | END OF TERMS AND CONDITIONS 195 | -------------------------------------------------------------------------------- /MANIFEST: -------------------------------------------------------------------------------- 1 | # file GENERATED by distutils, do NOT edit 2 | setup.cfg 3 | setup.py 4 | medium/__init__.py 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | **Warning:** This code is no longer supported or maintained by Medium. 2 | 3 | # Medium SDK for Python 4 | 5 | This repository contains the open source SDK for integrating 6 | [Medium](https://medium.com/)'s OAuth2 REST API with your Python app. 7 | 8 | For full API documentation, see our [developer docs](https://github.com/Medium/medium-api-docs). 9 | 10 | ## Installing dependencies 11 | 12 | To install dependencies using pip: 13 | 14 | ``` 15 | pip install -r requirements.txt 16 | ``` 17 | 18 | ## Usage 19 | 20 | ```python 21 | from medium import Client 22 | 23 | # Go to http://medium.com/me/applications to get your application_id and application_secret. 24 | client = Client(application_id="MY_APPLICATION_ID", application_secret="MY_APPLICATION_SECRET") 25 | 26 | # Build the URL where you can send the user to obtain an authorization code. 27 | auth_url = client.get_authorization_url("secretstate", "https://yoursite.com/callback/medium", 28 | ["basicProfile", "publishPost"]) 29 | 30 | # (Send the user to the authorization URL to obtain an authorization code.) 31 | 32 | # Exchange the authorization code for an access token. 33 | auth = client.exchange_authorization_code("YOUR_AUTHORIZATION_CODE", 34 | "https://yoursite.com/callback/medium") 35 | 36 | # The access token is automatically set on the client for you after 37 | # a successful exchange, but if you already have a token, you can set it 38 | # directly. 39 | client.access_token = auth["access_token"] 40 | 41 | # Get profile details of the user identified by the access token. 42 | user = client.get_current_user() 43 | 44 | # Create a draft post. 45 | post = client.create_post(user_id=user["id"], title="Title", content="
Content
", 46 | content_format="html", publish_status="draft") 47 | 48 | # When your access token expires, use the refresh token to get a new one. 49 | client.exchange_refresh_token(auth["refresh_token"]) 50 | 51 | # Confirm everything went ok. post["url"] has the location of the created post. 52 | print "My new post!", post["url"] 53 | ``` 54 | 55 | ## Running tests 56 | 57 | To run tests against this package, first install the test requirements and make 58 | sure that the `medium` package is exportable. (We recommend using virtualenv.) 59 | 60 | ```bash 61 | $ pip install -r tests/requirements.txt 62 | $ pip install -e . 63 | ``` 64 | 65 | Then run the primary test file: 66 | 67 | ```bash 68 | $ python tests/test.py 69 | ``` 70 | 71 | ## Contributing 72 | 73 | Questions, comments, bug reports, and pull requests are all welcomed. If you 74 | haven't contributed to a Medium project before please head over to the [Open 75 | Source Project](https://github.com/Medium/opensource#note-to-external-contributors) 76 | and fill out an OCLA (it should be pretty painless). 77 | 78 | ## Authors 79 | 80 | - [Kyle Hardgrave](https://github.com/kylehg) 81 | 82 | ## License 83 | 84 | Copyright 2015 [A Medium Corporation](https://medium.com/) 85 | 86 | Licensed under Apache License Version 2.0. Details in the attached LICENSE file. 87 | -------------------------------------------------------------------------------- /medium/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2015 A Medium Corporation 2 | from os.path import basename 3 | try: 4 | from urllib.parse import urlencode 5 | except ImportError: 6 | from urllib import urlencode 7 | 8 | import requests 9 | 10 | BASE_PATH = "https://api.medium.com" 11 | 12 | 13 | class Client(object): 14 | """A client for the Medium OAuth2 REST API.""" 15 | 16 | def __init__(self, application_id=None, application_secret=None, 17 | access_token=None): 18 | self.application_id = application_id 19 | self.application_secret = application_secret 20 | self.access_token = access_token 21 | 22 | def get_authorization_url(self, state, redirect_url, scopes): 23 | """Get a URL for users to authorize the application. 24 | 25 | :param str state: A string that will be passed back to the redirect_url 26 | :param str redirect_url: The URL to redirect after authorization 27 | :param list scopes: The scopes to grant the application 28 | :returns: str 29 | """ 30 | qs = { 31 | "client_id": self.application_id, 32 | "scope": ",".join(scopes), 33 | "state": state, 34 | "response_type": "code", 35 | "redirect_uri": redirect_url, 36 | } 37 | 38 | return "https://medium.com/m/oauth/authorize?" + urlencode(qs) 39 | 40 | def exchange_authorization_code(self, code, redirect_url): 41 | """Exchange the authorization code for a long-lived access token, and 42 | set the token on the current Client. 43 | 44 | :param str code: The code supplied to the redirect URL after a user 45 | authorizes the application 46 | :param str redirect_url: The same redirect URL used for authorizing 47 | the application 48 | :returns: A dictionary with the new authorizations :: 49 | { 50 | 'token_type': 'Bearer', 51 | 'access_token': '...', 52 | 'expires_at': 1449441560773, 53 | 'refresh_token': '...', 54 | 'scope': ['basicProfile', 'publishPost'] 55 | } 56 | """ 57 | data = { 58 | "code": code, 59 | "client_id": self.application_id, 60 | "client_secret": self.application_secret, 61 | "grant_type": "authorization_code", 62 | "redirect_uri": redirect_url, 63 | } 64 | return self._request_and_set_auth_code(data) 65 | 66 | def exchange_refresh_token(self, refresh_token): 67 | """Exchange the supplied refresh token for a new access token, and 68 | set the token on the current Client. 69 | 70 | :param str refresh_token: The refresh token, as provided by 71 | ``exchange_authorization_code()`` 72 | :returns: A dictionary with the new authorizations :: 73 | { 74 | 'token_type': 'Bearer', 75 | 'access_token': '...', 76 | 'expires_at': 1449441560773, 77 | 'refresh_token': '...', 78 | 'scope': ['basicProfile', 'publishPost'] 79 | } 80 | """ 81 | data = { 82 | "refresh_token": refresh_token, 83 | "client_id": self.application_id, 84 | "client_secret": self.application_secret, 85 | "grant_type": "refresh_token", 86 | } 87 | return self._request_and_set_auth_code(data) 88 | 89 | def get_current_user(self): 90 | """Fetch the data for the currently authenticated user. 91 | 92 | Requires the ``basicProfile`` scope. 93 | 94 | :returns: A dictionary with the users data :: 95 | 96 | { 97 | 'username': 'kylehg', 98 | 'url': 'https://medium.com/@kylehg', 99 | 'imageUrl': 'https://cdn-images-1.medium.com/...', 100 | 'id': '1f86...', 101 | 'name': 'Kyle Hardgrave' 102 | } 103 | """ 104 | return self._request("GET", "/v1/me") 105 | 106 | def create_post(self, user_id, title, content, content_format, tags=None, 107 | canonical_url=None, publish_status=None, license=None): 108 | """Create a post for the current user. 109 | 110 | Requires the ``publishPost`` scope. 111 | 112 | :param str user_id: The application-specific user ID as returned by 113 | ``get_current_user()`` 114 | :param str title: The title of the post 115 | :param str content: The content of the post, in HTML or Markdown 116 | :param str content_format: The format of the post content, either 117 | ``html`` or ``markdown`` 118 | :param list tags: (optional), List of tags for the post, max 3 119 | :param str canonical_url: (optional), A rel="canonical" link for 120 | the post 121 | :param str publish_status: (optional), What to publish the post as, 122 | either ``public``, ``unlisted``, or ``draft``. Defaults to 123 | ``public``. 124 | :param license: (optional), The license to publish the post under: 125 | - ``all-rights-reserved`` (default) 126 | - ``cc-40-by`` 127 | - ``cc-40-by-sa`` 128 | - ``cc-40-by-nd`` 129 | - ``cc-40-by-nc`` 130 | - ``cc-40-by-nc-nd`` 131 | - ``cc-40-by-nc-sa`` 132 | - ``cc-40-zero`` 133 | - ``public-domain`` 134 | :returns: A dictionary with the post data :: 135 | 136 | { 137 | 'canonicalUrl': '', 138 | 'license': 'all-rights-reserved', 139 | 'title': 'My Title', 140 | 'url': 'https://medium.com/@kylehg/55050649c95', 141 | 'tags': ['python', 'is', 'great'], 142 | 'authorId': '1f86...', 143 | 'publishStatus': 'draft', 144 | 'id': '55050649c95' 145 | } 146 | """ 147 | data = { 148 | "title": title, 149 | "content": content, 150 | "contentFormat": content_format, 151 | } 152 | if tags is not None: 153 | data["tags"] = tags 154 | if canonical_url is not None: 155 | data["canonicalUrl"] = canonical_url 156 | if publish_status is not None: 157 | data["publishStatus"] = publish_status 158 | if license is not None: 159 | data["license"] = license 160 | 161 | path = "/v1/users/%s/posts" % user_id 162 | return self._request("POST", path, json=data) 163 | 164 | def upload_image(self, file_path, content_type): 165 | """Upload a local image to Medium for use in a post. 166 | 167 | Requires the ``uploadImage`` scope. 168 | 169 | :param str file_path: The file path of the image 170 | :param str content_type: The type of the image. Valid values are 171 | ``image/jpeg``, ``image/png``, ``image/gif``, and ``image/tiff``. 172 | :returns: A dictionary with the image data :: 173 | 174 | { 175 | 'url': 'https://cdn-images-1.medium.com/0*dlkfjalksdjfl.jpg', 176 | 'md5': 'd87e1628ca597d386e8b3e25de3a18b8' 177 | } 178 | """ 179 | with open(file_path, "rb") as f: 180 | filename = basename(file_path) 181 | files = {"image": (filename, f, content_type)} 182 | return self._request("POST", "/v1/images", files=files) 183 | 184 | def _request_and_set_auth_code(self, data): 185 | """Request an access token and set it on the current client.""" 186 | result = self._request("POST", "/v1/tokens", form_data=data) 187 | self.access_token = result["access_token"] 188 | return result 189 | 190 | def _request(self, method, path, json=None, form_data=None, files=None): 191 | """Make a signed request to the given route.""" 192 | url = BASE_PATH + path 193 | headers = { 194 | "Accept": "application/json", 195 | "Accept-Charset": "utf-8", 196 | "Authorization": "Bearer %s" % self.access_token, 197 | } 198 | 199 | resp = requests.request(method, url, json=json, data=form_data, 200 | files=files, headers=headers) 201 | json = resp.json() 202 | if 200 <= resp.status_code < 300: 203 | try: 204 | return json["data"] 205 | except KeyError: 206 | return json 207 | 208 | raise MediumError("API request failed", json) 209 | 210 | 211 | class MediumError(Exception): 212 | """Wrapper for exceptions generated by the Medium API.""" 213 | 214 | def __init__(self, message, resp={}): 215 | self.resp = resp 216 | try: 217 | error = resp["errors"][0] 218 | except KeyError: 219 | error = {} 220 | self.code = error.get("code", -1) 221 | self.msg = error.get("message", message) 222 | super(MediumError, self).__init__(self.msg) 223 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | requests==2.20.0 2 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [metadata] 2 | description-file = README.md 3 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from distutils.core import setup 2 | setup( 3 | name='medium', 4 | packages=['medium'], 5 | install_requires=['requests'], 6 | version='0.3.0', 7 | description='SDK for working with the Medium API', 8 | author='Kyle Hardgrave', 9 | author_email='kyle@medium.com', 10 | url='https://github.com/Medium/medium-sdk-python', 11 | download_url='https://github.com/Medium/medium-sdk-python/tarball/v0.3.0', 12 | keywords=['medium', 'sdk', 'oauth', 'api'], 13 | classifiers=[], 14 | ) 15 | -------------------------------------------------------------------------------- /tests/requirements.txt: -------------------------------------------------------------------------------- 1 | responses==0.5.0 2 | -------------------------------------------------------------------------------- /tests/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Medium/medium-sdk-python/48a29d6a96258855ed89ceb7dce67f29219ac7e7/tests/test.png -------------------------------------------------------------------------------- /tests/test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2015 A Medium Corporation 2 | import json 3 | import unittest 4 | try: 5 | from urllib.parse import parse_qs 6 | except ImportError: 7 | from urlparse import parse_qs 8 | 9 | import requests 10 | import responses 11 | 12 | from medium import Client 13 | 14 | class TestClient(unittest.TestCase): 15 | 16 | def setUp(self): 17 | self.client = Client(access_token="myaccesstoken") 18 | 19 | @responses.activate 20 | def test_exchange_authorization_code(self): 21 | def response_callback(payload): 22 | self.assertEqual(payload["code"][0], "mycode") 23 | self.assertEqual(payload["client_id"][0], "myclientid") 24 | self.assertEqual(payload["client_secret"][0], "myclientsecret") 25 | self.assertEqual(payload["grant_type"][0], "authorization_code") 26 | self.assertEqual(payload["redirect_uri"][0], 27 | "http://example.com/cb") 28 | data = { 29 | "token_type": "Bearer", 30 | "access_token": "myaccesstoken", 31 | "expires_at": 4575744000000, 32 | "refresh_token": "myrefreshtoken", 33 | "scope": ["basicProfile"], 34 | } 35 | return (201, data) 36 | self._mock_endpoint("POST", "/v1/tokens", response_callback, 37 | is_json=False) 38 | 39 | client = Client(application_id="myclientid", 40 | application_secret="myclientsecret") 41 | 42 | # TODO(kyle) Remove after Ceasar's refactoring 43 | resp = client.exchange_authorization_code("mycode", 44 | "http://example.com/cb") 45 | self.assertEqual(resp["access_token"], "myaccesstoken") 46 | self.assertEqual(resp["refresh_token"], "myrefreshtoken") 47 | self.assertEqual(resp["scope"], ["basicProfile"]) 48 | 49 | # TODO(kyle) Uncomment after Ceasar's refactoring 50 | # client.exchange_authorization_code("mycode", "http://example.com/cb") 51 | # self.assertEqual(client.access_token, "myaccesstoken") 52 | # self.assertEqual(client.refresh_token, "myrefreshtoken") 53 | 54 | @responses.activate 55 | def test_exchange_refresh_token(self): 56 | def response_callback(payload): 57 | self.assertEqual(payload["refresh_token"][0], "myrefreshtoken") 58 | self.assertEqual(payload["client_id"][0], "myclientid") 59 | self.assertEqual(payload["client_secret"][0], "myclientsecret") 60 | self.assertEqual(payload["grant_type"][0], "refresh_token") 61 | data = { 62 | "token_type": "Bearer", 63 | "access_token": "myaccesstoken2", 64 | "expires_at": 4575744000000, 65 | "refresh_token": "myrefreshtoken2", 66 | "scope": ["basicProfile"], 67 | } 68 | return (201, data) 69 | self._mock_endpoint("POST", "/v1/tokens", response_callback, 70 | is_json=False) 71 | 72 | client = Client(application_id="myclientid", 73 | application_secret="myclientsecret") 74 | 75 | # TODO(kyle) Remove after Ceasar's refactoring 76 | resp = client.exchange_refresh_token("myrefreshtoken") 77 | self.assertEqual(resp["access_token"], "myaccesstoken2") 78 | self.assertEqual(resp["refresh_token"], "myrefreshtoken2") 79 | self.assertEqual(resp["scope"], ["basicProfile"]) 80 | 81 | # TODO(kyle) Uncomment after Ceasar's refactoring 82 | # client.exchange_refresh_token("myrefreshtoken") 83 | # self.assertEqual(client.access_token, "myaccesstoken2") 84 | # self.assertEqual(client.refresh_token, "myrefreshtoken2") 85 | 86 | @responses.activate 87 | def test_get_current_user(self): 88 | def response_callback(payload): 89 | data = { 90 | "username": "nicki", 91 | "url": "https://medium.com/@nicki", 92 | "imageUrl": "https://images.medium.com/0*fkfQiTzT7TlUGGyI.png", 93 | "id": "5303d74c64f66366f00cb9b2a94f3251bf5", 94 | "name": "Nicki Minaj", 95 | } 96 | return 200, data 97 | self._mock_endpoint("GET", "/v1/me", response_callback) 98 | 99 | resp = self.client.get_current_user() 100 | self.assertEqual(resp, { 101 | "username": "nicki", 102 | "url": "https://medium.com/@nicki", 103 | "imageUrl": "https://images.medium.com/0*fkfQiTzT7TlUGGyI.png", 104 | "id": "5303d74c64f66366f00cb9b2a94f3251bf5", 105 | "name": "Nicki Minaj", 106 | }) 107 | 108 | @responses.activate 109 | def test_create_post(self): 110 | def response_callback(payload): 111 | self.assertEqual(payload, { 112 | "title": "Starships", 113 | "content": "Are meant to flyyyy
", 114 | "contentFormat": "html", 115 | "tags": ["stars", "ships", "pop"], 116 | "publishStatus": "draft", 117 | }) 118 | 119 | data = { 120 | "license": "all-rights-reserved", 121 | "title": "Starships", 122 | "url": "https://medium.com/@nicki/55050649c95", 123 | "tags": ["stars", "ships", "pop"], 124 | "authorId": "5303d74c64f66366f00cb9b2a94f3251bf5", 125 | "publishStatus": "draft", 126 | "id": "55050649c95", 127 | } 128 | return 200, data 129 | self._mock_endpoint( 130 | "POST", 131 | "/v1/users/5303d74c64f66366f00cb9b2a94f3251bf5/posts", 132 | response_callback 133 | ) 134 | 135 | resp = self.client.create_post( 136 | "5303d74c64f66366f00cb9b2a94f3251bf5", 137 | "Starships", 138 | "Are meant to flyyyy
", 139 | "html", 140 | tags=["stars", "ships", "pop"], 141 | publish_status="draft" 142 | ) 143 | self.assertEqual(resp, { 144 | "license": "all-rights-reserved", 145 | "title": "Starships", 146 | "url": "https://medium.com/@nicki/55050649c95", 147 | "tags": ["stars", "ships", "pop"], 148 | "authorId": "5303d74c64f66366f00cb9b2a94f3251bf5", 149 | "publishStatus": "draft", 150 | "id": "55050649c95", 151 | }) 152 | 153 | @responses.activate 154 | def test_upload_image(self): 155 | def response_callback(req): 156 | self.assertEqual(req.headers["Authorization"], 157 | "Bearer myaccesstoken") 158 | self.assertIn(b"Content-Type: image/png", req.body) 159 | return 200, {}, json.dumps({ 160 | "url": "https://cdn-images-1.medium.com/0*dlkfjalksdjfl.jpg", 161 | "md5": "d87e1628ca597d386e8b3e25de3a18bc", 162 | }) 163 | responses.add_callback(responses.POST, "https://api.medium.com/v1/images", 164 | content_type="application/json", 165 | callback=response_callback) 166 | 167 | resp = self.client.upload_image("./tests/test.png", "image/png") 168 | self.assertEqual(resp, { 169 | "url": "https://cdn-images-1.medium.com/0*dlkfjalksdjfl.jpg", 170 | "md5": "d87e1628ca597d386e8b3e25de3a18bc", 171 | }) 172 | 173 | def _mock_endpoint(self, method, path, callback, is_json=True): 174 | def wrapped_callback(req): 175 | if is_json: 176 | self.assertEqual(req.headers["Authorization"], 177 | "Bearer myaccesstoken") 178 | if req.body is not None: 179 | body = json.loads(req.body) if is_json else parse_qs(req.body) 180 | else: 181 | body = None 182 | status, data = callback(body) 183 | return (status, {}, json.dumps(data)) 184 | response_method = responses.GET if method == "GET" else responses.POST 185 | url = "https://api.medium.com" + path 186 | content_type = ("application/json" if is_json else 187 | "application/x-www-form-urlencoded") 188 | responses.add_callback(response_method, url, content_type=content_type, 189 | callback=wrapped_callback) 190 | 191 | 192 | if __name__ == "__main__": 193 | unittest.main() 194 | --------------------------------------------------------------------------------