How to call "call_api" using PureCloudPlatformClientV2 (Python)

Hi, I'm able to connect using the example provided below and print the get_authorization_permissions, but I'm not able to call "call_api" to access "/api/v2/telephony/providers/edges". Is there a example available how to use "call_api" or how do I check Edge servers using PureCloudPlatformClientV2 ?

import PureCloudPlatformClientV2
client_id = "xxxxxx"
client_secret = "yyyyyy"
apiclient = PureCloudPlatformClientV2.api_client.ApiClient().get_client_credentials_token(client_id, client_secret)
authApi = PureCloudPlatformClientV2.AuthorizationApi(apiclient)
print(authApi.get_authorization_permissions().to_json())

You shouldn't use call api directly. Use the API classes and methods to invoke API resources. This concept is documented here: https://developer.mypurecloud.com/api/rest/client-libraries/python/#making_requests. For your specific request, see the SDK documentation: https://developer.mypurecloud.com/api/rest/client-libraries/python/TelephonyProvidersEdgeApi.html#get_telephony_providers_edges

Hi Ighor,

Also make sure you look at the reference document Tim has posted above. Each one of our API endpoints (and their corresponding SDKs) will show you the specific permissions you need to access the endpoint. In the endpoint above, it looks like your OAuth Client Credential grant needs to have a Role that has the telephony:plugin:all permission.

Thanks,
John Carnell
Manager, Developer Engagement

Tim, thank you. I found a way to do it using the code example below. But, I'm getting error for "get_telephony_providers_edge_diagnostic_nslookup" and "get_telephony_providers_edge_diagnostic_ping" . Any idea? Please check error below:

Code:

telephonyProvidersEdgeApi = PureCloudPlatformClientV2.TelephonyProvidersEdgeApi(apiclient)
print(telephonyProvidersEdgeApi.get_telephony_providers_edge_diagnostic_ping("xxxxxxx"))

Error:

    PureCloudPlatformClientV2.rest.ApiException: (400)
    Reason: Bad Request
    HTTP response headers: HTTPHeaderDict({'Content-Type': 'application/json', 'Content-Length': '230', 'Connection': 'keep-alive', 'Date': 'Wed, 06 Jan 2021 16:35:17 GMT', 'inin-ratelimit-count': '1', 'inin-ratelimit-allowed': '300', 'inin-ratelimit-reset': '61', 'ININ-Correlation-Id': 'xxxxxxxxxd', 'Strict-Transport-Security': 'max-age=600; includeSubDomains', 'Cache-Control': 'no-cache, no-store, must-revalidate', 'Pragma': 'no-cache', 'Expires': '0', 'X-Cache': 'Error from cloudfront', 'Via': '1.1 b595ec3d1fc5071e24341ed67173de69.cloudfront.net (CloudFront)', 'X-Amz-Cf-Pop': 'ATL56-C4', 'X-Amz-Cf-Id': 'yyyyyyyy=='})
    HTTP response body: {"message":"Network diagnostic not found for the command: PING for edge : zzzzzzzzzzzzzzzzz","code":"general.bad.request","status":400,"contextId":"zzzzzzzzzzzzzzz","details":[],"errors":[]}

Hi John, any idea what is the error in the post above?

Hi Ighort,

Let me check with some of my old team mates in Edge services. I know the developer who wrote the endpoints you are hitting. I have a suspicion but want to validate with her.

Thanks,
John Carnell
Manager - Developer Engagement

Hi Ighort,

Thanks for reaching out to us!! Getting a network diagnostic is a 2 step process. First you need to call POST endpoint and then GET.

Steps 1: Choose any of the post apis

https://developer.mypurecloud.com/api/rest/client-libraries/python/TelephonyProvidersEdgeApi.html#__edgenetworkdiagnostic____edgenetworkdiagnostic_html__post_telephony_providers_edge_diagnostic_nslookup_edge_id__body

POST: /api/v2/telephony/providers/edges/{edgeId}/diagnostic/ping

POST: api/v2/telephony/providers/edges/{edgeId}/diagnostic/route

POST: /api/v2/telephony/providers/edges/{edgeId}/diagnostic/nslookup

POST: /api/v2/telephony/providers/edges/{edgeId}/diagnostic/tracepath

Payload Example:

{
"host": "10.27.79.240"
}

In response you will get a self uri

Response Example:

{

"selfUri": "/api/v2/telephony/providers/edges/{edgeId}/diagnostic/ping"

}

STEP 2 :

