11import os
2- from typing import Optional , Union
2+ import warnings
3+ from typing import Optional , Union , List
34
45import requests
56
@@ -8,9 +9,7 @@ class Vultr(object):
89 url = "https://api.vultr.com/v2"
910
1011 def __init__ (self , api_key : Optional [str ] = None ):
11- """
12- :param api_key: Vultr API Key or `VULTR_API_KEY` Environment Variable
13- """
12+ """:param api_key: Vultr API Key or `VULTR_API_KEY` Environment Variable"""
1413 self .api_key = api_key or os .getenv ("VULTR_API_KEY" )
1514 """Provide the API key here or with the `VULTR_API_KEY` environment variable"""
1615 self ._session = requests .session ()
@@ -21,7 +20,7 @@ def get(self, url: str, params: Optional[dict] = None):
2120 """
2221 GET Data
2322 :param url: Request URL. Example `/instances`
24- :param params: Query Parameters
23+ :param params: Query Parameters Dictionary
2524 :return: Response Data
2625 """
2726 return self ._get (f"{ self .url } /{ url .lstrip ('/' )} " , params )
@@ -154,31 +153,48 @@ def delete_ipv4(self, instance: Union[str, dict]):
154153 url = f"{ self .url } /instances/{ instance_id } /ipv4"
155154 return self ._delete (url )
156155
156+ @staticmethod
157+ def filter_list (item_list : List [dict ], value : str , key : str = "name" ) -> dict :
158+ """
159+ Helper Function to get an Item from a List of Dictionaries
160+ :param item_list: List to filter
161+ :param value: Value of the Key
162+ :param key: Key to check for Value
163+ :return: Item or {}
164+ """
165+ return next ((d for d in item_list if str (d .get (key , "" )).lower () == value .lower ()), {})
166+
167+ @staticmethod
168+ def filter_regions (regions : list , locations : list ) -> list :
169+ return [d for d in regions if d ["id" ] in locations ]
170+
157171 @staticmethod
158172 def filter_keys (keys : list , name : str ) -> dict :
173+ """Soft Deprecated in 0.2.0. Use `Vultr.filter_list()`"""
174+ warnings .warn ("Soft Deprecated in 0.2.0. Use filter_list()" , PendingDeprecationWarning , stacklevel = 2 )
159175 try :
160176 return next (d for d in keys if d ["name" ].lower () == name .lower ())
161177 except StopIteration :
162178 return {}
163179
164180 @staticmethod
165181 def filter_os (os_list : list , name : str ) -> dict :
182+ """Soft Deprecated in 0.2.0. Use `Vultr.filter_list()`"""
183+ warnings .warn ("Soft Deprecated in 0.2.0. Use filter_list()" , PendingDeprecationWarning , stacklevel = 2 )
166184 try :
167185 return next (d for d in os_list if d ["name" ].lower () == name .lower ())
168186 except StopIteration :
169187 return {}
170188
171189 @staticmethod
172190 def filter_scripts (scripts : list , name : str ) -> dict :
191+ """Soft Deprecated in 0.2.0. Use `Vultr.filter_list()`"""
192+ warnings .warn ("Soft Deprecated in 0.2.0. Use filter_list()" , PendingDeprecationWarning , stacklevel = 2 )
173193 try :
174194 return next (d for d in scripts if d ["name" ].lower () == name .lower ())
175195 except StopIteration :
176196 return {}
177197
178- @staticmethod
179- def filter_regions (regions : list , locations : list ) -> list :
180- return [d for d in regions if d ["id" ] in locations ]
181-
182198 def _get (self , url , params : Optional [dict ] = None ):
183199 r = self ._session .get (url , params = params , timeout = 10 )
184200 if not r .ok :
0 commit comments