Guest User

Untitled

a guest
Dec 25th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.49 KB | None | 0 0
  1. #api.py
  2. import urllib2, re, json, hashlib
  3.  
  4. _key = ""
  5. _url = ""
  6.  
  7. _currEmail = ""
  8. _currPass = ""
  9.  
  10. _errs = []
  11.  
  12. _checkNums = "^\s*\d+\s*$"
  13. _checkEmail = "^[a-zA-Z0-9._%-+]+@[a-zA-Z0-9._%-]+.[a-zA-Z]{2,6}$"
  14. _checkCC = "^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$"
  15.  
  16. def initialize(key, url):
  17.   global _key, _url
  18.   _key = key
  19.   _url = url
  20.  
  21. def setCurrAcct(currEmail, currPass):
  22.   global _currEmail, _currPass
  23.   if not re.match(_checkEmail, currEmail):
  24.     _errs.append("api.setCurrAcct - validation - email (invalid)")
  25.   _currEmail = currEmail
  26.   _currPass = hashlib.sha256(currPass).hexdigest()
  27.  
  28. def _request(type, *args):
  29.   dataParams = []
  30.   urlParams = []
  31.  
  32.   if not _key:
  33.     _errs.append(("initialization", "must initialize with developer key for API"))
  34.   elif not _url:
  35.     _errs.append(("initialization", "must initialize with site at which API is running"))
  36.  
  37.   # seperate arguments into appropriate lists
  38.   for i in args:
  39.     i = str(i)
  40.     if re.search("=", i):
  41.       dataParams.append(i)
  42.       print i
  43.     else:
  44.       if i == "uN": # this is to prevent the header for the user API going out with the makeAcct function
  45.         iN = "u"
  46.         urlParams.append(iN)
  47.       else:
  48.         urlParams.append(i)
  49.  
  50.   append = "/" + "/".join(urlParams)
  51.   print "URL: " + _url
  52.   print "append: " + append
  53.  
  54.   opener = urllib2.build_opener(urllib2.HTTPHandler)
  55.   request = urllib2.Request(_url + append, "&".join(dataParams))
  56.   request.add_header('X-NAAMA-CLIENT-AUTHENTICATION', 'id="' + _key + '", version="1"')
  57.   request.add_header("Content-Type", "application/x-www-form-urlencoded");
  58.  
  59.   if args[0] == "u" or args[0] == "o":
  60.     if not _currEmail or not _currPass:
  61.       _errs.append(("user API", "valid email and password required to access user API"))
  62.     print "Hash is being created."
  63.     hash = hashlib.sha256(_currPass + _currEmail + append).hexdigest()
  64.     print "hash: " + hash
  65.     request.add_header("X-NAAMA-AUTHENTICATION", 'username="' + _currEmail + '", response="' + hash + '", version="1"')
  66.     print "header looks like: " + 'username="' + _currEmail + '", response="' + hash + '", version="1"'
  67.  
  68.   if _errs:
  69.     print _errs
  70.     return _errs
  71.     raise
  72.  
  73.   request.get_method = lambda: type
  74.   call = opener.open(request)
  75.  
  76.   result = call.read()
  77.   print result # use next line for production -- this one is for testing
  78.   return json.loads(result)
Add Comment
Please, Sign In to add comment