I have made a REST service in Tornado. I have tried a GET with JSON arguments and all works fine. But when I try with parameters in urls, I receive with postman a "socket hang up" error.
This is the url sent
http://127.0.0.1:8080/scenarios/asyncexec?project_name=LBP22&scenario_name=6a27351e-e51f-4349-89d8-a3e326a5bd12
and the handler for GET
def get(self):
# GET function for checking the status of execution
project_name = self.get_argument('project_name')
scenario_name = self.get_argument('scenario_name')
Loggers.access.info("Polling for exec status")
running = False
save_exec_element = None
for exec_element in strategy_lab_config.Scenarios.Execute.exec_list:
if exec_element[1] == project_name and \
exec_element[2] == scenario_name:
exec_future = exec_element[0]
if exec_future.running():
self._generate_output_json_from_dict({"execution_status": "RET_OK_PROCESSING"})
running = True
break
elif exec_future.done():
save_exec_element = exec_element
try:
output = exec_future.result()
scenario = {
'project_name': project_name,
'scenario_name': scenario_name,
"execution_status": 'RET_OK_DONE',
"output": output
}
self._generate_output_json_from_dict(scenario)
break
except Exception as exec_exc:
scenario = {
'project_name': project_name,
'scenario_name': scenario_name,
"execution_status": 'RET_ERR_FAIL',
"error_message": str(exec_exc),
"traceback": "".join(traceback.TracebackException.from_exception(exec_exc).format())
}
self._generate_output_json_from_dict(scenario)
break
else:
self._generate_output_json_from_dict({"execution_status": "RET_ERR_NOT_EXIST"})
return
Note that the previous version was with JSON and it all worked fine.
Here I have the handlers definitions
class Application(tornado.web.Application):
def __init__(self):
handlers = [
("/datasets/add", DatasetAdd),
("/projects/create", ProjectCreate),
("/projects/delete", ProjectDelete),
("/scenarios/execute", ScenarioExecute),
("/scenarios/asyncexec", AsyncScenarioExecute),
("/scenarios/tune", ScenarioTune),
("/scenarios/whatif", ScenarioWhatIfAnalysis)
]
tornado.web.Application.__init__(self, handlers, debug=True)
pass
There was a fatal error on the prepare() function of RequestHandler. So the server started correctly, but without receiving POST.
Related
I have a function that I am trying to test in querySomething.py:
class QuerySomething:
def retrieveIssues(self,token):
responses = []
if "customFields" in self._event:
if not self.custom_fields:
fields = []
else:
fields = self.custom_fields
else:
fields = []
for issueTypeKey, issueTypeValue in self.issueTypes.items():
print(issueTypeKey, ":", issueTypeValue)
query = self.getQuery(issueTypeValue, self.status, fields)
respons = httpClient.get_request(query, token)
responses.append(respons)
return responses
And the test file:
def mock_getQuery():
return "QUERY"
def mock_response(state):
if state=="unauth":
with open("src/tests/mockdata/unauthorized_api_response.json","r") as response_file:
unauth_error = response_file.read()
return json.dumps(unauth_error)
elif state=="auth":
with open("src/tests/mockdata/success_api_response.json","r") as response_file:
success_message = response_file.read()
return json.dumps(success_message)
return "No message"
class test_query(unittest.TestCase):
#mock.patch("querySomething.QuerySomething.getQuery", side_effect=mock_getQuery)
#mock.patch("httpClient.get_request", side_effect=mock_response)
def test_retreiveIssues_unauth_response(self,mock_get,QuerySomething):
self.assertEqual(QuerySomething.retrieveIssues("token"),mock_response("unauth"))
if __name__ == "__main__":
unittest.main()
I am trying to mock the httpClient.get_request so that it gets the JSON file instead of reaching out to the API. We want to test an unauthorized response and a success response which explains the mock_response function. However, when I run the test, I get the following:
AssertionError: <MagicMock name='getQuery.retri[36 chars]712'> != '"{\\n \\"errorMessages\\": [\\n [131 chars]\n}"'
which is somewhat correct, but we need just the text, not the object. I read that I need to call the function, but when I try to call the function it throws a ModuleNotFound or NotAPackage error. What do I need to do to mock the httpClient.get_request and return the JSON string in the retrieveIssues function?
Updated, I was able to pull the JSON from the other file, and then was able to mock the return value as follows:
QuerySomething.retrieveIssues.return_value=load_json("unauth")
where load_json("unauth") pulls from the JSON response file.
I am writing a script to get the Build related changes in azure devops and it expects argument which is added in the azure deployment file to run the script. That program works as expected, but I want to change it so that if the Build is triggered by PR it should show the PR source branch and PR target branch values and if the Build is triggered by the branch it should show the Source branch only.
Running script with arguments as below in deployment
script: python3 $(Python_file.secureFilePath) $(System.AccessToken) $(Build.Repository.Name) $(Build.DefinitionName) $(Build.BuildNumber) "$(Build.SourceBranchName)""$(System.PullRequest.SourceBranch)" "$(System.PullRequest.TargetBranch)"
My Previous code is some thing like this below
import
sys
pat = sys.argv[1]
repo_name = sys.argv[2]
pipeline_name = sys.argv[3]
build_number = sys.argv[4]
Source_Branch = sys.argv[5]
PR_Source_Branch = sys.argv[6]
PR_Target_Branch = sys.argv[7]
Am doing changes some thing like this below which is not working please let me know how it should be
import sys
Source_Branch =
{
def get_arg(Source_Branch):
try:
sys.argv[5]
except sysargvError:
return ''
else:
return sys.argv[5]
}
PR_Source_Branch =
{
def get_arg(PR_Source_Branch):
try:
sys.argv[6]
except sysargvError:
return ''
else:
return sys.argv[6]
}
PR_Target_Branch =
{
def get_arg(PR_Target_Branch):
try:
sys.argv[7]
except sysargvError:
return ''
else:
return sys.argv[7]
}
expected :
if PR is raised for source Branch to target Branch after merge it will trigger the Build then it should pick up
PR_Source_Branch = sys.argv[6]
PR_Target_Branch = sys.argv[7]
if the Build was run from Branch (Manually) then it should pick up as below because it will not have the source and target Branch's as it was not triggered by PR it will not have that values
Source_Branch = sys.argv[5]
Python "try" doesn't have an "else" clause. Also this is not the error you need to catch. Perhaps something like this:
Source_Branch = {
def get_arg(Source_Branch):
try:
return sys.argv[5]
except IndexError:
return ''
}
I am working on creating custom image in IBM Cloud using python. I have a very simple straight code for just creating the image and it fails.
As per me I am passing the relevant correct details for all the parameters.
Still I get an Error which is not much descriptive :
ERROR:root:Please check whether the resource you are requesting exists.
Traceback (most recent call last):
File "/Users/deepali.mittal/GITHUB/dcoa/python/build/dmittal/virtual-env36/lib/python3.6/site-packages/ibm_cloud_sdk_core/base_service.py", line 246, in send
response.status_code, http_response=response)
ibm_cloud_sdk_core.api_exception.ApiException: Error: Please check whether the resource you are requesting exists., Code: 400
Process finished with exit code 0
This is not related to the resource missing in COS. As if it is not able to find the image in COS it gives a different error.
Code :
from ibm_vpc import VpcV1 as vpc_client
from ibm_cloud_sdk_core import ApiException
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
from boto3 import client as boto3_client
import logging
#logging.basicConfig(level=logging.DEBUG)
SOURCE_OBJECT_PATH = 'cos://us-south/de-images-dmittal/abc.qcow2'
RESOURCE_GROUP_ID = '1234'
OPERATING_SYSTEM = 'ubuntu-16-amd64'
def create_ssm_client():
ssm_client = boto3_client("ssm", region_name="us-west-2")
return ssm_client
def retrieve_ibm_config(ssm_client):
params = ["/ibm/service-key"]
response = ssm_client.get_parameters(Names=params, WithDecryption=True)
try:
api_key = response["Parameters"][0]["Value"]
except (ValueError, IndexError):
raise RuntimeError(
f"Required SSM parameters not retrieved. "
f'Required parameters are: {params}.'
)
return api_key
def create_authenticator(api_key):
authenticator = IAMAuthenticator(api_key)
return authenticator
def create_ibm_client(authenticator):
ibm_client = vpc_client('2021-05-28', authenticator=authenticator)
return ibm_client
def create_image_prototype():
image_file_prototype_model = {'href': SOURCE_OBJECT_PATH}
operating_system_identity_model = {'name': OPERATING_SYSTEM}
resource_group_identity_model = {'id': RESOURCE_GROUP_ID}
image_prototype_model = {
'name': 'my-image',
#'resource_group': resource_group_identity_model,
'file': image_file_prototype_model,
'operating_system': operating_system_identity_model
}
image_prototype = image_prototype_model
return image_prototype
def create_image():
ssm_client = create_ssm_client()
api_key = retrieve_ibm_config(ssm_client)
authenticator = create_authenticator(api_key)
ibm_client = create_ibm_client(authenticator)
image_prototype = create_image_prototype()
try:
#images = ibm_client.list_images()
#print(vpc)
#ibm_client.set_service_url('https://us-south.iaas.cloud.ibm.com/v1')
response = ibm_client.create_image(image_prototype)
print(response)
except ApiException as e:
print("Failed")
if __name__ == "__main__":
create_image()
Issue was with IAM Permission. After fixing it worked, the error shown was not relevant so it took time to figure out
I try to use Google Calendar API
events_result = service.events().list(calendarId=calendarId,
timeMax=now,
alwaysIncludeEmail=True,
maxResults=100, singleEvents=True,
orderBy='startTime').execute()
Everything is ok, when I have permission to access the calendarId, but it will be errors if wrong when I don't have calendarId permission.
I build an autoload.py function with schedule python to load events every 10 mins, this function will be stopped if error come, and I have to use SSH terminal to restart autoload.py manually
So i want to know:
How can I get status_code, example, if it is 404, python will PASS
Answer:
You can use a try/except block within a loop to go through all your calendars, and skip over accesses which throw an error.
Code Example:
To get the error code, make sure to import json:
import json
and then you can get the error code out of the Exception:
calendarIds = ["calendar ID 1", "calendar ID 2", "calendar Id 3", "etc"]
for i in calendarIds:
try:
events_result = service.events().list(calendarId=i,
timeMax=now,
alwaysIncludeEmail=True,
maxResults=100, singleEvents=True,
orderBy='startTime').execute()
except Exception as e:
print(json.loads(e.content)['error']['code'])
continue
Further Reading:
Python Try Except - w3schools
Python For Loops - w3schools
Thanks to #Rafa Guillermo, I uploaded the full code to the autoload.py program, but I also wanted to know, how to get response json or status_code for request Google API.
The solution:
try:
code here
except Exception as e:
continue
import schedule
import time
from datetime import datetime
import dir
import sqlite3
from project.function import cmsCalendar as cal
db_file = str(dir.dir) + '/admin.sqlite'
def get_list_shop_from_db(db_file):
cur = sqlite3.connect(db_file).cursor()
query = cur.execute('SELECT * FROM Shop')
colname = [ d[0] for d in query.description ]
result_list = [ dict(zip(colname, r)) for r in query.fetchall() ]
cur.close()
cur.connection.close()
return result_list
def auto_load_google_database(list_shop, calendarError=False):
shopId = 0
for shop in list_shop:
try:
shopId = shopId+1
print("dang ghi vao shop", shopId)
service = cal.service_build()
shop_step_time_db = list_shop[shopId]['shop_step_time']
shop_duration_db = list_shop[shopId]['shop_duration']
slot_available = list_shop[shopId]['shop_slots']
slot_available = int(slot_available)
workers = list_shop[shopId]['shop_workers']
workers = int(workers)
calendarId = list_shop[shopId]['shop_calendarId']
if slot_available > workers:
a = workers
else:
a = slot_available
if shop_duration_db == None:
shop_duration_db = '30'
if shop_step_time_db == None:
shop_step_time_db = '15'
shop_duration = int(shop_duration_db)
shop_step_time = int(shop_step_time_db)
shop_start_time = list_shop[shopId]['shop_start_time']
shop_start_time = datetime.strptime(shop_start_time, "%H:%M:%S.%f").time()
shop_end_time = list_shop[shopId]['shop_end_time']
shop_end_time = datetime.strptime(shop_end_time, "%H:%M:%S.%f").time()
# nang luc moi khung gio lay ra tu file Json WorkShop.js
booking_status = cal.auto_load_listtimes(service, shopId, calendarId, shop_step_time, shop_duration, a,
shop_start_time,
shop_end_time)
except Exception as e:
continue
def main():
list_shop = get_list_shop_from_db(db_file)
auto_load_google_database(list_shop)
if __name__ == '__main__':
main()
schedule.every(5).minutes.do(main)
while True:
# Checks whether a scheduled task
# is pending to run or not
schedule.run_pending()
time.sleep(1)
(A same question is available in Stackoverflow. But that didn't help me because it used other function)
API Documentation
Hello, I am trying to implement Opensubtitle API with Python. I prefer trying to search subtitle file with hash, because it's accurate.
As I have never used xmlrpc before and quite new to using APIs, I had to study to make it work. But I am stuck at the final point. My program is returning Status 200 (OK), but the 'data' array is blank. I think, I am doing something wrong with the paramater passing thing. The code is here:
from xmlrpclib import ServerProxy
import hashCheck, os
server = 'http://api.opensubtitles.org/xml-rpc'
class MainEngine(object):
def __init__(self, language="None"):
self.rpc = ServerProxy(server, allow_none=True)
user_agent = 'OSTestUserAgentTemp'
self.Start()
def getToken(self):
self.logindata = self.rpc.LogIn(None, None, "en", "OSTestUserAgentTemp")
self.Token = self.logindata["token"]
return self.Token
def subSearch(self, path):
self.hash = hashCheck.hash(self.path)
token = self.getToken()
self.param = [
token, # token
[
'eng', # sublanguageid
self.hash, #hash
os.path.getsize(path), # byte size
]
]
Obj = self.rpc.SearchSubtitles(token, self.param)
print Obj
def Start(self):
# print server
self.path = "E:\Movies\English\Captain Phillips\Captain Phillips.mp4"
self.subSearch(self.path)
def main():
MainEngine()
if __name__ == '__main__':
main()