Package: anac
anacPythonAsyncNetBoxAPIClient, based onhttpxandpydanticDocumentationhttps://timeforplanb123.github.io/anacFeaturesMinimalistic interfaceAsync onlyPython interpreter autocompletionSupportsNetBox2.x, 3.xFlexibility. All the objects are coroutines or coroutine iteratorsSimple integration with parsers (TextFSM,TTP)Quick StartInstallPlease, at first, check the dependencies inpyproject.tomland create new virtual environment if necessary and then:with pip:pip install anacwith git:git clone https://github.com/timeforplanb123/anac.git
cd anac 
pip install .
# or
poetry installSimple ExamplesApi Instantiatingfromanacimportapia=api("https://demo.netbox.dev",token="cf1dc7b04de5f27cfc93aba9e3f537d2ad6fdf8c",)# get openapi spec and create attributes/endpointsawaita.openapi()getsome device andpatchitIn[1]:some_device=awaita.dcim_devices(get={"name":"dmi01-rochster-sw01"})In[2]:some_device.nameOut[2]:'dmi01-rochster-sw01'In[3]:some_device.device_typeOut[3]:{'id':7,'url':'https://demo.netbox.dev/api/dcim/device-types/7/','display':'C9200-48P','manufacturer':{'id':3,'url':'https://demo.netbox.dev/api/dcim/manufacturers/3/','display':'Cisco','name':'Cisco','slug':'cisco'},'model':'C9200-48P','slug':'c9200-48p'}In[4]:some_device.statusOut[4]:{'value':'active','label':'Active'}In[5]:some_device=awaitsome_device(patch={"status":"failed"})In[6]:some_device.statusOut[6]:{'value':'failed','label':'Failed'}getsome 2 devices andput+patchthemIn[7]:some_devices=awaita.dcim_devices(...:get=[{"name":"dmi01-rochster-sw01"},{"name":"dmi01-rochester-rtr01"}]...:)# EndpointAsIterator is a coroutine iterator with 2 coroutinesIn[8]:some_devicesOut[8]:EndpointAsIterator(api=Api,url='https://demo.netbox.dev/api',endpoint='/dcim/devices/')In[9]:importasyncio# run 2 coroutines in the event loopIn[10]:some_devices=awaitasyncio.gather(*some_devices)# EndpointId is a NetBox '/dcim/devices/' object and coroutineIn[11]:some_devicesOut[11]:[EndpointId(api=Api,url='https://demo.netbox.dev/api/dcim/devices/21/',endpoint='/dcim/devices/'),EndpointId(api=Api,url='https://demo.netbox.dev/api/dcim/devices/8/',endpoint='/dcim/devices/')]In[12]:patch_some_devices=[coro(patch={"status":"failed"})forcoroinsome_devices]# run 2 coroutines in the event loopIn[13]:patch_some_devices=awaitasyncio.gather(*patch_some_devices)# EndpointId is a coroutine, againIn[14]:patch_some_devicesOut[14]:[EndpointId(api=Api,url='https://demo.netbox.dev/api/dcim/devices/21/',endpoint='/dcim/devices/{id}/'),EndpointId(api=Api,url='https://demo.netbox.dev/api/dcim/devices/8/',endpoint='/dcim/devices/{id}/')]In[15]:patch_some_devices[0].nameOut[15]:'dmi01-rochster-sw01'In[16]:patch_some_devices[0].statusOut[16]:{'value':'failed','label':'Failed'}In[17]:patch_some_devices[1].nameOut[17]:'dmi01-rochester-rtr01'In[18]:patch_some_devices[1].statusOut[18]:{'value':'failed','label':'Failed'}getall devicesIn[19]:all_devices=awaita.dcim_devices(get={})# orIn[20]:all_devices=awaita.dcim_devices()# EndpointIdIterator is an coroutine iterator with EndpointId objectsIn[21]:all_devicesOut[21]:EndpointIdIterator(api=Api,url='https://demo.netbox.dev/api',endpoint='/dcim/devices/')In[22]:len(all_devices)Out[22]:50In[23]:all_devices[49]Out[23]:EndpointId(api=Api,url='https://demo.netbox.dev/api/dcim/devices/95/',endpoint='/dcim/devices/')In[24]:all_devices[49].nameOut[24]:'ncsu118-distswitch1'# by default, 'limit' parameter = 50, but you can run 'get' request with custom 'limit'In[25]:all_devices=awaita.dcim_devices(get={"limit":100})In[26]:len(all_devices)Out[26]:75In[27]:all_devices[74]Out[27]:EndpointId(api=Api,url='https://demo.netbox.dev/api/dcim/devices/106/',endpoint='/dcim/devices/')In[28]:all_devices[74].idOut[28]:106getall devices andpost2 new devicesIn[29]:all_test=awaita.dcim_devices(...:get={},...:post=[...:{...:"name":"test1",...:"device_role":1,...:"site":1,...:"device_type":1,...:"status":"planned",...:},...:{...:"name":"test2",...:"device_role":1,...:"site":1,...:"device_type":1,...:"status":"planned",...:},...:],...:)# run 3 coroutines in the event loopIn[30]:all_test=awaitasyncio.gather(*all_test)# EndpointIdIterator is an coroutine iterator with EndpointId objects# EndpointId is a NetBox '/dcim/devices/' object and coroutineIn[31]:all_testOut[31]:[EndpointIdIterator(api=Api,url='https://demo.netbox.dev/api',endpoint='/dcim/devices/'),EndpointId(api=Api,url='https://demo.netbox.dev/api/dcim/devices/110/',endpoint='/dcim/devices/'),EndpointId(api=Api,url='https://demo.netbox.dev/api/dcim/devices/111/',endpoint='/dcim/devices/')]In[32]:all_test[0][49].nameOut[32]:'ncsu118-distswitch1'In[33]:all_test[1].nameOut[34]:'test1'In[35]:all_test[2].nameOut[35]:'test2'# httpx.Response is available with .response attributeIn[36]:all_test[1].response.json()Out[36]:{'id':110,'url':'https://demo.netbox.dev/api/dcim/devices/110/','display':'test1','name':'test1','device_type':{'id':1,'url':'https://demo.netbox.dev/api/dcim/device-types/1/','display':'MX480','manufacturer':{'id':7,...}