pysnow icon indicating copy to clipboard operation
pysnow copied to clipboard

Problem in calling Service Now with custom API Name

Open rlthompson-godaddy opened this issue 2 years ago • 1 comments

I am trying to make a query to a custom API name in our servicenow instance like the following:

https://$COMPANY.service-now.com/api/now/on_call_rota/current?group_name=$GROUPNAME

However, am getting back empty responses or exceptions. I've tried the following methods:

import pysnow

snow = pysnow.client.Client(instance="$COMPANY", user="$USER", password="$PASSWORD")
oncall = snow.resource(api_path="/on_call_rota/current")
response = oncall.get(query={"group_name": "$GROUPNAME"})
response.all()

Which results in a 200, but the result is empty.

import pysnow

snow = pysnow.client.Client(instance="$COMPANY", user="$USER", password="$PASSWORD")
oncall = snow.resource(api_path="/on_call_rota/current")
oncall.request("GET", path_append="?group_name=$GROUPNAME"

which results in an exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/$USER/projects/.venv/lib/python3.8/site-packages/pysnow/resource.py", line 162, in request
    return self._request.custom(
  File "/home/$USER/projects/.venv/lib/python3.8/site-packages/pysnow/request.py", line 167, in custom
    self._url = self._get_custom_endpoint(path_append)
  File "/home/$USER/projects/.venv/lib/python3.8/site-packages/pysnow/request.py", line 83, in _get_custom_endpoint
    return self._url_builder.get_appended_custom(segment)
  File "/home/$USER/projects/.venv/lib/python3.8/site-packages/pysnow/url_builder.py", line 59, in get_appended_custom
    self.validate_path(path_component)
  File "/home/$USER/projects/.venv/lib/python3.8/site-packages/pysnow/url_builder.py", line 31, in validate_path
    raise InvalidUsage(
pysnow.exceptions.InvalidUsage: Path validation failed - Expected: '/<component>[/component], got: /?group_name=$GROUPNAME

How can I do this query?

rlthompson-godaddy avatar Sep 30 '22 05:09 rlthompson-godaddy

Ok, I might have figured out my own question. This seems to work:

import pysnow

snow = pysnow.client.Client(instance="$COMPANY", user="$USER", password="$PASSWORD")
response = snow.session.get(f"{snow.base_url}/api/now/on_call_rota/current?group_name=$GROUPNAME")
response.json()

However, my question then becomes is the best way to handle this?

rlthompson-godaddy avatar Sep 30 '22 05:09 rlthompson-godaddy