cat_helper.py 2.33 KB
Newer Older
Leonidas Poulopoulos's avatar
Leonidas Poulopoulos committed
1 2
import requests
from lxml import objectify
3

Leonidas Poulopoulos's avatar
Leonidas Poulopoulos committed
4 5

class CatQuery(object):
6

Leonidas Poulopoulos's avatar
Leonidas Poulopoulos committed
7 8 9 10 11
    def __init__(self, cat_key, cat_url):
        self.key = cat_key
        self.url = cat_url
        self.status = None
        self.response = None
12

Leonidas Poulopoulos's avatar
Leonidas Poulopoulos committed
13 14 15 16
    def post_request(self, kwargs):
        kwargs['APIKEY'] = self.key
        r = requests.post(self.url, data=kwargs)
        return r.content
17

Leonidas Poulopoulos's avatar
Leonidas Poulopoulos committed
18 19 20 21
    def curate_response(self, response):
        response = response.split('<CAT-API-Response>')[1]
        response = "<?xml version='1.0' ?><CAT-API-Response>"+response
        return response
22

Leonidas Poulopoulos's avatar
Leonidas Poulopoulos committed
23 24 25 26
    def newinst(self, kwargs):
        self.status = None
        self.response = None
        kwargs['ACTION'] = 'NEWINST'
27
        if 'NEWINST_PRIMARYADMIN' not in kwargs.keys():
Leonidas Poulopoulos's avatar
Leonidas Poulopoulos committed
28 29 30 31 32 33 34
            raise Exception('NEWINST_PRIMARYADMIN parameter is missing')
        response = self.post_request(kwargs)
        r = objectify.fromstring(response)
        try:
            assert(r.success)
            # Successfull response
            self.status = 'Success'
35 36 37 38
            self.response = {
                "inst_unique_id": r.success.inst_unique_id,
                "enrollment_URL": r.success.enrollment_URL
            }
Leonidas Poulopoulos's avatar
Leonidas Poulopoulos committed
39 40 41 42 43
            return True
        except AttributeError:
            self.status = 'Error'
            self.response = r.error.description
            return False
44

Leonidas Poulopoulos's avatar
Leonidas Poulopoulos committed
45 46 47 48 49 50 51
    def admincount(self, kwargs):
        self.status = None
        self.response = None
        kwargs['ACTION'] = 'ADMINCOUNT'
        if not 'INST_IDENTIFIER' in kwargs.keys():
            raise Exception('INST_IDENTIFIER parameter is missing')
        response = self.post_request(kwargs)
52
        response = self.curate_response(response)
Leonidas Poulopoulos's avatar
Leonidas Poulopoulos committed
53 54 55 56 57 58 59 60 61 62 63
        r = objectify.fromstring(response)
        try:
            assert(r.success)
            # Successfull response
            self.status = 'Success'
            self.response = {"number_of_admins":r.success.number_of_admins}
            return True
        except AttributeError:
            self.status = 'Error'
            self.response = r.error.description
            return False
64

Leonidas Poulopoulos's avatar
Leonidas Poulopoulos committed
65 66 67 68 69 70 71 72 73 74 75
    def statistics(self):
        self.status = None
        self.response = None
        kwargs = {}
        kwargs['ACTION'] = 'STATISTICS'
        response = self.post_request(kwargs)
        response = self.curate_response(response)
        r = objectify.fromstring(response)
        return r


76 77 78