#!/usr/bin/python3

import json
import os
import urllib.request

# Config and secrets
exapi_update = "https://the.url/exapi/update"
debug           = False

try:
    testentry="""Hi, this is a test entry.
This is the second line.

There is a blank line above."""
    ticketnum="1234567"
    payload = { "auth_token": "EXAMPLEAUTHTOKEN",
                "operation": "addentry",
                "number": ticketnum,
                "comment": testentry }
    jsonpayload = json.dumps(payload)
    req = urllib.request.Request(exapi_update)
    req.add_header('Content-Type', 'application/json; charset=utf-8')
    jsonencoded = jsonpayload.encode('utf-8')
    req.add_header('Content-Length', len(jsonencoded))
    response = urllib.request.urlopen(req, jsonencoded).read()
    responsejson = json.loads(response.decode('utf-8'))
    print("Got response " + repr(responsejson))
except Exception as ex:
    print("Well, something went wrong there.  How traumatic.")
    print(repr(ex))
