<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: 500 Error when Camera is Offline in Network Platform API</title>
    <link>https://community.cisco.com/t5/network-platform-api/500-error-when-camera-is-offline/m-p/5446272#M7219</link>
    <description>&lt;P&gt;Ha. Nevermind, I saw it as soon as I posted it. *smh* - then couldn't delete the post. &lt;/P&gt;&lt;P&gt;quit()&lt;/P&gt;&lt;P&gt;ugh. &lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
    <pubDate>Wed, 02 Dec 2020 12:32:46 GMT</pubDate>
    <dc:creator>karlwyatt</dc:creator>
    <dc:date>2020-12-02T12:32:46Z</dc:date>
    <item>
      <title>500 Error when Camera is Offline</title>
      <link>https://community.cisco.com/t5/network-platform-api/500-error-when-camera-is-offline/m-p/5446268#M7215</link>
      <description>&lt;P&gt;First, Please don't laugh at my code, I'm a network engineer, I've pieced this together from various sources. &lt;/P&gt;&lt;P&gt;Second, its incomplete. So I know there are things that just don't work. &lt;/P&gt;&lt;P&gt;So here is what it does. I have cameras that are in close to 200 different networks. In most cases there is 1 camera per network. For quality\compliance reasons I need to review each cameras placement and status at least once quarterly. I found it some what annoying to tackle this via the dashboard so I decided to figure out how to use the API. &lt;/P&gt;&lt;P&gt;Enter my python script below. When run it asks for the API Key and then I lists the available orgs. Eventually I'll have 3 options 1) snap a pic from all cameras, 2) choose the camera to snap a pic from 3) exit. The first option allows me to just let it run and look later, the second option allows me to pull a specific camera if I want to review one, or check placement after adjustments are made, it lists out networks that have cameras&lt;/P&gt;&lt;P&gt;Okay - now that we are past what it does, I can say it does work.... so long as the camera is online. &lt;/P&gt;&lt;P&gt;If you look at somewhere around line 39 (if copy and paste works) i have the line "&lt;SPAN&gt;snapurl = dashboard.camera.generateDeviceCameraSnapshot(indv_device[&lt;/SPAN&gt;&lt;SPAN&gt;'serial'&lt;/SPAN&gt;&lt;SPAN&gt;])" &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="inherit"&gt;&lt;SPAN&gt;I need help with some error handling here - if the camera is offline it can't create the snapshot url and throws a 500 error. It still tries &lt;/SPAN&gt;&lt;/FONT&gt;because&lt;FONT face="inherit"&gt;&lt;SPAN&gt; the dashboard knows about the device, but fails. I've tried to use try: with request response but maybe &lt;/SPAN&gt;&lt;/FONT&gt;I'm&lt;FONT face="inherit"&gt;&lt;SPAN&gt; doing something wrong? With the 500 error it just crashes the script with this message - "meraki.exceptions.APIError: camera, generateDeviceCameraSnapshot - 500 Internal Server Error, &amp;lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "&lt;A href="http://www.w3.org/TR/xhtml1/DTD/xhtm" target="_blank" rel="noopener nofollow noreferrer"&gt;http://www.w3.org/TR/xhtml1/DTD/xhtm&lt;/A&gt;"&lt;BR /&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="inherit"&gt;&lt;SPAN&gt;Does anyone have a tip to error handle a 500 error that maybe just prints "Camera is Unresponsive or Offline" or is there a way to test before the 500 if the device is online? &lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="inherit"&gt;&lt;SPAN&gt;Thanks,&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample language-python"&gt;&lt;CODE&gt;import getpass
import requests
import meraki
import time

USER_KEY = getpass.getpass('Enter your Meraki API Value: ')
dashboard = meraki.DashboardAPI(USER_KEY, suppress_logging=True)
available_orgs = dashboard.organizations.getOrganizations()
print("\n---------------------------\nAvailable Organizations\n---------------------------")
for i in range(len(available_orgs)): 
    option = f"{i}: {available_orgs[i]['name']}" 
    print(option)
org_selection = input(f"\nSelect an organization [0-{len(available_orgs)-1}]: ")
org = available_orgs[int(org_selection)]
answer = input("Choose what you would like to do:\n    1) Capture snapshot from all camers\n    2) Capture snapshot from a single camera\n    3) Exit\nPlease type 1,2 or 3: ")
answer = '2'
if answer == '1':
    print("I don't do this yet")
if answer == '2':
    networks = dashboard.organizations.getOrganizationNetworks(org['id'])
    print("\n-------------------------------\nAvailable Networks With Cameras\n-------------------------------")
    num_networks = len(networks)
    for n in range(num_networks): 
        network = networks[n]
        dev_network = dashboard.networks.getNetworkDevices(network['id'])
        num_device = len(dev_network)
        for s in range(num_device):
            indv_device = dev_network[s]
            if indv_device['model'] == 'MV12N':
                netoption = f"{n}: {networks[n]['name']}" 
                print(netoption)
    net_selection = input(f"\nSelect a Network to capture image from [0-{len(networks)-1}]: ")
    camera_net = networks[int(net_selection)]
    dev_network = dashboard.networks.getNetworkDevices(camera_net['id'])
    num_device = len(dev_network)
    for s in range(num_device):
        indv_device = dev_network[s]
        if indv_device['model'] == 'MV12N':
            snapurl = dashboard.camera.generateDeviceCameraSnapshot(indv_device['serial'])
            file_name = indv_device['name']
            snapshot = snapurl['url']
            time.sleep(5)
            with open(file_name + '.jpg', 'wb') as handle:
                response = requests.get(snapshot, stream=True, headers={'User-Agent': 'Custom'})
                if not response =='200':
                    print("Faild to download automaticly - see if image exists at: ", snapshot)
                for chunk in response.iter_content(1024):
                    if not chunk:
                        break
                    handle.write(chunk)
                print("Snapshot Saved from ", indv_device['name'])            