Use that self uri to get the diagnostic information of the executed network command for the edge. Paths of Get endpoints are same as POST.

https://developer.mypurecloud.com/api/rest/client-libraries/python/TelephonyProvidersEdgeApi.html#__edgenetworkdiagnosticresponse____edgenetworkdiagnosticresponse_html__get_telephony_providers_edge_diagnostic_nslookup_edge_id

GET: /api/v2/telephony/providers/edges/{edgeId}/diagnostic/ping

GET: /api/v2/telephony/providers/edges/{edgeId}/diagnostic/route

GET: /api/v2/telephony/providers/edges/{edgeId}/diagnostic/nslookup

GET: /api/v2/telephony/providers/edges/{edgeId}/diagnostic/tracepath

Response Example:

{

"commandCorrelationId": "cda9e939-31d9-48da-8fa0-64879826200b",

"diagnostics": "\n====== /usr/bin/ping -a -D -c 4 -4 10.27.79.240 ======\nPING 10.27.79.240 (10.27.79.240) 56(84) bytes of data.\n\n--- 10.27.79.240 ping statistics ---\n4 packets transmitted, 0 received, 100% packet loss, time 3062ms\n\n\nError exitCode=1 on cmd=/usr/bin/ping -a -D -c 4 -4 10.27.79.240"

}

Please let me know if you still face any issue, I will be happy to help!!

Thanks,
Sneha Tiwari

1 Like

Thank you for your reply... I'm using the code below, but still getting the same error (400):

CODE:

client_id = "xxxxxxx"
client_secret = "yyyyyy"
apiclient = PureCloudPlatformClientV2.api_client.ApiClient().get_client_credentials_token(client_id, client_secret)
authApi = PureCloudPlatformClientV2.AuthorizationApi(apiclient)
telephonyProvidersEdgeApi = PureCloudPlatformClientV2.TelephonyProvidersEdgeApi(apiclient)
edge_id = 'xxxxxxx' # str | Edge Id
body = PureCloudPlatformClientV2.EdgeNetworkDiagnosticRequest() # EdgeNetworkDiagnosticRequest | request payload to get network diagnostic
try:
    api_response  = telephonyProvidersEdgeApi.post_telephony_providers_edge_diagnostic_ping(edge_id, body)
    pprint(api_response)
except ApiException as e:
    print("Exception when calling TelephonyProvidersEdgeApi->post_telephony_providers_edge_diagnostic_ping: %s\n" % e)

ERROR:

Exception when calling TelephonyProvidersEdgeApi->post_telephony_providers_edge_diagnostic_ping: (400)
Reason: Bad Request
HTTP response headers: HTTPHeaderDict({'Content-Type': 'application/json', 'Content-Length': '170', 'Connection': 'keep-alive', 'Date': 'Wed, 06 Jan 2021 21:27:39 GMT', 'inin-ratelimit-count': '1', 'inin-ratelimit-allowed': '300', 'inin-ratelimit-reset': '61', 'ININ-Correlation-Id': 'xxxxxxx', 'Strict-Transport-Security': 'max-age=600; includeSubDomains', 'Cache-Control': 'no-cache, no-store, must-revalidate', 'Pragma': 'no-cache', 'Expires': '0', 'X-Cache': 'Error from cloudfront', 'Via': '1.1 xxxxxx.cloudfront.net (CloudFront)', 'X-Amz-Cf-Pop': 'ATL56-C4', 'X-Amz-Cf-Id': 'xxxxxx=='})
HTTP response body: {"message":"The requested operation failed with status 400","code":"bad.request","status":400,"contextId":"xxxxxx","details":[],"errors":[]}

Hi Sneha,

I was able to get response from ping using this:

body={'host':ip} #where ip is my edge server

Resposnse:
'self_uri': '/api/v2/telephony/providers/edges/xxxxxxx/diagnostic/ping'

