|
21 | 21 |
|
22 | 22 | __all__ = [ |
23 | 23 | "API_HOST", |
24 | | - "VultrConnection", |
25 | 24 | "VultrException", |
26 | | - "VultrResponse", |
27 | 25 | "DEFAULT_API_VERSION", |
28 | 26 | "VultrResponseV2", |
29 | 27 | "VultrConnectionV2", |
|
37 | 35 | DEFAULT_API_VERSION = "2" |
38 | 36 |
|
39 | 37 |
|
40 | | -class VultrResponse(JsonResponse): |
41 | | - objects = None |
42 | | - error_dict = {} # type: Dict[str, str] |
43 | | - errors = None |
44 | | - ERROR_CODE_MAP = { |
45 | | - 400: "Invalid API location. Check the URL that you are using.", |
46 | | - 403: "Invalid or missing API key. Check that your API key is present" |
47 | | - + " and matches your assigned key.", |
48 | | - 405: "Invalid HTTP method. Check that the method (POST|GET) matches" |
49 | | - + " what the documentation indicates.", |
50 | | - 412: "Request failed. Check the response body for a more detailed" + " description.", |
51 | | - 500: "Internal server error. Try again at a later time.", |
52 | | - 503: "Rate limit hit. API requests are limited to an average of 1/s." |
53 | | - + " Try your request again later.", |
54 | | - } |
55 | | - |
56 | | - def __init__(self, response, connection): |
57 | | - self.errors = [] |
58 | | - super().__init__(response=response, connection=connection) |
59 | | - self.objects, self.errors = self.parse_body_and_errors() |
60 | | - if not self.success(): |
61 | | - raise self._make_excp(self.errors[0]) |
62 | | - |
63 | | - def parse_body_and_errors(self): |
64 | | - """ |
65 | | - Returns JSON data in a python list. |
66 | | - """ |
67 | | - json_objects = [] |
68 | | - errors = [] |
69 | | - |
70 | | - if self.status in self.ERROR_CODE_MAP: |
71 | | - self.error_dict["ERRORCODE"] = self.status |
72 | | - self.error_dict["ERRORMESSAGE"] = self.ERROR_CODE_MAP[self.status] |
73 | | - errors.append(self.error_dict) |
74 | | - |
75 | | - js = super().parse_body() |
76 | | - if isinstance(js, dict): |
77 | | - js = [js] |
78 | | - |
79 | | - json_objects.append(js) |
80 | | - |
81 | | - return (json_objects, errors) |
82 | | - |
83 | | - def _make_excp(self, error): |
84 | | - """ |
85 | | - Convert API error to a VultrException instance |
86 | | - """ |
87 | | - |
88 | | - return VultrException(error["ERRORCODE"], error["ERRORMESSAGE"]) |
89 | | - |
90 | | - def success(self): |
91 | | - return len(self.errors) == 0 |
92 | | - |
93 | | - |
94 | | -class VultrConnection(ConnectionKey): |
95 | | - """ |
96 | | - A connection to the Vultr API |
97 | | - """ |
98 | | - |
99 | | - host = API_HOST |
100 | | - responseCls = VultrResponse |
101 | | - |
102 | | - def add_default_params(self, params): |
103 | | - """ |
104 | | - Returns default params such as api_key which is |
105 | | - needed to perform an action.Returns a dictionary. |
106 | | - Example:/v1/server/upgrade_plan?api_key=self.key |
107 | | - """ |
108 | | - params["api_key"] = self.key |
109 | | - |
110 | | - return params |
111 | | - |
112 | | - def add_default_headers(self, headers): |
113 | | - """ |
114 | | - Returns default headers such as content-type. |
115 | | - Returns a dictionary. |
116 | | - """ |
117 | | - headers["Content-Type"] = "application/x-www-form-urlencoded" |
118 | | - headers["Accept"] = "text/plain" |
119 | | - |
120 | | - return headers |
121 | | - |
122 | | - def set_path(self): |
123 | | - self.path = "/v/" |
124 | | - return self.path |
125 | | - |
126 | | - |
127 | 38 | class VultrResponseV2(JsonResponse): |
128 | 39 | valid_response_codes = [ |
129 | 40 | httplib.OK, |
|
0 commit comments