StatusCakeDev / statuscake-py

StatusCake Python SDK (Alpha)
https://developers.statuscake.com
MIT License
1 stars 3 forks source link

Improper serializing alert_at for SslApi - Alert At Invalid #13

Open vgrebenschikov opened 1 year ago

vgrebenschikov commented 1 year ago

Describe the bug Quite obvious code from example does not work:

SslApi(api_client=client).create_ssl_test(website_url='https://example.com', check_rate=86400, alert_at=[1, 2, 3])

To Reproduce Script:

import os
from statuscake import ApiClient
from statuscake.apis import ( SslApi )

client = ApiClient(
   header_name='Authorization',
   header_value='Bearer %s' % os.environ['API_TOKEN'],
)

SslApi(api_client=client).create_ssl_test(website_url='https://example.com', check_rate=86400, alert_at=[1, 2, 3])

Shows an error:

Traceback (most recent call last):
  File "sc.py", line 11, in <module>
    SslApi(api_client=client).create_ssl_test(website_url='https://example.com', check_rate=86400, alert_at=[1, 2, 3])
  File "/Library/Python/3.10/lib/python/site-packages/statuscake/api/ssl_api.py", line 540, in create_ssl_test
...

statuscake.exceptions.ApiException: (400)
Reason: Bad Request
HTTP response body: {"message":"The provided parameters are invalid. Check the errors output for detailed information.","errors":{"alert_at":["Alert At Invalid","Alert At requires exactly 3 unique, positive integer values"]}}

Expected behavior SSL test should be created, exactly same values works fine with curl:

$ curl -X POST https://api.statuscake.com/v1/ssl -H "Authorization: Bearer ${API_TOKEN}" -d "website_url=https://www.example.com&check_rate=86400&alert_at[]=1&alert_at[]=2&alert_at[]=3"

{"data":{"new_id":"308943"}}

Desktop (please complete the following information):

Additional context If turn on debug, it is seen that list of alert_at sent to server is improperly serialized (different from curl):

send: b'POST /v1/ssl HTTP/1.1\r\nHost: api.statuscake.com\r\nAccept-Encoding: identity\r\nContent-Length: 73\r\nContent-Type: application/x-www-form-urlencoded\r\nAccept: application/json\r\nAuthorization: Bearer...\r\nUser-Agent: statuscake/python\r\n\r\n'
send: b'website_url=https%3A%2F%2Fexample.com&check_rate=86400&alert_at=1%2C2%2C3'
reply: 'HTTP/1.1 400 Bad Request\r\n'

it first converted to string "1,2,3" then url-encoded to "alert_at=1%2C2%2C3" and then sent instead of sending as &alert_at[]=1&alert_at[]=2&alert_at[]=3

vgrebenschikov commented 1 year ago

as a w/a:

% git diff
diff --git a/statuscake/api/ssl_api.py b/statuscake/api/ssl_api.py
index 201917f..40f8837 100644
--- a/statuscake/api/ssl_api.py
+++ b/statuscake/api/ssl_api.py
@@ -155,7 +155,7 @@ class SslApi(object):
                     'user_agent': 'form',
                 },
                 'collection_format_map': {
-                    'alert_at': 'csv',
+                    'alert_at': 'multi',
                     'contact_groups': 'csv',
                 }
             },
diff --git a/statuscake/api_client.py b/statuscake/api_client.py
index 6261ed2..cdaf46d 100644
--- a/statuscake/api_client.py
+++ b/statuscake/api_client.py
@@ -525,7 +525,7 @@ class ApiClient(object):
             if k in collection_formats:
                 collection_format = collection_formats[k]
                 if collection_format == 'multi':
-                    new_params.extend((k, value) for value in v)
+                    new_params.extend((k + '[]', value) for value in v)
                 else:
                     if collection_format == 'ssv':
                         delimiter = ' '