• Stars
    star
    151
  • Rank 246,057 (Top 5 %)
  • Language
    Python
  • License
    Other
  • Created about 11 years ago
  • Updated almost 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Library for accessing the Asterisk REST Interface

About

This package contains the Python client library for the Asterisk REST Interface. It builds upon the Swagger.py library, providing an improved, Asterisk-specific API over the API generated by Swagger.py

Usage

Install from source using the setup.py script.

$ sudo ./setup.py install

API

An ARI client can be created simply by the ari.connect method. This will create a client based on the Swagger API downloaded from Asterisk.

The API is modeled into the Repository Pattern, as you would find in Domain Driven Design. Each Swagger Resource (a.k.a. API declaration) is mapped into a Repository object, which is provided as a field on the client (client.channels, client.bridges).

Responses from Asterisk are mapped into first-class objects, akin to Domain Objects in the Repository Pattern. These are provided both on the responses to RESTful API calls, and for fields from events received over the WebSocket.

Making REST calls

Each Repository Object provides methods which invoke the non-instance specific operations of the associated Swagger resource (bridges.list(), channels.get()). Instance specific methods are also provided, which require identity parameters to be passed along (channels.get(channelId=id)).

Instance specific methods are also provided on the Domain Objects (some_channel.hangup()).

Registering event callbacks

Asterisk may send asyncronous messages over a WebSocket to indicate events of interest to the application.

The Client object has an on_event method, which can be used to subscribe for specific events from Asterisk.

The first-class objects also have 'on_event' methods, which can subscribe to Stasis events relating to that object.

Object lifetime

The Repository Objects exist for the lifetime of the client that owns them.

Domain Objects are ephemeral, and not tied to the lifetime of the underlying object in Asterisk. Pratically, this means that if you call channels.get('1234') several times, you may get a different object back every time.

You may hold onto an instance of a Domain Object, but you should consider it to be stale. The data contained in the object may be out of date, but the methods on the object should still behave properly.

If you invoke a method on a stale Domain Object that no longer exists in Asterisk, you will get a HTTPError exception (404 Not Found).

Caveats

The dynamic methods exposed by Repository and Domain objects are, effectively, remote procedure calls. The current implementation is synchronous, which means that if anything were to happen to slow responses (slow network, packet loss, system load, etc.), then the entire application could be affected.

Examples

import ari

client = ari.connect('http://localhost:8088/', 'hey', 'peekaboo')

def on_dtmf(channel, event):
    digit = event['digit']
    if digit == '#':
        channel.play(media='sound:goodbye')
        channel.continueInDialplan()
    elif digit == '*':
        channel.play(media='sound:asterisk-friend')
    else:
        channel.play(media='sound:digits/%s' % digit)


def on_start(channel, event):
    channel.on_event('ChannelDtmfReceived', on_dtmf)
    channel.answer()
    channel.play(media='sound:hello-world')


client.on_channel_event('StasisStart', on_start)
client.run(apps="hello")

Development

The code is documented using Sphinx, which allows IntelliJ IDEA to do a better job at inferring types for autocompletion.

To keep things isolated, I also recommend installing (and using) virtualenv.

$ sudo pip install virtualenv
$ mkdir -p ~/virtualenv
$ virtualenv ~/virtualenv/ari
$ . ~/virtualenv/ari/bin/activate

Setuptools is used for building. Nose is used for unit testing, with the coverage plugin installed to generated code coverage reports. Pass --with-coverage to generate the code coverage report. HTML versions of the reports are put in cover/index.html.

$ ./setup.py develop   # prep for development (install deps, launchers, etc.)
$ ./setup.py nosetests # run unit tests
$ ./setup.py bdist_egg # build distributable

TODO

  • Create asynchronous bindings that can be used with Twisted, Tornado, etc.
  • Add support for Python 3

License

Copyright (c) 2013-2014, Digium, Inc. All rights reserved.

Swagger.py is licensed with a BSD 3-Clause License.

More Repositories

1

asterisk

The official Asterisk Project repository.
C
2,223
star
2

node-ari-client

Node.js client for ARI. This library is best effort with limited support.
JavaScript
253
star
3

ari-examples

ARI examples in Python and JavaScript.
JavaScript
104
star
4

starpy

Python twisted library for AMI and FastAGI
Python
95
star
5

pjproject-archive

Asterisk fork of PJSIP NO PULL REQUESTS OR ISSUES!!!
C
69
star
6

asterisk-external-media

JavaScript
64
star
7

cyber_mega_phone_2k

JavaScript
61
star
8

dahdi-linux

This is the official dahdi-linux repository. All issues and PR should be raised here.
C
49
star
9

aeap-speech-to-text

Asterisk external speech to text application
JavaScript
47
star
10

testsuite

The official Asterisk Test Suite repository.
Python
33
star
11

documentation

The Asterisk Documentation Project.
XSLT
29
star
12

libss7

C
16
star
13

dahdi-tools

This is the official dahdi-tools repository. All issues and PR should be raised here.
C
16
star
14

asterisk_rest_libraries

Client library generator for the REST API provided by Asterisk's res_http_stasis.
Python
15
star
15

node-confbridge

Conference Bridge Application using ARI and Node.js
JavaScript
15
star
16

libpri

PRI Library
C
9
star
17

node-voicemail

Voicemail application using ARI and Node.js
JavaScript
9
star
18

node-voicemail-js

Prototype voicemail application implemented with ARI and Node.js
JavaScript
7
star
19

infrastructure

Configuration for the Asterisk project Continuous Integration infrastructure
Groovy
5
star
20

node-ari-client-wrapper

ARI client wrapper
JavaScript
5
star
21

node-voicemail-logging

Logging module for voicemail.
JavaScript
4
star
22

third-party

Tarballs for various third-party libraries
4
star
23

node-voicemail-data

Data abstraction layer for voicemail application
JavaScript
4
star
24

node-voicemail-admin

A command line administration tool for managing mailboxes in node-voicemail
JavaScript
4
star
25

node-voicemail-mailbox

Mailbox module for voicemail
JavaScript
4
star
26

node-voicemail-fsm

Voicemail application finite state machine
JavaScript
3
star
27

node-voicemail-main

Voicemail main application using ARI and Node.js
JavaScript
3
star
28

node-voicemail-auth

Authentication module for voicemail
JavaScript
3
star
29

node-voicemail-prompt

Prompt module for voicemail
JavaScript
3
star
30

node-voicemail-main-fsm

Voicemail main application finite state machine
JavaScript
3
star
31

voicemail-docs

Documentation for Node.js Voicemail Application
2
star
32

asterisk-feature-requests

A place to submit feature and improvement requests for the Asterisk project. Contains no code.
2
star
33

node-voicemail-notify

Notification module for voicemail
JavaScript
1
star
34

node-voicemail-config

Configuration module for voicemail
JavaScript
1
star
35

reviewboard-crowd

Atlassian Crowd authentication integration with ReviewBoard
Python
1
star
36

publish-docs

The publish-docs utility for Asterisk, takes Asterisk XML documentation and Confluenceizes it
XSLT
1
star