2020
2121__all__ = [
2222 "API_HOST" ,
23- "API_ROOT" ,
2423 "DEFAULT_API_VERSION" ,
25- "LinodeException" ,
26- "LinodeResponse" ,
27- "LinodeConnection" ,
2824 "LinodeResponseV4" ,
2925 "LinodeConnectionV4" ,
3026 "LinodeExceptionV4" ,
3430
3531# Endpoint for the Linode API
3632API_HOST = "api.linode.com"
37- API_ROOT = "/"
38-
3933
4034DEFAULT_API_VERSION = "4.0"
4135
42- # Constants that map a RAM figure to a PlanID (updated 2014-08-25)
43- LINODE_PLAN_IDS = {
44- 1024 : "1" ,
45- 2048 : "2" ,
46- 4096 : "4" ,
47- 8192 : "6" ,
48- 16384 : "7" ,
49- 32768 : "8" ,
50- 49152 : "9" ,
51- 65536 : "10" ,
52- 98304 : "12" ,
53- }
54-
5536# Available filesystems for disk creation
56- LINODE_DISK_FILESYSTEMS = ["ext3" , "ext4" , "swap" , "raw" ]
5737LINODE_DISK_FILESYSTEMS_V4 = ["ext3" , "ext4" , "swap" , "raw" , "initrd" ]
5838
5939
60- class LinodeException (Exception ):
61- """Error originating from the Linode API
62-
63- This class wraps a Linode API error, a list of which is available in the
64- API documentation. All Linode API errors are a numeric code and a
65- human-readable description.
66- """
67-
68- def __init__ (self , code , message ):
69- self .code = code
70- self .message = message
71- self .args = (code , message )
72-
73- def __str__ (self ):
74- return "(%u) %s" % (self .code , self .message )
75-
76- def __repr__ (self ):
77- return "<LinodeException code %u '%s'>" % (self .code , self .message )
78-
79-
80- class LinodeResponse (JsonResponse ):
81- """
82- Linode API response
83-
84- Wraps the HTTP response returned by the Linode API.
85-
86- libcloud does not take advantage of batching, so a response will always
87- reflect the above format. A few weird quirks are caught here as well.
88- """
89-
90- objects = None
91-
92- def __init__ (self , response , connection ):
93- """Instantiate a LinodeResponse from the HTTP response
94-
95- :keyword response: The raw response returned by urllib
96- :return: parsed :class:`LinodeResponse`"""
97- self .errors = []
98- super ().__init__ (response , connection )
99-
100- self .invalid = LinodeException (0xFF , "Invalid JSON received from server" )
101-
102- # Move parse_body() to here; we can't be sure of failure until we've
103- # parsed the body into JSON.
104- self .objects , self .errors = self .parse_body ()
105-
106- if not self .success ():
107- # Raise the first error, as there will usually only be one
108- raise self .errors [0 ]
109-
110- def parse_body (self ):
111- """Parse the body of the response into JSON objects
112-
113- If the response chokes the parser, action and data will be returned as
114- None and errorarray will indicate an invalid JSON exception.
115-
116- :return: ``list`` of objects and ``list`` of errors"""
117- js = super ().parse_body ()
118-
119- try :
120- if isinstance (js , dict ):
121- # solitary response - promote to list
122- js = [js ]
123- ret = []
124- errs = []
125- for obj in js :
126- if "DATA" not in obj or "ERRORARRAY" not in obj or "ACTION" not in obj :
127- ret .append (None )
128- errs .append (self .invalid )
129- continue
130- ret .append (obj ["DATA" ])
131- errs .extend (self ._make_excp (e ) for e in obj ["ERRORARRAY" ])
132- return (ret , errs )
133- except Exception :
134- return (None , [self .invalid ])
135-
136- def success (self ):
137- """Check the response for success
138-
139- The way we determine success is by the presence of an error in
140- ERRORARRAY. If one is there, we assume the whole request failed.
141-
142- :return: ``bool`` indicating a successful request"""
143- return len (self .errors ) == 0
144-
145- def _make_excp (self , error ):
146- """Convert an API error to a LinodeException instance
147-
148- :keyword error: JSON object containing ``ERRORCODE`` and
149- ``ERRORMESSAGE``
150- :type error: dict"""
151- if "ERRORCODE" not in error or "ERRORMESSAGE" not in error :
152- return None
153- if error ["ERRORCODE" ] == 4 :
154- return InvalidCredsError (error ["ERRORMESSAGE" ])
155- return LinodeException (error ["ERRORCODE" ], error ["ERRORMESSAGE" ])
156-
157-
158- class LinodeConnection (ConnectionKey ):
159- """
160- A connection to the Linode API
161-
162- Wraps SSL connections to the Linode API, automagically injecting the
163- parameters that the API needs for each request.
164- """
165-
166- host = API_HOST
167- responseCls = LinodeResponse
168-
169- def add_default_params (self , params ):
170- """
171- Add parameters that are necessary for every request
172-
173- This method adds ``api_key`` and ``api_responseFormat`` to
174- the request.
175- """
176- params ["api_key" ] = self .key
177- # Be explicit about this in case the default changes.
178- params ["api_responseFormat" ] = "json"
179- return params
180-
181-
18240class LinodeExceptionV4 (Exception ):
18341 def __init__ (self , message ):
18442 self .message = message
@@ -187,7 +45,7 @@ def __str__(self):
18745 return "%s" % self .message
18846
18947 def __repr__ (self ):
190- return "<LinodeException '%s'>" % self .message
48+ return "<LinodeExceptionV4 '%s'>" % self .message
19149
19250
19351class LinodeResponseV4 (JsonResponse ):
0 commit comments