0

I'm trying to use requests to pass a number of conditions to the Connectwise REST api. I'm trying to generate a URL like below

/company/configurations/?pageSize=1000&conditions=id=83500 and type/name="Printer" and name="TEST API PRINTER"

but I only seem to be able to generate this:

/company/configurations/?pageSize=1000&conditions=id&conditions=type/name&conditions=name

My payload looks like this:

parameters = {}
    parameters['conditions'] = {}
    parameters['pageSize'] = 1000
    if db_rid:
        parameters['conditions']['id'] = 83500
    if type_name:
        parameters['conditions']['type/name'] = "Printer"
    if name:
        parameters['conditions']['name'] = "TEST API PRINTER"
requests.get(APIurl, params=parameters)

Where am I going wrong?

3
  • That's because the value of "conditions" in the original url is one long string, id=83500 and type/name="Printer" and name="TEST API PRINTER". It's not some sort of magic nested parameters. You just need to set the conditions parameter equal to that string. Commented Oct 27, 2016 at 13:17
  • use string parameters['conditions'] = 'id=83500 and ...' Commented Oct 27, 2016 at 13:18
  • I thought that may be the case, I've opted for building a function to build the string for me which I'll put in the answers below. Thanks for the clarity! Commented Oct 28, 2016 at 12:05

2 Answers 2

2

You could give this a try, if you are open to using urllib.urlencode -

conditions = {"id": 83500, "type/name": "Printer", "name": "TEST API PRINTER"} 
query = {"pageSize": 1000, "conditions": conditions}
params = urllib.urlencode(query)
final_url = str(APIurl) + "&" + str(params)
response = requests.get(url=final_url)
Sign up to request clarification or add additional context in comments.

Comments

0

I created a function to generate the conditions string for this.

    def _add_condition(self, string, add_string, add_value):
    if string == '':
        if type(add_value) is not str:
            result = '{}={}'.format(add_string, add_value)
        else:
            result = '{}="{}"'.format(add_string, add_value)
    else:
        if type(add_value) is not str:
            result = '{} and {}={}'.format(string, add_string, add_value)
        else:
            result = '{} and {}="{}"'.format(string, add_string, add_value)
    print(result)
    return result

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.