if answer == '3':
        quit &lt;/CODE&gt;&lt;/PRE&gt;&lt;DIV&gt; &lt;/DIV&gt;</description>
      <pubDate>Tue, 01 Dec 2020 23:51:02 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-platform-api/500-error-when-camera-is-offline/m-p/5446268#M7215</guid>
      <dc:creator>karlwyatt</dc:creator>
      <dc:date>2020-12-01T23:51:02Z</dc:date>
    </item>
    <item>
      <title>Re: 500 Error when Camera is Offline</title>
      <link>https://community.cisco.com/t5/network-platform-api/500-error-when-camera-is-offline/m-p/5446269#M7216</link>
      <description>&lt;P&gt;Untested, but something like:&lt;/P&gt;&lt;P&gt;try:&lt;/P&gt;&lt;P&gt;  snapurl = dashboard.camera.generateDeviceCameraSnapshot(indv_device['serial'])&lt;/P&gt;&lt;P&gt;  file_name = indv_device['name']&lt;/P&gt;&lt;P&gt;  ...&lt;/P&gt;&lt;P&gt;except meraki.APIError as e:&lt;/P&gt;&lt;P&gt;  print(e)&lt;/P&gt;&lt;P&gt;  &lt;/P&gt;</description>
      <pubDate>Wed, 02 Dec 2020 00:24:47 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-platform-api/500-error-when-camera-is-offline/m-p/5446269#M7216</guid>
      <dc:creator>Philip D'Ath</dc:creator>
      <dc:date>2020-12-02T00:24:47Z</dc:date>
    </item>
    <item>
      <title>Re: 500 Error when Camera is Offline</title>
      <link>https://community.cisco.com/t5/network-platform-api/500-error-when-camera-is-offline/m-p/5446270#M7217</link>
      <description>&lt;P&gt;You might like this article I wrote about API keys.&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.meraki.com/t5/Developers-APIs/A-newer-safer-way-to-access-the-dashboard-API/m-p/69602" target="_self"&gt;https://community.meraki.com/t5/Developers-APIs/A-newer-safer-way-to-access-the-dashboard-API/m-p/69602&lt;/A&gt; &lt;/P&gt;</description>
      <pubDate>Wed, 02 Dec 2020 00:27:08 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-platform-api/500-error-when-camera-is-offline/m-p/5446270#M7217</guid>
      <dc:creator>Philip D'Ath</dc:creator>
      <dc:date>2020-12-02T00:27:08Z</dc:date>
    </item>
    <item>
      <title>Re: 500 Error when Camera is Offline</title>
      <link>https://community.cisco.com/t5/network-platform-api/500-error-when-camera-is-offline/m-p/5446271#M7218</link>
      <description>&lt;P&gt;Yeah this kind of worked, it at least handles the error, but then gives me a new one that comes up after the try\except - &lt;/P&gt;&lt;P&gt;NameError: name 'snapurl' is not defined&lt;/P&gt;&lt;P&gt;So I tried to add a quit to the "except" for now, then i was going to figure out what better to do later but that doesn't work either, it seems to ignore the quit, and keeps moving.&lt;/P&gt;&lt;PRE class="lia-code-sample language-python"&gt;&lt;CODE&gt;except meraki.APIError as e:
                print(e)
                quit
snapshot = snapurl['url']&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;It still results in - NameError: name 'snapurl' is not defined - at this line&lt;/P&gt;</description>
      <pubDate>Wed, 02 Dec 2020 12:27:43 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-platform-api/500-error-when-camera-is-offline/m-p/5446271#M7218</guid>
      <dc:creator>karlwyatt</dc:creator>
      <dc:date>2020-12-02T12:27:43Z</dc:date>
    </item>
    <item>
      <title>Re: 500 Error when Camera is Offline</title>
      <link>https://community.cisco.com/t5/network-platform-api/500-error-when-camera-is-offline/m-p/5446272#M7219</link>
      <description>&lt;P&gt;Ha. Nevermind, I saw it as soon as I posted it. *smh* - then couldn't delete the post. &lt;/P&gt;&lt;P&gt;quit()&lt;/P&gt;&lt;P&gt;ugh. &lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Wed, 02 Dec 2020 12:32:46 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-platform-api/500-error-when-camera-is-offline/m-p/5446272#M7219</guid>
      <dc:creator>karlwyatt</dc:creator>
      <dc:date>2020-12-02T12:32:46Z</dc:date>
    </item>
  </channel>
</rss>

