0

Urls are defined in a single variable and will be used in get request as per the user put, please find code below...

d = {'Mango': 'http://12.345.67.891:8000/api/datasources/proxy/1/query?db=UK_GHS&q=SELECT%20sum(%22count%22)%20FROM%20%22gatling%22%20WHERE%20%22status%22%20%3D%20%27ok%27%20AND%20%22simulation%22%20%3D~%20%2Fsoak-test*%2F%20AND%20time%20%3E%201544491800000ms%20and%20time%20%3C%201544495400000ms%20GROUP%20BY%20%22script%22&epoch=ms',
 'Banana':'http://12.345.67.891:8000/api/datasources/proxy/1/query?db=UK_GHS&q=SELECT%20sum(%22count%22)%20FROM%20%22gatling%22%20WHERE%20%22status%22%20%3D%20%27ok%27%20AND%20%22simulation%22%20%3D~%20%2Fspike-test*%2F%20AND%20time%20%3E%201544491800000ms%20and%20time%20%3C%201544495400000ms%20GROUP%20BY%20%22script%22&epoch=ms',
 'Apple':'http://12.345.67.891:8000/api/datasources/proxy/1/query?db=UK_GHS&q=SELECT%20sum(%22count%22)%20FROM%20%22gatling%22%20WHERE%20%22status%22%20%3D%20%27ok%27%20AND%20%22simulation%22%20%3D~%20%2Fload-test*%2F%20AND%20time%20%3E%201544491800000ms%20and%20time%20%3C%201544495400000ms%20GROUP%20BY%20%22script%22&epoch=ms'}

Dashboard_name = raw_input("Enter dashboard name from the above list :")

if the user enters Mango get request will take 1st URL and if user enters banana get request will take 2nd url and so on

user_input_from = raw_input("Enter from date and time in YYYY-MM-DD HH:MM:SS format :")
user_input_to = raw_input("Enter to date and time in YYYY-MM-DD HH:MM:SS format :")

def user_input_to_epoch_time(user_input):
    return int(time.mktime(time.strptime(user_input, '%Y-%m-%d %H:%M:%S')))

final_url = d.format(user_input_to_epoch_time(user_input_from),
                                  user_input_to_epoch_time(user_input_to))
print(final_url)
results = d.get(Dashboard_name, 'No value in list')
print results

I implemented the code suggested by you, but getting below error as URL is not direct and it is taking as per the user input.

    final_url = d.format(user_input_to_epoch_time(user_input_from),
AttributeError: 'dict' object has no attribute 'format'
SG131712
  • 135
  • 3
  • 13
  • you can have a look at [How to change values of url query in python?](https://stackoverflow.com/questions/43607870/how-to-change-values-of-url-query-in-python) – anky Feb 16 '19 at 11:29
  • Possible duplicate of [How to change values of url query in python?](https://stackoverflow.com/questions/43607870/how-to-change-values-of-url-query-in-python) – Constantin Guidon Feb 16 '19 at 12:04

1 Answers1

1

Code below:

import time

SHORT_URL_TEMPLATE_MANGO = 'http://12.345.67.891:8000/api/mango?from={}&to={}'
SHORT_URL_TEMPLATE_APPLE = 'http://12.345.67.891:8000/api/apple?from={}&to={}'

URLS = {'mango': SHORT_URL_TEMPLATE_MANGO, 'apple': 
        SHORT_URL_TEMPLATE_APPLE}


def user_input_to_epoch_time(user_input):
    return int(time.mktime(time.strptime(user_input, '%Y-%m-%d %H:%M:%S')))


dash_board = 'mango'
user_input_from = "2018-10-12 23:12:44"
user_input_to = "2018-10-12 23:17:55"

final_url = URLS[dash_board].format(user_input_to_epoch_time(user_input_from),
                                user_input_to_epoch_time(user_input_to))

print(final_url)  

Output:

http://12.345.67.891:8000/api/mango?from=1539375164&to=1539375475
balderman
  • 22,927
  • 7
  • 34
  • 52
  • this doesnot replace the query in the url – anky Feb 16 '19 at 11:34
  • Above code help me in convert human time to unix time but how to store taht unix time in a variable and replace that in the get request URL? – SG131712 Feb 16 '19 at 11:52
  • Code was modified and shows how to replace time fields in url – balderman Feb 16 '19 at 12:02
  • Thanks it worked fine. But in my code URL is defined in different way. I edited the code with more details can you please have a look. – SG131712 Feb 16 '19 at 17:39
  • I am not sure I follow.. All you need to do (as far as I understand) is to modify SHORT_URL_TEMPLATE according to your needs. Is there anything else left? Please explain – balderman Feb 16 '19 at 17:45
  • Yes you are right i need to modify only SHORT_URL_TEMPLATE. I did it and updated the complete code in the question. Can you please have look on code and suggest me how i can resolve that error? – SG131712 Feb 16 '19 at 18:00
  • d is a dict. You need to use d[‘dashboard ’]. This will point to the url template – balderman Feb 16 '19 at 18:07
  • Code was modified in order to support the dashboard type. – balderman Feb 16 '19 at 18:23