Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Updated using 4PSA automated script

Excerpt

This example written in Python allows users to manage VoipNow accounts and fetch call statistics.

Table of Contents
typelist

How To Install It

In order to To be able to use the SystemAPI Python Tool, you must first check what components you need depending on your operating system and after that follow the installation steps.ensure that you have the following resources: 

System Requirements for Linux

System Requirements for Microsoft Windows

Setup

Note
titleThe code is available on GitHub

Download here. Do not hesitate to contribute in order to make this example better - we welcome Pull Requests!

Linux

STEP 1: As soon as gcc and Python are installed and fpconst is downloaded, please extract the contents of  Install GCC and Python and download fpconst. Then unzip the fpconst archive, open the extracted folder, and run the following commands:

Code Block
python
python
python setup.py build
python setup.py install

To check that make sure fpconst has  has been properly installed, type the following in  in your Python command-line:

Code Block
languagepython
import fpconst

If you don't have re not getting any error messages, it means fpconst is correctly installed.

STEP 2: As soon as Download PyXMLis downloaded, please extract the contents of , then unzip the archive, open the extracted folder, and run the following commands:

Code Block
python
python
python setup.py build
python setup.py install

To check that make sure PyXML has been properly installed, type the following in your Python command-line:

Code Block
languagepython
import xml

If you do 're not have getting any error messages, it means that PyXML is correctly installed.

STEP 3: As soon as Download SOAPpyis downloaded, please extract the contents of , then unzip the archive, open the extracted folder, and run the following commands:

Code Block
python
python
python setup.py build
python setup.py install

To check that make sure SOAPpy has been properly installed, type the following in your Python command-line:

Code Block
languagepython
import SOAPpy

If you do 're not have getting any error messages, it means SOAPpy is correctly installed.

...

If during installation you

...

're prompted the following error

...

:

Code Block
File ".../SOAPpy/Client.py", line 46


from _future_ import nested_scopes


SyntaxError: from _future_ imports must occur at the beginning of the file 

You need to open your temporary folder SOAPpy\Client.py and copy/paste the "from _future_ imports" line to be the first import. The Client.py is just a temporary folder used by the SOAPpy engine, so ignore it.

Note

Please note that the same error will occur
The same error will appear with other files as well during installation; therefore. Therefore, it is recommended that you repeat this step .

Note

The Client.py is just a temporary folder used by the SOAPpy engine. You don't need to bother with it.

whenever necessary.

STEP 4: Download ZSI, then unzip the archive, STEP 4: As soon as ZSI is downloaded, please extract the contents of the archive, open the extracted folder, and run the following commands:

Code Block
python
python
python setup.py build
python setup.py install

To check that make sure ZSI has been properly installed, type the following in your Python command-line:

Code Block
languagepython
import ZSI

If you do 're not have getting any error messages, it means ZSI is correctly installed.

STEP 5: You  At this point, you can start writing applications for your VoipNow server using the SOAPpy web services toolkit.

Microsoft Windows

STEP 1: As soon as  Install Visual Studio 2008 Express and Python are installed , and fpconst is downloaded, please extract the contents of the fpconst download fpconst. Then unzip the fpconst archive, open the extracted folder, and run the following commands:

Code Block
python
python
python setup.py build
python setup.py install

STEP 2: As soon as PyXML is downloaded, you need to Download PyXML and run the installer:

Note

There are different PyXML installers for various Python versions, so make sure you download the appropriate one, otherwise the installer will not work. If you're not sure what to do, please visit the links in the README file.

STEP 3: As soon as Download SOAPpy is downloaded, please extract the contents of , then unzip the archive, open the extracted folder, and run the following commands:

If you get
Code Block
python
python
python setup.py build
python setup.py install
Warning

If during installation you're prompted the following error

...

:

Code Block
File ".../SOAPpy/Client.py", line 46


from _future_ import nested_scopes


SyntaxError: from _future_ imports must occur at the beginning of the file 

You must open your temporary folder SOAPpy\Client.py and copy/paste the "from _future_ imports" line to be the first import.

Note

Please note that the
The same error will appear occur with other files as well during installation; therefore. Therefore, it is recommended that you repeat this step whenever necessary.

