rtkit

Python Api for Request Tracker's REST interface

View the Project on GitHub z4r/python-rtkit

Request Tracker REST Interface

travis

coveralls

Request Tracker data access python module for REST interface.

Installation

Using pip stable:

$ pip install python-rtkit

Using pip dev:

$ pip install git+https://github.com/z4r/python-rtkit

RT REST API Summary

More detailed version: Request Tracker Wiki

01 W Create ticket ticket/new
02 RW Read/Update ticket ticket/<ticket-id>
03 W Create ticket comment ticket/<ticket-id>/comment
04 RW Read/Update ticket links ticket/<ticket-id>/links
05 R Read ticket attachments ticket/<ticket-id>/attachments
06 R Read ticket attachment ticket/<ticket-id>/attachments/<attachment-id>
07 R Read ticket attachment content ticket/<ticket-id>/attachments/<attachment-id>/content
08 R Read ticket history ticket/<ticket-id>/history
09 R Read detailed ticket history ticket/<ticket-id>/history?format=l
10 R Read ticket history item ticket/<ticket-id>/history/id/<history-id>
11 R Read user by id user/<user-id>
12 R Read user by name user/<user-Name>
13 R Read queue by id queue/<queue-id>
14 R Read queue by name queue/<queue-Name>
15 R Search tickets search/ticket?query=<q>&orderby=<o>&format=<f>

Authentication Handlers

Basic Authentication

from rtkit.resource import RTResource
from rtkit.authenticators import BasicAuthenticator

url = 'http://<HOST>/REST/1.0/'
resource = RTResource(url, '<USER>', '<PWD>', BasicAuthenticator)

Cookie-based Authentication

from rtkit.resource import RTResource
from rtkit.authenticators import CookieAuthenticator

url = 'http://<HOST>/REST/1.0/'
resource = RTResource(url, '<USER>', '<PWD>', CookieAuthenticator)

Kerberos Authentication

from rtkit.resource import RTResource
from rtkit.authenticators import KerberosAuthenticator

url = 'http://<HOST>/REST/1.0/'
resource = RTResource(url, None, None, KerberosAuthenticator)

Remeber to install urllib2_kerberos

Logging

from rtkit import set_logging
import logging
set_logging('debug')
logger = logging.getLogger('rtkit')

Create ticket

content = {
    'content': {
        'Queue': 1,
        'Subject' : 'New Ticket',
        'Text' : 'My useless\ntext on\nthree lines.',
    }
}
try:
    response = resource.post(path='ticket/new', payload=content,)
    logger.info(response.parsed)
except RTResourceError as e:
    logger.error(e.response.status_int)
    logger.error(e.response.status)
    logger.error(e.response.parsed)

Read a ticket

try:
    response = resource.get(path='ticket/1')
    for r in response.parsed:
        for t in r:
            logger.info(t)
except RTResourceError as e:
    logger.error(e.response.status_int)
    logger.error(e.response.status)
    logger.error(e.response.parsed)

Edit a ticket or ticket's links

Ticket (or ticket's links) editing hasn't all-or-nothing behaviour; so it's very difficult to capture errors. For example trying to change Queue to a not admitted one (or to edit an unknown field) RT will return:

RT/3.8.10 409 Syntax Error

# queue: You may not create requests in that queue.
# spam: Unknown field.

id:
Subject: Try Edit Ticket
TimeWorked: 1
Queue: 2
Spam: 10

For now rtkit will raise SyntaxError with the errors list in e.response.parsed

Comment on a Ticket with Attachments

Usually your requests will be something like this.

try:
    params = {
        'content' :{
            'Action' : 'comment',
            'Text' : 'Comment with attach',
            'Attachment' : 'x.txt, 140x105.jpg',
        },
        'attachment_1' : file('x.txt'),
        'attachment_2' : file('140x105.jpg'),
    }
    response = resource.post(path='ticket/16/comment', payload=params,)
    for r in response.parsed:
       for t in r:
           logger.info(t)
except RTResourceError as e:
    logger.error(e.response.status_int)
    logger.error(e.response.status)
    logger.error(e.response.parsed)

 License

This software is licensed under the Apache License 2.0. See the LICENSE file in the top distribution directory for the full license text.