File: //home/arjun/projects/buyercall_new/buyercall/buyercall/blueprints/api2/doc/endpoints/audiourls.py
import logging
from flask import jsonify, request
from flask_restx import Resource
from sqlalchemy import and_
from buyercall.lib.util_rest import rest_partnership, requires_auth, rest_is_partnership
from buyercall.blueprints.api2.doc import serializers
from buyercall.blueprints.phonenumbers.models import Phone, Audio
from buyercall.blueprints.api2.restplus import api
from buyercall.blueprints.partnership.models import PartnershipAccount
from buyercall.blueprints.widgets.models import Widget
log = logging.getLogger(__name__)
ns = api.namespace('Audio URLs', description='Operations related to phone number audio URLs.', path='/accounts')
@ns.route('/<int:paid>/audiourls')
@api.doc(responses={200: 'OK',
400: 'Error performing operation.',
401: 'Unauthorized request.'},
params={'paid': 'The partner account Id'})
class AudioUrls(Resource):
"""
# DISABLE ENDPOINT
@api.response(200, 'Audio URL successfully created.')
@api.expect(serializers.audio_url_add, validate=True)
@requires_auth
def post(self, paid):
"""
"""
Add an phone number audio URL.
<p>
The Audio URLs POST endpoint should be used to create new Audio URLs to be
utilized by phone numbers and widgets.
The Audio URLs are resources pointing to audio files that is used for greeting and
whisper messages during phone calls generated from inbound calls and widget calls.
The Audio URLs gets utilized in place of text-to-speech when
message types, defined in the phone number or widget setup, is set to 'audio'.
</p>
<br />
<p>For example, if the
voicemailMessageType field is set to 'audio', during the phone number or widget configuration, the audio URL
associated with the phone number id or widget guid in the Audio URL table will be used for the voicemail message
instead of using text specified in the voicemailMessage field. This provides the customer the opportunity to
play a custom message instead of using an automated 'robot' voice to speak the message. You will need a
phone number or widget
configured before you can create a new Audio URL using the Audio URL Post endpoint.
</p>
<br />
<p>
You require a partner authentication token, a partner account id and a phone number id or
widget guid to make a successful request. A response will be returned, similar to the example below, based
on a successful request:
<br />
<br />
</p>
<pre class="code-background" style="color: white">
{
"audio_url_id": 6,
"partnership_account_id": 1,
"phonenumber_id": 5
}
</pre>
"""
"""
if rest_is_partnership and rest_partnership:
partnership_account = PartnershipAccount \
.query \
.filter(and_(PartnershipAccount.id == paid, PartnershipAccount.partnership_id == rest_partnership.id)) \
.first()
if partnership_account:
received = request.json
result_enabled = False
result_widget_guid = None
result_phonenumber_id = received['phonenumber_id']
result_whisper_message_type = received['whisper_message_type']
result_audio_url = received['audio_url']
widget = None
if 'widget_guid' in received:
result_widget_guid = received['widget_guid']
widget = Widget.query\
.filter(and_(Widget.partnership_account_id == paid, Widget.guid == result_widget_guid))\
.first()
if 'enabled' in received:
result_enabled = received['enabled']
phone = Phone.query\
.filter(and_(Phone.id == result_phonenumber_id, Phone.partnership_account_id == paid))\
.first()
if phone:
if not widget:
result_widget_guid = None
params = {
'inbound_id': result_phonenumber_id,
'widget_guid': result_widget_guid,
'audio_url': result_audio_url,
'whisper_message_type': result_whisper_message_type,
'enabled': result_enabled
}
audio_id = Audio.api_created(params)
if audio_id > 0:
return jsonify(
partnership_account_id=paid,
phonenumber_id=result_phonenumber_id,
audio_url_id=audio_id
)
return api.abort(code=400, message="Error adding audio URL.")
return api.abort(code=400, message="Error adding audio URL. Phone number not found.")
return api.abort(code=400, message="Error adding audio URL. Account not found.")
return api.abort(401)
"""
@requires_auth
def get(self, paid):
"""
Retrieves all phone number audio URLs.
<p>
The Audio URLs GET endpoint should be used to return the Audio URLs for a partner account.
</p>
<br />
<p>
You require a partner authentication token and a partner account id to make a successful request.
A response will be returned, similar to the example below, based
on a successful request:
<br />
<br />
</p>
<pre class="code-background" style="color: white">
{
"audio_urls": [
{
"audio_url": "http://url.com/song.mp3",
"created_on": "2019-04-04 19:30:25",
"enabled": true,
"id": 6,
"phonenumber_id": 5,
"updated_on": "2019-04-04 19:30:25",
"whisper_message_type": "whisperMessage",
"widget_guid": "11ba6af7-b6f1-47dc-a561-1b7e3e0e037b"
}
]
}
</pre>
"""
audio_url_list = []
merged_list = []
if rest_is_partnership and rest_partnership:
partnership_account = PartnershipAccount \
.query \
.filter(and_(PartnershipAccount.id == paid, PartnershipAccount.partnership_id == rest_partnership.id)) \
.first()
if partnership_account:
phones = Phone.query.with_entities('id').filter(Phone.partnership_account_id == paid).all()
widgets = Widget.query.with_entities('guid').filter(Widget.partnership_account_id == paid).all()
if phones:
phone_audio = Audio.query.with_entities('id').filter(Audio.inbound_id.in_(phones)).distinct().all()
merged_list = merged_list + phone_audio
if widgets:
widget_audio = Audio.query.with_entities('id').filter(
Audio.widget_guid.in_(widgets)
).distinct().all()
merged_list = merged_list + widget_audio
audios = Audio.query.filter(Audio.id.in_(merged_list)).distinct().all()
for audio in audios:
audio_add = {
'id': audio.id,
'phonenumber_id': audio.inbound_id,
'widget_guid': audio.widget_guid,
'audio_url': audio.audio_url,
'enabled': audio.enabled,
'created_on': audio.created_datetime,
'updated_on': audio.updated_datetime,
'whisper_message_type': audio.whisper_message_type
}
audio_url_list.append(audio_add)
return jsonify(
audio_urls=audio_url_list
)
else:
return api.abort(code=400, message="Partnership account not found.")
else:
return api.abort(401)
"""
# DISABLE ENDPOINT
@ns.route('/<int:paid>/audiourls/<int:auid>')
@api.doc(responses={200: 'OK',
400: 'Error performing operation.',
401: 'Unauthorized request.'},
params={'paid': 'The partner account Id',
'auid': 'The phone number audio URL Id'})
"""
class AudioUrlsEdit(Resource):
@api.response(200, 'Audio URL successfully updated.')
@api.expect(serializers.audio_url_edit, validate=True)
@requires_auth
def put(self, paid, auid):
"""
Update a phone number audio URL.
<p>
The Audio URLs PUT endpoint should be used to update an Audio URL for a partner account.
</p>
<br />
<p>
You will require a partner authentication token, a partner account id as well as a audio url id to make a
successful request.
</p>
"""
if rest_is_partnership and rest_partnership:
partnership_account = PartnershipAccount \
.query \
.filter(and_(PartnershipAccount.id == paid, PartnershipAccount.partnership_id == rest_partnership.id)) \
.first()
if partnership_account:
received = request.json
result_enabled = None
result_widget_guid = None
result_phonenumber_id = None
result_whisper_message_type = None
result_audio_url = None
if 'phonenumber_id' in received:
result_phonenumber_id = received['phonenumber_id']
phone = Phone.query\
.filter(and_(Phone.id == result_phonenumber_id, Phone.partnership_account_id == paid))\
.first()
if not phone:
return api.abort(
code=400,
message="Error updating audio URL. Phone number does not exist in partnership account."
)
if 'widget_guid' in received:
result_widget_guid = received['widget_guid']
widget = Widget.query\
.filter(and_(Widget.guid == result_widget_guid, Widget.partnership_account_id == paid))\
.first()
if not widget:
return api.abort(
code=400,
message="Error updating audio URL. Widget does not exist in partnership account."
)
if 'enabled' in received:
result_enabled = received['enabled']
if 'whisper_message_type' in received:
result_whisper_message_type = received['whisper_message_type']
if 'audio_url' in received:
result_audio_url = received['audio_url']
result = Audio.api_update(auid, result_phonenumber_id, result_widget_guid,
result_whisper_message_type, result_audio_url, result_enabled)
if result:
return True, 204
return api.abort(code=400, message="Error updating audio URL.")
return api.abort(code=400, message="Partnership account not found.")
return api.abort(401)
@api.response(200, 'Audio URL successfully deleted.')
@requires_auth
def delete(self, paid, auid):
"""
Deletes a phone number audio URL.
<p>
The Audio URLs DELETE endpoint should be used to delete an Audio URL for a partner account.
</p>
<br />
<p>
You will require a partner authentication token, a partner account id as well as a audio url id to make a
successful request.
</p>
"""
if rest_is_partnership and rest_partnership:
partnership_account = PartnershipAccount \
.query \
.filter(and_(PartnershipAccount.id == paid, PartnershipAccount.partnership_id == rest_partnership.id)) \
.first()
if partnership_account:
try:
result = Audio.api_delete(auid, paid)
if result:
return True, 200
else:
return api.abort(code=400, message="Unable to delete audio URL.")
except Exception as e:
log.error('Error deleting audio URL. Error: {}'.format(e))
return api.abort(code=400, message="Unable to delete audio URL.")
else:
return api.abort(code=400, message="Partnership account not found.")
else:
return api.abort(401)