STEP 4: As soon as Download ZSI is downloaded, please extract the contents of , then unzip the archive, open the extracted folder, and run the following commands:

Code Block
python
python
python setup.py build
python setup.py install

STEP 5: You  At this point, you can start writing applications for your VoipNow server using the SOAPpy web services toolkit.

...

When writing applications, you may

...

be prompted the following error:

Code Block
Traceback (most recent call last):
File "pythonDemo.py", line 61, in ?
server._callWithBody(body1)
File "/usr/lib/python2.4/site-packages/SOAPpy/Client.py", line 436, in _callWithBody
return self.__call(None, body, {})
File "/usr/lib/python2.4/site-packages/SOAPpy/Client.py", line 395, in __call
p, attrs = parseSOAPRPC(r, attrs = 1)
File "/usr/lib/python2.4/site-packages/SOAPpy/Parser.py", line 1049, in parseSOAPRPC
t = _parseSOAP(xml_str, rules = rules)
File "/usr/lib/python2.4/site-packages/SOAPpy/Parser.py", line 1029, in _parseSOAP
parser.parse(inpsrc)
File "/usr/lib/python2.4/site-packages/_xmlplus/sax/expatreader.py", line 109, in parse
xmlreader.IncrementalParser.parse(self, source)
File "/usr/lib/python2.4/site-packages/_xmlplus/sax/xmlreader.py", line 123, in parse
self.feed(buffer)
File "/usr/lib/python2.4/site-packages/_xmlplus/sax/expatreader.py", line 216, in feed
self._parser.Parse(data, isFinal)
File "/usr/lib/python2.4/site-packages/_xmlplus/sax/expatreader.py", line 363, in end_element_ns
self._cont_handler.endElementNS(pair, None)
File "/usr/lib/python2.4/site-packages/SOAPpy/Parser.py", line 235, in endElementNS
kind = (self._prem[kind[:i]], kind[i + 1:])
KeyError: u'http'
Note

If Whenever this happens to you, please make sure you download the latest patch for the Parser.py file from the SOAPpy library.
You may find it here: http://pywebsvcs.svn.sourceforge.net/viewvc/pywebsvcs/trunk/SOAPpy/SOAPpy/Parser.py?view=markup&pathrev=1497The patch is available here.

How To Use It

Authentication

First you need to create STEP 1: Create a headerType object.

Code Block
python
python
header1 = SOAPpy.Types.headerType()

Then you need to fill STEP 2: Fill in the user credentials. VoipNow uses the OAuth protocol, where the user must provide his access token. The old legacy authentication and OAuth with consumer key and consumer secret are deprecated.

...

Code Block
python
python
# Authentication data
ACCESS_TOKEN = "CHANGEME"
  sys.argv[2]

# Start constructing the Header
soapHeader = SOAPpy.Types.headerType()

#XML template for sending the authentication data
authenticationTemplate = """ <ns1:accessToken>%s</ns1:accessToken>"""
  				
# filling in the template above with our authentication data				
credentials = authenticationTemplate % (ACCESS_TOKEN)

soapHeader.userCredentials = SOAPpy.Types.headerType(credentials)

# the namespace for the user credentials
soapHeader.userCredentials._ns = "http://4psa.com/HeaderData.xsd/3.0.0"
soapHeader.userCredentials.userCredentials = SOAPpy.Types.untypedType(credentials)

Examples

Add a Service Provider

Examples

Add a Service Provider

In DemoAddServiceProvider.py, you have an example on how to The following example shows how to add a new service provider. It The script also fetches the list of charging plans in order to pick a random one to use with plans you can choose from and use for the new service provider.

For more information on how to get the access token, please check the access management documentation. The code should look like this:

Code Block
curl "https://raw.githubusercontent.com/4psa/systemapi-example-python/Demo/DemoAddServiceProvider.py" -o DemoAddServiceProvider.py -L

 management documentation.

Adding Other Account Types

If you wish want to add other account types (organizations, users or extensions), there is not much to change from the program listed above. First of allsimply tweak the previous Service Provider example using the recommendations below.  

Most important, you will also need the id of a parent account. This stands for the ID of a parent account. Depending on the account type you want to add, this ID can be:

  • The ID of the service provider that owns the organization (when adding a new organization)

