I'm generating a URL (in string) that depends on 3 optional parameters, file, user and active.
From a base url: /hey I want to generate the endpoint, this means:
- If
fileis specificied, my desired output would is:/hey?file=example - If
fileanduseris specified, my desired output is:/hey?file=example&user=boo - If
userandactiveare specified, my desired output is:/hey?user=boo&active=1 - If no optional parameters are specified, my desired output is:
/hey - and so on with all the combinations...
My code, which is working correctly, is as follows (change the None's at the top if you want to test it):
file = None
user = None
active = 1
ep = "/hey"
isFirst = True
if file:
if isFirst:
ep+= "?file=" + file;
isFirst = False;
else: ep += "&file=" + file;
if user:
if isFirst:
ep+= "?user=" + user;
isFirst = False;
else: ep += "&user=" + user;
if active:
if isFirst:
ep+= "?active=" + str(active);
isFirst = False;
else: ep += "&active=" + str(active);
print ep
Can someone give me a more python implementation for this? I can't use modules as requests.
Thanks in advance.
;. makes python look ugly \$\endgroup\$