importrequestsfromrequestsimportgetimportreclass__RPC:def__init__(self,api_key,name):self.api_key=api_keyself.api_info=Noneself.name=nameself.errors={200:"Function successfully executed.",400:"Invalid API location. Check the URL that you are using.",403:"Invalid or missing API key. Check that your API key is present and matches your assigned key.",405:"Invalid HTTP method. Check that the method (POST|GET) matches what the documentation indicates.",412:"Request failed. Check the response body for a more detailed description.",500:"Internal server error. Try again at a later time.",503:"Rate limit hit. API requests are limited to an average of 2/s. Try your request again later."}defapi_info_initial(self):res=get("https://www.vultr.com/api/")html=res.textmethods=(m.group(1)forminre.finditer(r"<td>(POST|GET)</td>",html))names=(m.group(1)forminre.finditer(r"/v1/(.*?)</a>",html))self.api_info=dict(zip(names,methods))def__getattr__(self,name):returneval("__RPC")(self.api_key,self.name+"/"+name)def__call__(self,**kwargs):ifnotself.api_info:self.api_info_initial()ifself.namenotinself.api_info:raiseValueError("The API is not exists.")ifself.api_info[self.name]=="GET":res=requests.get("https://api.vultr.com/v1/"+self.name,headers={"API-Key":self.api_key},params=kwargs)elifself.api_info[self.name]=="POST":res=requests.post("https://api.vultr.com/v1/"+self.name,headers={"API-Key":self.api_key},data=kwargs)ifres.status_code==200:returnres.status_code,res.text.strip()elifres.status_codeinself.errors.keys():returnres.status_code,self.errors.get(res.status_code)else:res.raise_for_status()classVultr:def__init__(self,api_key):self.api_key=api_keydef__getattr__(self,name):returneval("__RPC")(self.api_key,name)