...

  • The ID of the organization that owns the user (when adding a new user)

...

  •  
  • The ID of the user that owns the extension (when adding a new extension)

. Therefore, the list of appropriate accounts will be fetched and a random one will be chosen as the parent for of the new account:

Code Block
languagepython
# We need a parent service provider for the new organization, so we make a request to
# fetch all the service providers.

# Construct the Body
soapBody = SOAPpy.Types.untypedType("")
soapBody._name = "ns2:GetServiceProviders"  

# Set namespaces
soapBody._setAttr("xmlns:ns2", "http://4psa.com/ServiceProviderMessages.xsd/3.0.0"" + version)

# Set endpoint
endpoint = "https://voipnow2demo.4psa.com" + str(sys.argv[1]) + "/soap2/sp_agent.php"

# Set soapaction
soapaction = "http://4psa.com/ServiceProvider/3.0.0" + version + ":getServiceProvidersIn"

# Set service connection
server = SOAPProxy(endpoint, noroot = 1, soapaction = soapaction, header = soapHeader)

#run our soap request
try:
	serviceProviders = server._callWithBody(soapBody)
except SOAPpy.Types.faultType, error:
	# Catch exception, for situations when the service providers could not be fetched
	print "Error " + error[0] + ": " + error[1]
	sys.exit(1)

# Get the id of a random service provider
serviceProviderID = None
if len(serviceProviders) != 0:
	randServiceProvider = serviceProviders[random.randint(0, len(serviceProviders) - 1)]
	if hasattr(randServiceProvider, 'ID'):
		serviceProviderID = randServiceProvider.ID
		if hasattr(randServiceProvider, 'name')
		if hasattr(randServiceProvider, 'name'):
			print "Using parent service provider " + randServiceProvider.name + "."
		else:
			print "Using parent service provider with id " + randServiceProvider.nameserviceProviderID + "."
		else
if serviceProviderID == None:
			print "Using parent service provider with id " + serviceProviderID + "."

if serviceProviderID == None:
	print "No service providers found on the server. Can not add an organization."
	sys.exit(1)

You will also need to make adjustments to the parameters of the new request, in order to match the new account type:

print "No service providers found on the server. Can not add an organization."
	sys.exit(1)

You will also need to make adjustments to the parameters of the new request so that they match the new type of account:

Code Block
languagepython
# Organization data
name = "OrgPython" + str(random.randint(1, 1000))
login = "OrgPython" + str(random.randint(1, 1000))
firstname = "FirstnamePython" + str(random.randint(1, 1000))
lastname = "LastnamePython
Code Block
languagepython
# Organization data
name = "OrgPython" + str(random.randint(1, 1000))
loginemail = "OrgPythongEmail" + str(random.randint(1, 1000)) + "@example.com"
password = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(10))
country = "us"
parentID = serviceProviderID
company = "test_company"

# Filling in the template above with the organization data
if chargingPlanID != None:
	body = bodyTemplate % (name, login, firstname, lastname, email, password, country, parentID, company, chargingPlanID)
else:
	body = bodyTemplate % (namename, login, firstname, lastname, loginemail, password, country, parentID, company)

soapBody = SOAPpy.Types.untypedType(body)
soapBody._name = "ns2:AddOrganization" 
 
# Set namespaces
soapBody._setAttr("xmlns:ns2", "http://4psa.com/OrganizationMessages.xsd/3.0.0"" + version)
soapBody._setAttr("xmlns:ns3", "http://4psa.com/OrganizationData.xsd/3.0.0"" + version)

# Set endpoint
endpoint = "https://voipnow2demo.4psa.com" + str(sys.argv[1]) + "/soap2/organization_agent.php"

# Set soapaction
soapaction = "http://4psa.com/Organization/3.0.0" + version + ":addOrganizationIn"

Adding an a user or an extension is very similar. You only need to adjust the namespaces, endpoint, and soapaction variables. Also, for extension accounts, you should use the label field instead of name and login.

CallCosts

The DEMO version also provides a CallCosts request. Here is the code:

...

.

...

 

 

Except where otherwise noted, content in this space is licensed under a Creative Commons Attribution 4.0 International.