0

I want to check these endpoints if the answer is other than 0 it is ok but if 0 it is not ok. After I have done it these checks, I want to send the result to an 'outlook' email, can anyone help me with it.

This is my code so far.

import json, requests, urllib.parse, re
from termcolor import colored
import numpy as np

cdwenv1 = 'cdwu' #Note that it only works with the http version right now
cdwenv2 = 'cdwp'

#Dev Static
cdwenv  = '' #leave empty

# static
cdwEndPoints = [
                 'http://cdwu/cdw/counterparties?count=true'
                ,'http://cdwu/identifier/assets?count=true'
                ,'http://cdwu/identifier/assetListings?count=true'
]

# Check that Counts are different from 0
import sys

def countsCDWdata(input: list, cdwenv1, flag=0):
   results = []
   for i, item in enumerate(input):
        result = []
        if '?' in item:
            cdwcounturl = item
        try:
            r = requests.get(cdwcounturl)
        except:
                print(cdwcounturl, colored('is erroring', 'red'))
        result.append([cdwenv1, r.text])
        sys.stdout.flush()
        sys.stdout.write('\r Queried '+ str(i) + ' out of ' + str(len(input)))   
        results.append([re.findall('//.*?/.*?/(.*?)/',item)[0], result[0], result[0]])
   if flag == 1:
      return(results)
   df = pd.DataFrame(results, columns = ['CDWEndpoint', 'CDW Env', 'Counts'])
   df['Status'] = np.where(df['Counts1'] != 0, colored('Ok', 'green'), colored('not Ok', 'red'))
   sys.stdout.flush()
   sys.stdout.write('\r')
   print(df)

I am getting this error:

http://cdwu/cdw/counterparties?count=true is erroring
---------------------------------------------------------------------------
http://cdwu/cdw/counterparties?count=true is erroring
---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-72-797620017bbc> in <module>()
----> 1 countsCDWdata(cdwEndPoints, [cdwenv1])

<ipython-input-70-73176a33caf9> in countsCDWdata(input, cdwenv1, flag)
      9         except:
     10                 print(cdwcounturl, colored('is erroring', 'red'))
---> 11         result.append([cdwenv1, r.text])
     12         sys.stdout.flush()
     13         sys.stdout.write('\r Queried '+ str(i) + ' out of ' + str(len(input)))

UnboundLocalError: local variable 'r' referenced before assignment
2
  • The r variable is only assigned line 12 => You don't achieve the try, fall in the except => r kind of "doesn't exist". Which lead to this error. And look at the first line of the error http://cdwu/cdw/counterparties?count=true is erroring, it looks like the error printing line 14. Commented Sep 27, 2021 at 10:03
  • Notwithstanding the previous comment, using input as a variable name is inadvisable Commented Sep 27, 2021 at 10:49

1 Answer 1

1
import json
import jsonschema
from jsonschema import validate

def validateJson(jsonData):
    try:
        validate(instance=jsonData, schema=studentSchema)
    except jsonschema.exceptions.ValidationError as err:
        return False
    return True
Sign up to request clarification or add additional context in comments.

1 Comment

Please find the official docs where jsonschema.validate is explained. One may require for the exception to not be caught at this level.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.