|
4 | 4 | from textwrap import dedent |
5 | 5 |
|
6 | 6 | from dateutil.parser import parse |
7 | | -import requests |
8 | 7 |
|
| 8 | +from pythonanywhere.api.base import PYTHON_VERSIONS, call_api, get_api_endpoint |
9 | 9 | from pythonanywhere.exceptions import SanityException |
10 | 10 | from pythonanywhere.snakesay import snakesay |
11 | 11 |
|
12 | | -PYTHON_VERSIONS = { |
13 | | - "2.7": "python27", "3.4": "python34", "3.5": "python35", "3.6": "python36", "3.7": "python37", "3.8": "python38", |
14 | | -} |
15 | | - |
16 | | - |
17 | | -class AuthenticationError(Exception): |
18 | | - pass |
19 | | - |
20 | | - |
21 | | -class NoTokenError(Exception): |
22 | | - pass |
23 | | - |
24 | | - |
25 | | -def get_api_endpoint(): |
26 | | - hostname = os.environ.get( |
27 | | - "PYTHONANYWHERE_SITE", |
28 | | - "www." + os.environ.get( |
29 | | - "PYTHONANYWHERE_DOMAIN", |
30 | | - "pythonanywhere.com" |
31 | | - ) |
32 | | - ) |
33 | | - return "https://{hostname}/api/v0/user/{{username}}/{{flavor}}/".format(hostname=hostname) |
34 | | - |
35 | | - |
36 | | -def call_api(url, method, **kwargs): |
37 | | - token = os.environ.get("API_TOKEN") |
38 | | - if token is None: |
39 | | - raise NoTokenError( |
40 | | - "Oops, you don't seem to have an API token. " |
41 | | - "Please go to the 'Account' page on PythonAnywhere, then to the 'API Token' " |
42 | | - "tab. Click the 'Create a new API token' button to create the token, then " |
43 | | - "start a new console and try running this script again." |
44 | | - ) |
45 | | - insecure = os.environ.get("PYTHONANYWHERE_INSECURE_API") == "true" |
46 | | - response = requests.request( |
47 | | - method=method, |
48 | | - url=url, |
49 | | - headers={"Authorization": "Token {token}".format(token=token)}, |
50 | | - verify=not insecure, |
51 | | - **kwargs |
52 | | - ) |
53 | | - if response.status_code == 401: |
54 | | - print(response, response.text) |
55 | | - raise AuthenticationError( |
56 | | - "Authentication error {status_code} calling API: {response_text}".format( |
57 | | - status_code=response.status_code, response_text=response.text |
58 | | - ) |
59 | | - ) |
60 | | - return response |
61 | | - |
62 | 12 |
|
63 | 13 | class Webapp: |
64 | 14 | def __init__(self, domain): |
|
0 commit comments