ari.py 4.78 KB
Newer Older
1 2 3 4 5 6 7 8 9
import requests
import sqlite3

def ari_call(config, verbs, method, payload):
    '''
    Generate a call to the ARI. Config contains host, protocol, and API base.
    Return the result.json().
    '''
    try:
10
        base_url = config['protocol'] + '://' + config['server'] + '/' + config['api_base']
11 12 13 14
    except KeyError as e:
        raise e
    try:
        full_url = '/'.join([base_url, '/'.join(verbs)])
15
        if method.lower() == 'del':
16 17
            call = requests.delete
        else:
okhin's avatar
okhin committed
18
            call = getattr(requests, method.lower())
19
        payload['app'] = config['app']
20
        payload['api_key'] = config['key'] + ':' + config['password']
21 22
        result = call(full_url, data=payload)
        result.raise_for_status()
23 24 25 26 27 28
        if method.lower() == 'post':
            # We need to test if we have something in the object, to eventually return it
            try:
                return result.json()
            except:
                result = requests.get(full_url, data=payload)
29 30 31 32
        try:
            return result.json()
        except:
            return '{}'
33 34 35 36 37 38 39
    except:
        raise

class Bridge(object):
    '''
    This is a bridge object, we use it to manage various needed calls
    '''
40
    def __init__(self, config, name, mode='mixing'):
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
        self.name = name
        self.config = config
        self.mode = mode

    def status(self):
        '''
        Get the status of a specific bridge
        '''
        try:
            return ari_call(self.config, ['bridges', self.name], 'GET', {})
        except:
            raise

    def create(self):
        '''
        Create a bridge.
        '''
        try:
            return ari_call(self.config, ['bridges', self.name], 'POST', {'mode': self.mode})
        except:
            raise

    def startMoh(self, playback_class):
        '''
        Let's start a playback on this bridge
        '''
        try:
            return ari_call(self.config, ['bridges', self.name, 'moh'], 'POST', {'mohClass': playback_class})
        except:
            raise

    def addChannel(self, channel):
        '''
        We want to add a channel to the bridge
        '''
        try:
            return ari_call(self.config, ['bridges', self.name, 'addChannel'], 'POST', {'channel': channel})
        except:
            raise
80

81 82 83 84 85 86 87 88 89
    def removeChannel(self, channel):
        '''
        We want to remove a channel from the bridge
        '''
        try:
            return ari_call(self.config, ['bridges', self.name, 'removeChannel'], 'POST', {'channel': channel})
        except:
            raise

okhin's avatar
okhin committed
90 91 92 93 94 95 96 97 98
    def delete(self):
        '''
        Let's delete the bridge
        '''
        try:
            return ari_call(self.config, ['bridges', self.name], 'DEL', {})
        except:
            raise

99 100 101 102 103 104
class Channel(object):
    '''
    This class is used to regroup all the method to manage a Channel
    '''
    def __init__(self, config, name):
        self.config = config
okhin's avatar
okhin committed
105
        self.name = name
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

    def status(self):
        '''
        Get the status of a channel
        '''
        try:
            return ari_call(self.config, ['channels', self.name], 'GET', {})
        except:
            raise

    def originate(self, endpoint):
        '''
        Originate a call to endpoint
        '''
        try:
            return ari_call(self.config, ['channels', self.name], 'POST', {'endpoint': endpoint})
        except:
            raise

    def playback(self, playback_id, media, lang='en_US'):
        '''
        Starts a playback on a Channels
        '''
        try:
            return ari_call(self.config, ['channels', self.name, 'play', playback_id], 'POST', {'media': media, 'lang': lang})
        except:
            raise
133 134 135 136 137 138 139 140 141 142

    def delete(self):
        '''
        Delete the channel (hangup)
        '''
        try:
            return ari_call(self.config, ['channels', self.name], 'DEL', {})
        except:
            raise

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    def startMoh(self, moh_class):
        '''
        Start playing music on hold on the channel
        '''
        try:
            return ari_call(self.config, ['channels', self.name, 'moh'], 'POST', {'mohClass': moh_class})
        except:
            raise

    def stopMoh(self):
        '''
        Stop playing moh on the channel
        '''
        try:
            return ari_call(self.config, ['channels', self.name, 'moh'], 'DEL', {})
        except:
            raise

161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
class Playback(object):
    '''
    This object is used to manage ARI calls for playbacks.
    '''
    def __init__(self, config, name, media, lang='en_US'):
        self.config = config
        self.name = name
        self.media = media
        self.lang = lang

    def stop(self):
        '''
        Let's stop a specific playback
        '''
        try:
176
             ari_call(self.config, ['playbacks', self.name], 'DEL', {})
177 178
        except:
            raise