Python script example

Not applicable

Could someone have python sample code to API with methods namely get and post within Apigee. I have tested a request in the Apigee Developer Console and go HTTP 1.1 200 OK. I searched in Apigee but there is lack of python script examples. Thanks ahead

Below is the my Request details;

POST /rest/connections HTTP/1.1 Authorization: Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx apiAccessKeyId: ?????.???@????.com.au.nctest Host: apisandbox-api.zuora.com Content-Length: 0 X-Target-URI: https://apisandbox-api.zuora.com Connection: Keep-Alive apiSecretAccessKey: xxxxxxxx

0 9 4,981
9 REPLIES 9

adas
Participant V

@ricard le I didn't understand exactly what you are looking for. If you are asking about a simple python script policy that you can run in apigee to make REST api calls. Here's an example which uses httplib module:

import urllib, httplib2

github_url = 'https://api.github.com/user/repos'

h = httplib2.Http(".cache") # WAT?
h.add_credentials("user", "******", "https://api.github.com")

data = urllib.urlencode({"name":"test"})
resp, content = h.request(github_url, "POST", data)

print content

You can add a python script policy and paste this script in the .py file. Let me know if this helps.

Not applicable

Hi, i have changed the url and kept the same variable names but with the following error. any help much appreciated.

github_url = 'https://apisandbox-api.zuora.com/rest/v1/journal-entries/journal-runs/JR-00000136'

h.add_credentials("kiung.lay@sensis.com.au.nctest", "xxxxxxx", "https://apisandbox-api.zuora.com/rest/v1")

Traceback (most recent call last): File "<pyshell#7>", line 1, in <module> resp, content = h.request(github_url, "POST", data) File "C:\Python34\lib\site-packages\httplib2\__init__.py", line 1313, in request (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey) File "C:\Python34\lib\site-packages\httplib2\__init__.py", line 1072, in _request for authorization in self._auth_from_challenge(host, request_uri, headers, response, content): File "C:\Python34\lib\site-packages\httplib2\__init__.py", line 958, in _auth_from_challenge challenges = _parse_www_authenticate(response, 'www-authenticate') File "C:\Python34\lib\site-packages\httplib2\__init__.py", line 251, in _parse_www_authenticate raise MalformedHeader("WWW-Authenticate") httplib2.MalformedHeader: WWW-Authenticate

adas
Participant V

@ricard le

Some of these libraries may not be available with the version of python we use in the platform. Please try this instead:

import hashlib
import httplib, urllib


params = [("foo", "bar"),("apigee", "iloveapis")]
params = urllib.urlencode(params)
headers = {"Content-type": "application/xml"}
conn = httplib.HTTPConnection("www.httpbin.org:80")
conn.request("POST", "/post", params, headers)
response = conn.getresponse()
data = response.read()
print response.status, response.reason
print data
conn.close()
flow.setVariable("response.content", data);

Similarly you can make GET and PUT or DELETE calls too. The flow.setVariable is being used to set the response so that the response from the this call would be sent to the caller of your api proxy.

Hi I am a little confused as to where to put my 2 urls in params.

also i don't know there put my user_id and password. can you help out. cheers

target_url = 'https://apisandbox-api.zuora.com/rest/v1/journal-entries/journal-runs/JR-00000136'

base_url = "https://apisandbox-api.zuora.com/rest/v1"

@ricard le That's nothing specific to Apigee actually. You just need to follow the jython documentation for http client.

This might be a good place to get started: http://www.jython.org/docs/library/httplib.html

@ricard le , Welcome to Apigee Community 🙂

Can you please let us know what exactly do you mean python script example ? Are you looking to implement some logic on API Proxy using Apigee Extension Policy Python script ? Are you looking to consume API using python script ?

adas
Participant V

@sgilson This might be a good example to be added to the docs for the python policy. I am sure users would have similar questions. What do you think ?

It's a good question, but we don't have examples showing how to call a REST API in Java, Ajax, etc. There should be a lot out there that shows it.

Stephen

Not applicable

You're right. I've also found there are not enough questions on python script that relates specific to Apigee.