but when I use:
body = PureCloudPlatformClientV2.EdgeNetworkDiagnosticRequest() #
I get : print(body) = 'host':'none' (not sure where I'm missing something here)

Also, how can I call the self_uri using the API ?

Thank you

Hi Ighor,

EdgeNetworkDiagnosticRequest has a mandatory field "host" and you need to set the value before passing it to the request. In your example you are just creating the instance of the request class but did not provide any value to the "host".

https://developer.mypurecloud.com/api/rest/client-libraries/python/EdgeNetworkDiagnosticRequest.html

Once you post the request see this example to GET the response.

https://developer.mypurecloud.com/api/rest/client-libraries/python/TelephonyProvidersEdgeApi.html#__ edgenetworkdiagnosticresponse____edgenetworkdiagnosticresponse_html__get_telephony_providers_edge_diagnostic_nslookup_edge_id

Thanks,
Sneha Tiwari

Hello Ighor,

Sneha answered faster than me :slight_smile:

You would have to set your targeted IP/Host. The example in Python SDK is not showing that step.
So that would be:

body = PureCloudPlatformClientV2.EdgeNetworkDiagnosticRequest() # EdgeNetworkDiagnosticRequest | request payload to get network diagnostic
body.host = "The IP or host you want to ping"

To clarify, this is to request to run a ping command from the edge to this target IP/Host. So you should put something else than your edge IP.

As a side note, it you are using virtual edges (from Genesys Cloud Voice or BYOC Cloud), the ping will probably show 100% packet loss. The virtual edges are hosted on AWS and AWS tend to block any ICMP traffic (ping).
If you have a premise edge, it should work.

I am using BYOC Cloud and Virtual edges in my environment. So ping shows 100% packet loss. But I could use other command (I tried route and it works - didn't try nslookup and trace).

Regards,

1 Like

Hi @Jerome.Saint-Marc Jerome, indeed this works:
body.host = ip
Thank you very much!
But I still can't find which function from this API should I call to get the information from the 'self_uri' returned. Do you have an example? Would this be "AsyncQueryResponse" ???

self_uri in the response refers to the resource that was requested. The SDKs operate based on calling functions on the API Client instance, not using explicit resource paths. If you want to request the same resource again, just make the same request again using the SDK.

Hi @tim.smith, I'm not sure if I make myself clear. In order to get the information from command ping (using the function: post_telephony_providers_edge_diagnostic_ping), I need curl the URL from self_uri right? At least this I understood from the example . So, I want to know if there is a function to request that URL without using curl or requests from python.
This would be like this:

Step 1 = POST (post_telephony_providers_edge_diagnostic_ping)
Step 2 = RESPONSE (self_uri)
Step 3 = GET (self_uri)
Step 4 = RESPONSE from GET : ping xx.x.x.x etc,etc,etc...

Am I wrong? I just can't find way to do Step 3.

Step 1 = POST (post_telephony_providers_edge_diagnostic_ping)

and

Step 3 = GET (self_uri)

would literally be the same request because the self uri points back to the resource that was requested. You don't need the self uri for anything. Use the SDK to make the GET request. https://developer.mypurecloud.com/api/rest/client-libraries/python/TelephonyProvidersEdgeApi.html#get_telephony_providers_edge_diagnostic_ping

Is there a GET function inside the SDK ? I can't find it.

Yes, see the link above:

1 Like

I guess I found it.. lol

POST = post_telephony_providers_edge_diagnostic_ping(edge_id, body)
GET = get_telephony_providers_edge_diagnostic_ping(edge_id)

But response from GET: Network diagnostic not found for the command: PING for edge

Exception when calling TelephonyProvidersEdgeApi->get_telephony_providers_edge_diagnostic_ping: (400)
Reason: Bad Request
HTTP response headers: HTTPHeaderDict({'Content-Type': 'application/json', 'Content-Length': '230', 'Connection': 'keep-alive', 'Date': 'Thu, 07 Jan 2021 20:23:11 GMT', 'ININ-Correlation-Id': 'xxx', 'inin-ratelimit-count': '2', 'inin-ratelimit-allowed': '300', 'inin-ratelimit-reset': '61', 'Strict-Transport-Security': 'max-age=600; includeSubDomains', 'Cache-Control': 'no-cache, no-store, must-revalidate', 'Pragma': 'no-cache', 'Expires': '0', 'X-Cache': 'Error from cloudfront', 'Via': '1.1 xxx.cloudfront.net (CloudFront)', 'X-Amz-Cf-Pop': 'ATL56-C4', 'X-Amz-Cf-Id': 'xxx=='})
HTTP response body: {"message":"Network diagnostic not found for the command: PING for edge : xxx","code":"general.bad.request","status":400,"contextId":"xxx","details":[],"errors":[]}

If you're making the POST request and immediately making the GET request, this seems expected. The ping operation will take several seconds to complete, which is why this task requires two requests. If you're not getting any data after waiting for a bit before checking the result, please open a case with Genesys Cloud Care to investigate why ping results are not eventually being populated.

1 Like

Thank you @tim.smith !!! :wink: