Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
La Quadrature du Net
piphone
piphone-sip
Commits
b57018d9
Commit
b57018d9
authored
May 09, 2016
by
okhin
Browse files
Adding ari.py - a small and incomplete API for the ARI REST API
parent
afc5535f
Changes
1
Show whitespace changes
Inline
Side-by-side
ari.py
0 → 100644
View file @
b57018d9
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
:
base_url
=
config
[
'protocol'
]
+
'://'
+
config
[
'server'
]
+
'/'
+
config
[
'base_url'
]
except
KeyError
as
e
:
raise
e
try
:
full_url
=
'/'
.
join
([
base_url
,
'/'
.
join
(
verbs
)])
if
method
.
loweer
()
==
'del'
:
call
=
requests
.
delete
else
:
call
=
getattrs
(
requests
,
method
.
lower
())
payload
[
'app'
]
=
config
[
'app'
]
payload
[
'api_key'
]
=
config
[
'api_key'
]
result
=
call
(
full_url
,
data
=
payload
)
result
.
raise_for_status
()
return
result
.
json
()
except
:
raise
class
Bridge
(
object
):
'''
This is a bridge object, we use it to manage various needed calls
'''
def
__init__
(
self
,
config
,
name
,
mode
):
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
class
Channel
(
object
):
'''
This class is used to regroup all the method to manage a Channel
'''
def
__init__
(
self
,
config
,
name
):
self
.
config
=
config
self
.
channel
=
name
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
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
:
return
ari_call
(
self
.
config
,
[
'playbacks'
,
self
.
name
],
'DEL'
,
{})
except
:
raise
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment