def load_from_url(self, url): """ reaches out to a url and loads the profile :param url: :return: dict: profile """ print type(url) if type(url) == list: # if we are fed a list of urls # resolve each one and merge them with the later url taking precedence outputDict = {} for u in url: Base.info("Attempting to fetch profile at: %s" % u) response = requests.get(u, verify=False) response.raise_for_status() print json.loads(str(response.text)) outputDict = dict( self.merge_profiles(outputDict, json.loads(str(response.text)))) pprint.pprint(outputDict) return outputDict else: response = requests.get(url, verify=False) response.raise_for_status() return json.loads(str(response.text))
def load_from_url(self, url): """ reaches out to a url and loads the profile :param url: :return: dict: profile """ print type(url) if type(url) == list: # if we are fed a list of urls # resolve each one and merge them with the later url taking precedence outputDict = {} for u in url : Base.info("Attempting to fetch profile at: %s" % u) response = requests.get(u, verify=False) response.raise_for_status() print json.loads(str(response.text)) outputDict = dict(self.merge_profiles(outputDict, json.loads(str(response.text)))) pprint.pprint(outputDict) return outputDict else: response = requests.get(url, verify=False) response.raise_for_status() return json.loads(str(response.text))
def get_time_difference(curr_line, prev_line): if "'" not in curr_line and "'" not in prev_line: curr_event = json.loads(curr_line) prev_event = json.loads(prev_line) difference = (float(curr_event['down']) - float(prev_event['up'])) / 1000 return difference else: return 0
def get_time_pause(curr_line, prev_line): if "'" not in curr_line and "'" not in prev_line: curr_event = json.loads(curr_line) prev_event = json.loads(prev_line) pause = prev_event['pause'] return pause else: return 0
def createRelease(self, version, project, selectedPackages): projectId = self.getProjectId(project) url = '/api/releases' packages = json.loads(selectedPackages) data = { "Version": version, "ProjectId": projectId, "SelectedPackages": packages } response = self.httpRequest.post(url, headers=self.headers, body=json.dumps(data)) print("HTTP_STATUS = %s" % response.getStatus()) if response.getStatus() in HTTP_SUCCESS: data = json.loads(response.getResponse()) return data["Id"] self.throw_error(response)
def wait_for_deploy(self, project_name, deploy_id): # /projects/gcr-watcher/deploys/44.json url = "/projects/%s/deploys/%s.json" % (project_name, deploy_id) status = "" number_of_checks = 0 while status not in ["succeeded", "failed"]: response = self.httpRequest.get(url, headers=self.headers) if response.getStatus() in HTTP_SUCCESS: depData = json.loads(response.getResponse()) status = depData["status"] depReportUrl = depData["url"] number_of_checks += 1 if number_of_checks > self.maxChecksPerDeploymentId: sys.exit("Fail: project %s deployment %s failed" % (project_name, deploy_id)) time.sleep(self.timeout) else: self.throw_error(response) if status == "failed": sys.exit("Fail: project %s deployment %s failed" % (project_name, deploy_id)) else: print "Success: project %s deployment %s succeeded" % ( project_name, deploy_id)
def findReleaseWithId(credentials, releaseId): xlrAPIUrl = xlrUrl + '/api/v1/releases/' + releaseId request = XLRequest(xlrAPIUrl, 'GET', None, credentials['username'], credentials['password'], 'application/json').send() if request.status == 200: release = json.loads(request.read()) return release
def parse_error_file(self, parseTask): """Extract errors from JSON file """ #jyson import jysonModule = File.separator.join( [parseTask.app.SCRIPTDIR, "tools", "jyson.jar"]) if jysonModule not in sys.path: sys.path.append(jysonModule) from com.xhaus.jyson import JysonCodec as json data = json.loads(parseTask.app.errorsData) for e in data["errors"]: #lat, lon, errorId lat = float(e[0]) lon = float(e[1]) errorId = e[2] errorType = e[3] # osmose item #osmId osmId = e[6] osmId = osmId.replace("node", "n") osmId = osmId.replace("way", "w") osmId = osmId.replace("relation", "r") other = [errorType] #desc if e[8] != "": desc = "%s %s" % (e[8], e[9]) else: desc = e[9] #bbox bbox = parseTask.build_bbox(lat, lon) #Append to errors if errorType in parseTask.errors: parseTask.errors[errorType].append( (osmId, (lat, lon), bbox, errorId, desc, other)) return True
def get_change_request(self, table_name, sysId): servicenow_api_url = '/api/now/v1/table/%s/%s' % (table_name, sysId) response = self.httpRequest.get(servicenow_api_url, contentType='application/json') if response.getStatus() == SN_RESULT_STATUS: data = json.loads(response.getResponse()) return data['result'] self.throw_error(response)
def queryIssues(self, query, options=None): if not query: error('No JQL query provided.') # Create POST body content = { 'jql': query, 'startAt': 0, 'fields': ['summary', 'status', 'assignee'] } # Do request request = self._createRequest() response = request.post('/rest/api/2/search', self._serialize(content), contentType='application/json') # Parse result if response.status == 200: issues = {} data = Json.loads(response.response) for item in data['issues']: issue = item['key'] issues[issue] = { 'issue' : issue, 'summary' : item['fields']['summary'], 'status' : item['fields']['status']['name'], 'assignee': item['fields']['assignee']['displayName'], 'link' : "{1}/browse/{0}".format(issue, self.jira_server['url']) } return issues else: error(u"Failed to execute search '{0}' in JIRA.".format(query), response)
def update_variable_store(variable_store_title, variables): conn_fac = HttpRequest({'url': 'http://localhost:5516'}, release.scriptUsername, release.scriptUserPassword) response = conn_fac.get('/configurations', contentType='application/json') data = json.loads(response.getResponse()) variable_store_ci = None for i in range(0, len(data)): if data[i][ 'type'] == 'vars.VariableStore' and variable_store_title == data[ i]['properties']['title']: variable_store_ci = data[i] break if not variable_store_ci: print "ERROR: Unable to find variable store '%s'" % ( variable_store_title) sys.exit(1) variable_store_ci['properties']['variablesJson'] = json.dumps(variables) response = conn_fac.put('/configurations/%s' % (variable_store_ci['id']), json.dumps(variable_store_ci), contentType='application/json') if not response.isSuccessful: print "ERROR: Unable to update variable store '%s':" % ( variable_store_title) response.errorDump() sys.exit(1)
def execute_test_specification(self, test_specification_name): test_specification_id = self.get_test_specification_id(test_specification_name) xltestview_api_url = "/api/internal/execute/%s" % test_specification_id content = '{"id":"%s"}' % test_specification_id test_specification_response = self.http_request.post(xltestview_api_url, content, contentType='application/json') result = json.loads(test_specification_response.getResponse()) return result['taskId']
def createIssue(self, project, title, description, issue_type): # Create POST body content = { 'fields': { 'project': { 'key': project }, 'summary': title, 'description': description, 'issuetype': { 'name': string.capwords(issue_type) } } } # Do request request = self._createRequest() response = request.post('/rest/api/2/issue', self._serialize(content), contentType='application/json') # Parse result if response.status == 201: data = Json.loads(response.response) issue_id = data.get('key') print u"Created {0} in JIRA.".format(self._link(issue_id)) return issue_id else: error("Failed to create issue in JIRA.", response)
def do_it(self,verb, path, data=None, params=None, parse_response=True): print "downloading json from %s" % self.url error = 300 url = "%s%s" % (self.url, path) if verb == "GET": output = requests.get(url) elif verb =="POST": if params: output = requests.post(url,params=params ) elif data: output = requests.post(url, data=data) else: output = requests.post(url) else: print "method %s not supported yet " % verb return None output.raise_for_status() print "Download from %s : succesfull" % url if parse_response: json_data = str(output.text) decoded = json.loads(json_data) print decoded return decoded else: return output.text
def load_profile(profile): sp = SearchParameters() sp.setType(Type.valueOf('rel.ReleaseProfile')) for p in XLReleaseServiceHolder.getRepositoryService().listEntities(sp): if str(p.getTitle()) == profile: return json.loads(p.getProperty('profileJson'))
def get_latest_version(self, image_name): api_url = '/%s/tags/list' % (image_name) response = self.httpRequest.get(api_url, contentType='application/json', headers=self.headers) if response.getStatus() == SUCCESS_RESULT_STATUS: versions_list = json.loads(response.getResponse())['tags'] #print("Versions founds: %s" % str(versions_list)) # the will always be a 'latest' but that's not helpful to us to see if there's a new version if "latest" in versions_list: versions_list.remove("latest") # End if try: versions_list.sort(key=LooseVersion) #print("Tags sorted %s" % str(versions_list)) except Exception, e: print("Failed to sort, ignoring: %s " % str(e)) # End try if len(versions_list) > 0: return versions_list[-1] else: return None
def queryIssues(self, query, options=None): if not query: error('No JQL query provided.') # Create POST body content = { 'jql': query, 'startAt': 0, 'fields': ['summary', 'status', 'assignee'] } # Do request request = self._createRequest() response = request.post('/rest/api/2/search', self._serialize(content), contentType='application/json') # Parse result if response.status == 200: issues = {} data = Json.loads(response.response) for item in data['issues']: issue = item['key'] issues[issue] = { 'issue': issue, 'summary': item['fields']['summary'], 'status': item['fields']['status']['name'], 'assignee': item['fields']['assignee']['displayName'], 'link': "{1}/browse/{0}".format(issue, self.jira_server['url']) } return issues else: error(u"Failed to execute search '{0}' in JIRA.".format(query), response)
def verifyAndGetMetaData(artist_song): if not artist_song: return singers = artist_song['artist'] song = artist_song['song'] singers = singers.strip() song = song.strip() esinger = urllib.quote(singers.encode('utf-8')) esong = urllib.quote(song.encode('utf-8')) # 验证音乐是否有效 success = 0 valid = False query = u'播放' + singers + u'的' + song valid_url = '' try: valid_url = 'http://m.mobvoi.com/search/qa/?output=lite&appkey=com.mobvoi.home&query=' + urllib.quote( query.encode('utf-8')) response = urllib2.urlopen(valid_url) data = response.read() line_dict = json.loads(data) if 'clientAction' in line_dict: domain = line_dict['domain'] clientActionDict = line_dict['clientAction'] if len(clientActionDict) == 0: return action = clientActionDict['action'] if 'public.music' == domain and 'com.mobvoi.semantic.action.TENCENT.MEDIA.PLAY' == action: valid = True except Exception, e: traceback.print_exc() return
def get_change_request(self, table_name,sysId): servicenow_api_url = '/api/now/v1/table/%s/%s' % (table_name, sysId) response = self.httpRequest.get(servicenow_api_url, contentType = 'application/json') if response.getStatus() == SN_RESULT_STATUS: data = json.loads(response.getResponse()) return data['result'] self.throw_error(response)
def Load(self): f = open(self.cf,"r") js = f.read() f.close() self.cfg = json.loads(js) self.Host = self.cfg["Host"].encode("latin-1") self.Port = self.cfg["Port"] self.MasterName = self.cfg["MasterName"].encode("latin-1") self.MasterPassword = self.cfg["MasterPassword"].encode("latin-1") if self.__password: self.MasterPassword+="*" + self.__password self.MasterArena = self.cfg["MasterArena"].encode("latin-1") self.MasterChats = self.cfg["MasterChats"].encode("latin-1") self.ConfigurationFile = self.cfg["ConfigurationFile"].encode("latin-1") self.Modules = [ (b["Name"].encode("latin-1"),b["Param"].encode("latin-1")) for b in self.cfg["Modules"] ] self.AutoLoad = [ (b["Type"].encode("latin-1"),b["Arena"].encode("latin-1")) for b in self.cfg["AutoLoad"] ] self.paths = [ p.encode("latin-1") for p in self.cfg["Paths"]] self.Bots = {} nb = None for b in self.cfg["Bots"]: nb = BotConfiguration(b,self.__password) self.Bots[nb.Type] = nb
def __init__(self, *args, **kwargs): """ TODO: need to come up with a way to resolve variables used inside the profile Best way to do this i think is to pull all available variables form the xlr context and match the dictonary's values against them :param args: :param kwargs: :return: """ # pull in the xlrelease apis self.__releaseApi = XLReleaseServiceHolder.getReleaseApi() self.__repositoryService = XLReleaseServiceHolder.getRepositoryService() self.__taskApi = XLReleaseServiceHolder.getTaskApi() self.__phaseApi = XLReleaseServiceHolder.getPhaseApi() self.__variable_start_regex = re.compile('\$\{', re.IGNORECASE) # TODO: replace with {} once testing is done self.__variable_start_string = "$<" self.__variable_end_string = ">" self.store = dict() if kwargs.has_key('url'): kwargs = self.load_from_url(kwargs['url']) elif kwargs.has_key('repoId'): kwargs = self.load_profile_from_xlr_repo(kwargs['repoId']) elif kwargs.has_key('repoString'): kwargs = json.loads(str(kwargs['repoString'])) self.update(dict(*args, **kwargs)) # use the free update to set keys
def __init__(self, sd, query, output='json'): """ Execute the query and store the results. """ self._sd = sd self._query = query self._data = {'output': output, 'query': query} #self.response = requests.get(self._sd.url, params = self._data) #print "url = " + str(self._sd.url) conn = httplib.HTTPConnection(self._sd.url) #print "conn = " + str(conn) #print "query = " + str(query) _path = self._sd.sqpath + "?" + urllib.urlencode(self._data) #print "path = " + str(_path) conn.request("GET", _path) response = conn.getresponse() if response.status != 200: print response.status, response.reason, "\n" raise IOError, "HTTP GET %s not successful" % _path head = response.msg #print "head = " + str(head) self.text = response.read() #print "body = " + str(self.text) self.json = json.loads(self.text) #print "json = " + str(self.json) conn.close()
def _transitionIssue(self, issue_id, new_status): issue_url = self._issueUrl(issue_id) # Find possible transitions request = self._createRequest() response = request.get(issue_url + "/transitions?expand=transitions.fields", contentType='application/json') if response.status != 200: error(u"Unable to find transitions for issue {0}".format(self._link(issue_id)), response) transitions = Json.loads(response.response)['transitions'] # Check transition wanted_transaction = -1 for transition in transitions: if transition['to']['name'].lower() == new_status.lower(): wanted_transaction = transition['id'] break if wanted_transaction == -1: error(u"Unable to find status {0} for issue {1}".format(new_status, self._link(issue_id))) # Prepare POST body transition_data = { "transition": { "id": wanted_transaction } } # Perform transition response = request.post(issue_url + "/transitions?expand=transitions.fields", self._serialize(transition_data), contentType='application/json') if response.status != 204: error(u"Unable to perform transition {0} for issue {1}".format(wanted_transaction, self._link(issue_id)), response)
def extracthashtags(hashtags): # print "[DEBUG]hashtags:", hashtags # print "[DEBUG]type(hashtags):", type(hashtags) # print '[DEBUG]json.loads(hashtags)', json.loads(hashtags) hashtags = json.loads(hashtags) if len(hashtags) > 0: return ' '.join([x['text'].encode("utf-8") for x in hashtags])
def _get_records(f_100, sug_json): try: url = 'http://libris.kb.se/xsearch' values = {'query' : 'forf:(%s) spr:swe' % f_100, 'format' : 'json'} data = urllib.urlencode(values) #print "XSEARCH URL: %s?%s" % (url, data) reply = urllib2.urlopen(url + "?" + data) response = reply.read().decode('utf-8') #print "got response", response, type(response) xresult = json.loads(response)['xsearch'] sug_json['records'] = xresult['records'] top_3 = xresult['list'][:3] top_titles = {} for p in top_3: top_titles[p['identifier']] = unicode(p['title']) sug_json['top_titles'] = top_titles except: print "exception in get_records" 0 return sug_json
def map(key, value, context): try: payload = json.loads(value) except: e = sys.exc_info()[0] context.write(Text("exception:%s" % e), Text(key)) return record = False try: ch = payload['chromeHangs'] if type(ch) == dict: record = len(ch['memoryMap']) > 0 except KeyError: pass try: lw = payload['lateWrites'] if type(lw) == dict: record = record or len(lw['memoryMap']) > 0 except KeyError: pass if record: outkey = Text(value) context.write(outkey, Text())
def start_deploy(self, releaseId, environment): environmentId = self.getEnvironmentId(environment) url = '/api/deployments' data = { "ReleaseId": releaseId, "EnvironmentId": environmentId, "TenantId": None, "SkipActions": [], "QueueTime": None, "QueueTimeExpiry": None, "FormValues": {}, "ForcePackageDownload": False, "UseGuidedFailure": False, "SpecificMachineIds": [], "ExcludedMachineIds": [], "ForcePackageRedeployment": False } print("data = %s" % data) response = self.httpRequest.post(url, headers=self.headers, body=json.dumps(data)) if response.getStatus() in HTTP_SUCCESS: data = json.loads(response.getResponse()) print(json.dumps(data)) return data["Id"] self.throw_error(response)
def Load(self): f = open(self.cf, "r") js = f.read() f.close() self.cfg = json.loads(js) self.Host = self.cfg["Host"].encode("latin-1") self.Port = self.cfg["Port"] self.MasterName = self.cfg["MasterName"].encode("latin-1") self.MasterPassword = self.cfg["MasterPassword"].encode("latin-1") if self.__password: self.MasterPassword += "*" + self.__password self.MasterArena = self.cfg["MasterArena"].encode("latin-1") self.MasterChats = self.cfg["MasterChats"].encode("latin-1") self.ConfigurationFile = self.cfg["ConfigurationFile"].encode( "latin-1") self.Modules = [(b["Name"].encode("latin-1"), b["Param"].encode("latin-1")) for b in self.cfg["Modules"]] self.AutoLoad = [(b["Type"].encode("latin-1"), b["Arena"].encode("latin-1")) for b in self.cfg["AutoLoad"]] self.paths = [p.encode("latin-1") for p in self.cfg["Paths"]] self.Bots = {} nb = None for b in self.cfg["Bots"]: nb = BotConfiguration(b, self.__password) self.Bots[nb.Type] = nb
def parse_error_file(self, parseTask): """Extract errors from JSON file """ #jyson import jysonModule = File.separator.join([parseTask.app.SCRIPTDIR, "tools", "jyson.jar"]) if jysonModule not in sys.path: sys.path.append(jysonModule) from com.xhaus.jyson import JysonCodec as json data = json.loads(parseTask.app.errorsData) for e in data["errors"]: #lat, lon, errorId lat = float(e[0]) lon = float(e[1]) errorId = e[2] errorType = e[3] # osmose item #osmId osmId = e[6] osmId = osmId.replace("node", "n") osmId = osmId.replace("way", "w") osmId = osmId.replace("relation", "r") other = [errorType] #desc if e[8] != "": desc = "%s %s" % (e[8], e[9]) else: desc = e[9] #bbox bbox = parseTask.build_bbox(lat, lon) #Append to errors if errorType in parseTask.errors: parseTask.errors[errorType].append((osmId, (lat, lon), bbox, errorId, desc, other)) return True
def get_scorecards(self): servicenow_api_url = '/api/now/v1/pa/scorecards' response = self.httpRequest.get(servicenow_api_url, contentType='application/json') if response.getStatus() == SN_RESULT_STATUS: data = json.loads(response.getResponse()) return data['result'] self.throw_error(response)
def test_generate_payload(self): self.tm.create_all_metrics(1) thread = ingest.IngestThread(0) payload = json.loads(thread.generate_payload(0, [[2, 3], [2, 4], [2, 5]])) valid_payload = [ { u"collectionTime": 0, u"metricName": u"int.abcdefg.hijklmnop.qrstuvw.xyz.ABCDEFG.HIJKLMNOP.QRSTUVW.XYZ.abcdefg.hijklmnop.qrstuvw.xyz.met.3", u"metricValue": 0, u"tenantId": u"2", u"ttlInSeconds": 172800, u"unit": u"days", }, { u"collectionTime": 0, u"metricName": u"int.abcdefg.hijklmnop.qrstuvw.xyz.ABCDEFG.HIJKLMNOP.QRSTUVW.XYZ.abcdefg.hijklmnop.qrstuvw.xyz.met.4", u"metricValue": 0, u"tenantId": u"2", u"ttlInSeconds": 172800, u"unit": u"days", }, { u"collectionTime": 0, u"metricName": u"int.abcdefg.hijklmnop.qrstuvw.xyz.ABCDEFG.HIJKLMNOP.QRSTUVW.XYZ.abcdefg.hijklmnop.qrstuvw.xyz.met.5", u"metricValue": 0, u"tenantId": u"2", u"ttlInSeconds": 172800, u"unit": u"days", }, ] self.assertEqual(payload, valid_payload)
def checkQuery(self, query): if not query: error('No JQL query provided.') # Create POST body content = { 'jql': query, 'startAt': 0, 'fields': ['summary', 'status'] } # Do request request = self._createRequest() response = request.post('/rest/api/2/search', self._serialize(content), contentType='application/json') # Parse result if response.status == 200: data = Json.loads(response.response) issues = {} for item in data['issues']: issue = item['key'] issues[issue] = (item['fields']['summary'], item['fields']['status']['name']) return issues else: error(u"Failed to execute search '{0}' in JIRA.".format(query), response)
def start_deploy(self, webhook_id, commit_sha, message, branch="master"): url = "/integrations/generic/%s" % webhook_id body = { "deploy": { "branch": branch, "commit": { "sha": commit_sha, "message": message } } } print "body = %s" % json.dumps(body) # example response: {"deploy_ids":[],"messages":"INFO: Branch master is release branch: true\nINFO: Deploying to 0 stages\n"} response = self.httpRequest.post(url, headers=self.headers, body=json.dumps(body)) if response.getStatus() in HTTP_SUCCESS: data = json.loads(response.getResponse()) print json.dumps(data["deploy_ids"]) print json.dumps(data["messages"]) return data else: self.throw_error(response)
def do_POST(self): if 'content-length' in self.headers.dict: length = int(self.headers.dict['content-length']) else: logging.warn('content length required') self.send_error(400, 'content length required for post') return if 'content-type' not in self.headers.dict or self.headers.dict['content-type'] != 'text/json': logging.warn('content type missing or non-json') body = self.rfile.read(length) try: logging.debug('received: [%s]' % body) data_in = json.loads(body) except: logging.warn('content does not parse') self.send_error(400, 'content does not parse as valid json') return try: data_out = handle_request(data_in, self.server) except (Exception, java.lang.Exception), e: msg = "" if isinstance(e, java.lang.Exception): e.printStackTrace() #todo: log the java stacktrace elif isinstance(e, urllib2.HTTPError): if e.headers.get("content-type", "") == "text/plain": msg = e.read() logging.exception('error handling request') self.send_error(500, u'internal error handling request: %s: %s%s' % (type(e), str(e), u": %s" % msg if msg else "")) return
def execute(self, expr, forest) : sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try : sock.connect((self.HOST, self.PORT)) f = sock.makefile() print str(expr) f.write(str(expr) + "\n") f.write("\n") f.flush() #print type(forest) print forest.toDict() f.write("Batch 1.0 JSON 1.0\n") f.write(json.dumps(forest.toDict()) + "\n") header = f.readline() received = f.readline() #print "Header " + str(header) #print "Received " + str(received) f.close() finally : sock.close() # Possible no dictionary received, so hold off on loading if received : received = json.loads(received) else : received = {} new_forest = Forest(received) #print str(new_forest) return new_forest # Return the forest
def test_generate_payload(self): self.tm.create_all_metrics(1) thread = ingest.IngestThread(0) payload = json.loads( thread.generate_payload(0, [[2, 3], [2, 4], [2, 5]])) valid_payload = [{ u'collectionTime': 0, u'metricName': u'int.abcdefg.hijklmnop.qrstuvw.xyz.ABCDEFG.HIJKLMNOP.QRSTUVW.XYZ.abcdefg.hijklmnop.qrstuvw.xyz.met.3', u'metricValue': 0, u'tenantId': u'2', u'ttlInSeconds': 172800, u'unit': u'days' }, { u'collectionTime': 0, u'metricName': u'int.abcdefg.hijklmnop.qrstuvw.xyz.ABCDEFG.HIJKLMNOP.QRSTUVW.XYZ.abcdefg.hijklmnop.qrstuvw.xyz.met.4', u'metricValue': 0, u'tenantId': u'2', u'ttlInSeconds': 172800, u'unit': u'days' }, { u'collectionTime': 0, u'metricName': u'int.abcdefg.hijklmnop.qrstuvw.xyz.ABCDEFG.HIJKLMNOP.QRSTUVW.XYZ.abcdefg.hijklmnop.qrstuvw.xyz.met.5', u'metricValue': 0, u'tenantId': u'2', u'ttlInSeconds': 172800, u'unit': u'days' }] self.assertEqual(payload, valid_payload)
def postgres_lookup_session_command(cursor, key): postgres_lookup(cursor, POSTGRES_TABLE, 'sess_id', str(key), 'sess_json') if cursor.rowcount is 0: raise KeyError value = cursor.fetchone()[0] jsonobj = json.loads(value.decode('utf8')) return jsonobj
def do_POST(self): delay() if 'content-length' in self.headers.dict: length = int(self.headers.dict['content-length']) else: logging.warn('content length required') self.send_error(400, 'content length required for post') return if 'content-type' not in self.headers.dict or self.headers.dict['content-type'] != 'text/json': logging.warn('content type missing or non-json') body = self.rfile.read(length) try: logging.debug('received: [%s]' % body) data_in = json.loads(body) except: logging.warn('content does not parse') self.send_error(400, 'content does not parse as valid json') return try: data_out = handle_request(data_in, extensions=self.server.extensions) except (Exception, java.lang.Exception), e: if isinstance(e, java.lang.Exception): e.printStackTrace() #todo: log the java stacktrace logging.exception('error handling request') self.send_error(500, 'internal error handling request: %s: %s' % (type(e), str(e))) return
def queryForIssueIds(self, query): if not query: error('No JQL query provided.') # Create POST body content = { 'jql': query, 'startAt': 0, 'fields': ['summary'], 'maxResults': 1000 } # Do request request = self._createRequest() response = request.post('/rest/api/2/search', self._serialize(content), contentType='application/json') # Parse result if response.status == 200: data = Json.loads(response.response) print "#### Issues found" issueIds = [] for item in data['issues']: issueIds.append(item['id']) print u"* {0} - {1}".format(item['id'], item['key']) print "\n" return issueIds else: error(u"Failed to execute search '{0}' in JIRA.".format(query), response)
def test_generate_payload(self): agent_num = 1 generator = ingest.IngestGenerator(0, agent_num, MockReq(), self.test_config) payload = json.loads( generator.generate_payload(0, [[2, 3, 0], [2, 4, 0], [2, 5, 0]])) valid_payload = [{ 'collectionTime': 0, 'metricName': 'org.example.metric.3', 'metricValue': 0, 'tenantId': '2', 'ttlInSeconds': 172800, 'unit': 'days' }, { 'collectionTime': 0, 'metricName': 'org.example.metric.4', 'metricValue': 0, 'tenantId': '2', 'ttlInSeconds': 172800, 'unit': 'days' }, { 'collectionTime': 0, 'metricName': 'org.example.metric.5', 'metricValue': 0, 'tenantId': '2', 'ttlInSeconds': 172800, 'unit': 'days' }] self.assertEqual(payload, valid_payload)
def wait_for_deploy(self, deploymentId): url = '/api/deployments/%s' % deploymentId response = self.httpRequest.get(url, headers=self.headers) if response.getStatus() not in HTTP_SUCCESS: self.throw_error(response) deployment_details = json.loads(response.getResponse()) print(json.dumps(deployment_details)) taskUrl = deployment_details["Links"]["Task"] time.sleep(5) task_details = self.get_task_details(taskUrl) while not task_details["IsCompleted"]: task_details = self.get_task_details(taskUrl) print(json.dumps(task_details)) time.sleep(5) if task_details["FinishedSuccessfully"]: print("Deployment finished successfully.") else: msg = "Deployment failed, errors: [%s]" % task_details[ "ErrorMessage"] print(msg) sys.exit(msg)
def get_change_request_states(self): servicenow_api_url = '/api/now/v1/table/%s?element=state&name=task&sysparm_fields=%s' % ('sys_choice','value,label') response = self.httpRequest.get(servicenow_api_url, contentType = 'application/json') if response.getStatus() == SN_RESULT_STATUS: data = json.loads(response.getResponse()) return data['result'] self.throw_error(response)
def tick(self): # Burn through old messages, and only use the latest ones # This will allow the controller to keep working if the simulation slows # down. In the future they should be synchronized while True: if self.read_socket( self.sock_in ): try: data_in = json.loads( self.odom_str ) except JSONDecodeError: break self.sensor_data = data_in else: break self.position.set([self.sensor_data["roll"]]) self.velocity.set([self.sensor_data["wx"]]) self.counter += 1 if self.counter % CONTROL_PERIOD == 0: torque = np.array(self.torque.get())[0] # Send the command to the simulator as a torque data_out = '{"force":[0,0,0],"torque":[%f,0,0]}\n' % torque self.sock_out.send( data_out )
def do_POST(self): if "content-length" in self.headers.dict: length = int(self.headers.dict["content-length"]) else: logger.warn("content length required") self.send_error(400, "content length required for post") return if "content-type" not in self.headers.dict or self.headers.dict["content-type"] != "text/json": logger.warn("content type missing or non-json") body = self.rfile.read(length) try: logger.debug("received: [%s]" % body) data_in = json.loads(body) except: logger.warn("content does not parse") self.send_error(400, "content does not parse as valid json") return try: data_out = handle_request(data_in, self.server) reply = json.dumps(data_out) except TouchFormsBadRequest, e: self.send_error(400, str(e)) return
def __init__(self, sd, query, output = 'json'): """ Execute the query and store the results. """ self._sd = sd; self._query = query; self._data = { 'output' : output, 'query' : query } #self.response = requests.get(self._sd.url, params = self._data) #print "url = " + str(self._sd.url) conn = httplib.HTTPConnection(self._sd.url) #print "conn = " + str(conn) #print "query = " + str(query) _path = self._sd.sqpath + "?" + urllib.urlencode(self._data) #print "path = " + str(_path) conn.request("GET", _path ); response = conn.getresponse(); if response.status != 200: print response.status, response.reason, "\n" raise IOError, "HTTP GET %s not successful" % _path head = response.msg #print "head = " + str(head) self.text = response.read() #print "body = " + str(self.text) self.json = json.loads(self.text) #print "json = " + str(self.json) conn.close()
def body_handler(body): if resp.status_code == 200: favs['favorites'] = json.loads(body.to_string()) else: print "Failed to fetch favorites: %s" % body.to_string() EventBus.send('log.event', "user.favorites.list.result") EventBus.send('user.favorites.list.result', json.dumps(favs))
def __init__(self, *args, **kwargs): """ TODO: need to come up with a way to resolve variables used inside the profile Best way to do this i think is to pull all available variables form the xlr context and match the dictonary's values against them :param args: :param kwargs: :return: """ # pull in the xlrelease apis self.__releaseApi = XLReleaseServiceHolder.getReleaseApi() self.__repositoryService = XLReleaseServiceHolder.getRepositoryService( ) self.__taskApi = XLReleaseServiceHolder.getTaskApi() self.__phaseApi = XLReleaseServiceHolder.getPhaseApi() self.__variable_start_regex = re.compile('\$\{', re.IGNORECASE) # TODO: replace with {} once testing is done self.__variable_start_string = "$<" self.__variable_end_string = ">" self.store = dict() if kwargs.has_key('url'): kwargs = self.load_from_url(kwargs['url']) elif kwargs.has_key('repoId'): kwargs = self.load_profile_from_xlr_repo(kwargs['repoId']) elif kwargs.has_key('repoString'): kwargs = json.loads(str(kwargs['repoString'])) self.update(dict(*args, **kwargs)) # use the free update to set keys
def do_POST(self): if 'content-length' in self.headers.dict: length = int(self.headers.dict['content-length']) else: logger.warn('content length required') self.send_error(400, 'content length required for post') return if 'content-type' not in self.headers.dict or self.headers.dict['content-type'] != 'text/json': logger.warn('content type missing or non-json') body = self.rfile.read(length) try: logger.debug('received: [%s]' % body) data_in = json.loads(body) except: logger.warn('content does not parse') self.send_error(400, 'content does not parse as valid json') return try: data_out = handle_request(data_in, self.server) reply = json.dumps(data_out) except TouchFormsBadRequest, e: self.send_error(400, str(e)) return
def get_change_request(self, table_name,number,fields): servicenow_api_url = '/api/now/v1/table/%s?number=%s&sysparm_fields=%s' % (table_name,number,",".join(fields)) response = self.httpRequest.get(servicenow_api_url, contentType = 'application/json') if response.getStatus() == SN_RESULT_STATUS: data = json.loads(response.getResponse()) if len(data['result']) == 1: return data['result'][0] self.throw_error(response)
def body_handler(body): if resp.status_code == 200: data = json.loads(body.to_string()) print data def bla(reply): print dir(reply), reply.message EventBus.send("auth.github.login", data, bla) else: print resp.status_code, body.to_string()
def get_boards(self, board_name): if not board_name: error("No board name provided.") request = self._createRequest() response = request.get("/rest/agile/1.0/board?name=%s" % board_name, contentType="application/json") if response.status != 200: error(u"Unable to find boards for {0}".format(board_name), response) return Json.loads(response.response)['values']
def update_ci_property(self, ci_id, ci_property, property_value): if self.check_CI_exist(ci_id): ci = self.get_ci(ci_id, 'json') data = json.loads(ci) data[ci_property] = property_value self.update_ci(ci_id, json.dumps(data), 'json') else: raise Exception("Did not find ci with id [%s]" % ci_id)