Using Collaborator JSON APIs from Python
I have a very simple Python 3.5 script to try and replicate a ServerInfoService.getVersion command that works in the JSON Testing page for Collaborator.
Extracts of the key bits of my script below:
# Server URL
url = "http://myserver.com/services/json/v1"
# Simple JSON API command to try
# [{"command" : "ServerInfoService.getVersion"}] # This is the command that works OK in the Collab JSON API webpage
val_get_version = {"command" : "ServerInfoService.getVersion"}
# The urllib.parse.urlencode() function takes a mapping or sequence of 2-tuples and returns a string in this format.
data = urllib.parse.urlencode(val_get_version)
# Add the [ and ] array delimiters that the JSON seems to require to the string... Do I need to do this ???
#cmd_data = data
cmd_data = "[" + data + "]"
# Encoded to bytes before being used as the data parameter in the request.
cmd_data = cmd_data.encode('utf-8')
# Make the request
req = urllib.request.Request(url, cmd_data)
response = urllib.request.urlopen(req)
When I run this:
The request is going to the server OK but no matter what I try for the request format I get the Response:
b'[{"errors":[{"code":"JsonMappingException","message":"No content to map due to end-of-input\\n at [Source: ; line: 1, column: 0]"}]}]'
It seems my formatting of the request is not recognised but I am stumped as to what I need to change/add to make this work.
I'm sadly not a Python expert but the basics I am using here I do already have working with other tool that support their own JSON APIs.
As an aside I am going this route as I want my reports to include the Subversion revision of the files in a Review. It seems the standard reports nor the custom report nor the admin-xml can give me this (unless anyone can enlighten me otherwise).
Grateful for any hints or suggestions as I've disappointingly ground to a halt at the first hurdle!
Thanks