fuel-qa/fuelweb_test/testrail/testrail.py
Alexandr Kostrikov e317c2d6d2 doc: Added an initial Sphinx documentation
I have added a auto-generated documentation. It is done with
Sphinx library. Sphinx uses reStructuredText as its markup language
and docstrings as a source for automated documentation.

According to https://www.python.org/dev/peps/pep-0257/
I used a """ for quotes in docstrings and made
"""Docstring."""-looking one-liners and correct indentation
for multiple-liners.
Based on https://github.com/kennethreitz/requests
I moved Makefile, requirements.txt and conf.py to /doc folder.
Also I decided to make fuel-qa documentation
similar to https://docs.python.org/2/tutorial/index.html
with single table of contents.
I have added empty __init__.py files to be able to produce
documentation.
I have added sys_test.log to .gitigonre because it has
been generated at the documentation build.

To make it faster and non-conflicting I added some banal
class descriptions in order to get them in auto-generated documentation.
I decided not to fight with warnings and poor docstring descriptions
I added to make documentation less conflicting and for a faster review.
Docstring improvements and detailed code description are left for later.
For the same reasons I decied not to add docstrings to all methods.
At a places where I made poor docstrings I have added a
TODO documentation.

Change-Id: I7701a9a3429abbf62b13ec5a31972298c0be5201
Related-Bug: #1321682
2015-05-07 17:42:38 +03:00

106 lines
3.1 KiB
Python

# Copyright 2015 Mirantis, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# TestRail API binding for Python 2.x (API v2, available since
# TestRail 3.0)
#
# Learn more:
#
# http://docs.gurock.com/testrail-api2/start
# http://docs.gurock.com/testrail-api2/accessing
#
# Copyright Gurock Software GmbH. See license.md for details.
#
import base64
import json
import urllib2
class APIClient:
"""APIClient.""" # TODO documentation
def __init__(self, base_url):
self.user = ''
self.password = ''
if not base_url.endswith('/'):
base_url += '/'
self.__url = base_url + 'index.php?/api/v2/'
#
# Send Get
#
# Issues a GET request (read) against the API and returns the result
# (as Python dict).
#
# Arguments:
#
# uri The API method to call including parameters
# (e.g. get_case/1)
#
def send_get(self, uri):
return self.__send_request('GET', uri, None)
#
# Send POST
#
# Issues a POST request (write) against the API and returns the result
# (as Python dict).
#
# Arguments:
#
# uri The API method to call including parameters
# (e.g. add_case/1)
# data The data to submit as part of the request (as
# Python dict, strings must be UTF-8 encoded)
#
def send_post(self, uri, data):
return self.__send_request('POST', uri, data)
def __send_request(self, method, uri, data):
url = self.__url + uri
request = urllib2.Request(url)
if method == 'POST':
request.add_data(json.dumps(data))
auth = base64.encodestring(
'%s:%s' % (self.user, self.password)).strip()
request.add_header('Authorization', 'Basic %s' % auth)
request.add_header('Content-Type', 'application/json')
e = None
try:
response = urllib2.urlopen(request).read()
except urllib2.HTTPError as e:
response = e.read()
if response:
result = json.loads(response)
else:
result = {}
if e is not None:
if result and 'error' in result:
error = '"' + result['error'] + '"'
else:
error = 'No additional error message received'
raise APIError('TestRail API returned HTTP %s (%s)' %
(e.code, error))
return result
class APIError(Exception):
"""APIError.""" # TODO documentation
pass