In previous blog we have seen how we can enable REST Services in database schema using ORDS. Also we have created REST API using GET,POST & DELETE Method. Oracle Rest Data Services (ORDS) enable REST interface for the Oracle Database, so that you can create RESTfull APIs that uses HTTP/HTTPS requests to GET, PUT, POST & DELETE resource in the database table.

Refer below blogs for REST Service & APIs setup.

In this blog we will call Oracle Database REST APIs using Python.


REST API 1: For retrieving customer email based on text pattern (GET Method)

import requests
import json

url = "http://localhost:8080/ords/customerorders/search/adam"
req = requests.get(url)
print('Response code received: {code} '.format(code=req.status_code))
if req.status_code == 200:
    req_text_dict = json.loads(req.text)
    for i in req_text_dict['items']:
        print(i)
else:
    print("Some issue in API")

REST API 2: For retrieving orders based on customer email & status (GET Method)

import requests
import json

url = "http://localhost:8080/ords/customerorders/serachorder/orders/"
payload = (('CUSTOMER_ID', 272), ('ORDER_STATUS', "CANCELLED"))
req = requests.get(url, params=payload)

print('Response code received: {code} '.format(code=req.status_code))
if req.status_code == 200:
    req_text_dict = json.loads(req.text)
    for i in req_text_dict['items']:
        print(i)
else:
    print("Some issue in API")

REST API 3: For creating new customer entry (PUT Method)

import requests
import json

url = "http://localhost:8080/ords/customerorders/customer/addcust"
payload = (('EMAIL_ADDRESS', "devuser1@test.com"), ('FULL_NAME', "DevUser1"))
req = requests.post(url, params=payload)
print('Response code received: {code} '.format(code=req.status_code))
if req.status_code == 200:
    req_text_dict = json.loads(req.text)
    print(req_text_dict)
else:
    print("Some issue in API")

REST API 4: For deleting customer entry based using email id (DELETE Method)

import requests
import json

url = "http://localhost:8080/ords/customerorders/customer/addcust"
payload = ('EMAIL_ADDRESS', "devuser1@test.com")
req = requests.delete(url, data=json.dumps(payload))
print('Response code received: {code} '.format(code=req.status_code))
if req.status_code == 200:
    req_text_dict = json.loads(req.text)
    print(req_text_dict)
else:
    print("Some issue in API")

Schema Used: Customer-Orders Sample Schema | Oracle 12c

Thanks!

Happy Learning! Your feedback would be appreciated!

2 thoughts on “Calling Oracle Database REST APIs using Python

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s