11import getpass
2- from urllib .parse import urljoin
32
43from pythonanywhere_core .base import call_api , get_api_endpoint
54
65
76class Website :
7+ """ Interface for PythonAnywhere websites API.
8+
9+ Uses ``pythonanywhere_core.base`` :method: ``get_api_endpoint`` to
10+ create url, which is stored in a class variable ``Website.api_endpoint``,
11+ then calls ``call_api`` with appropriate arguments to execute websites
12+ action.
13+
14+ Use :method: ``Website.create`` to create new website.
15+ Use :method: ``Website.get`` to get website info.
16+ Use :method: ``Website.list`` to get all websites list.
17+ Use :method: ``Website.reload`` to reload website.
18+ Use :method: ``Website.delete`` to delete website.
19+ """
20+
821 api_endpoint = get_api_endpoint (username = getpass .getuser (), flavor = "websites" )
922
10- def create (self , domain_name : str , command : str ):
23+ def create (self , domain_name : str , command : str ) -> dict :
24+ """Creates new website with ``domain_name`` and ``command``.
25+
26+ :param domain_name: domain name for new website
27+ :param command: command for new website
28+ :returns: dictionary with created website info"""
29+
1130 response = call_api (
1231 self .api_endpoint ,
1332 "post" ,
@@ -19,18 +38,45 @@ def create(self, domain_name: str, command: str):
1938 )
2039 return response .json ()
2140
22- def get (self , domain_name : str ):
41+ def get (self , domain_name : str ) -> dict :
42+ """Returns dictionary with website info for ``domain_name``.
43+ :param domain_name:
44+ :return: dictionary with website info"""
45+
2346 response = call_api (
24- urljoin ( self .api_endpoint , domain_name ) ,
47+ f" { self .api_endpoint } { domain_name } /" ,
2548 "get" ,
2649 )
2750 return response .json ()
2851
29- def list (self ):
30- raise NotImplemented
52+ def list (self ) -> list :
53+ """Returns list of dictionaries with all websites info.
54+ :return: list of dictionaries with websites info"""
3155
32- def reload (self ):
33- raise NotImplemented
56+ response = call_api (
57+ self .api_endpoint ,
58+ "get" ,
59+ )
60+ return response .json ()
61+
62+ def reload (self , domain_name : str ) -> dict :
63+ """Reloads website with ``domain_name``.
64+ :param domain_name: domain name for website to reload
65+ :return: dictionary with response"""
3466
35- def delete (self ):
36- raise NotImplemented
67+ response = call_api (
68+ f"{ self .api_endpoint } { domain_name } /reload/" ,
69+ "post" ,
70+ )
71+ return response .json ()
72+
73+ def delete (self , domain_name : str ) -> dict :
74+ """Deletes website with ``domain_name``.
75+ :param domain_name: domain name for website to delete
76+ :return: empty dictionary"""
77+
78+ call_api (
79+ f"{ self .api_endpoint } { domain_name } /" ,
80+ "delete" ,
81+ )
82+ return {}
0 commit comments