
Type: OAuth2.0
Grant Type: Client Credentials
Access Token URL: Enter tokenurl from previous section
Client ID: Enter clientId from previous section
Client Secret: Enter clientSecret from previous section
Scope: blank
Client Authentication: Send as Basic Auth header
Enter the "url" from the JSON generated in the previous section and append "/systems"
import os
import requests
from flask import Flask, jsonify, request, render_template
import json
from tabulate import tabulate
app = Flask(__name__)
port = int(os.environ.get('PORT', 3000))
# OAuth 2 client configuration
client_id = "Enter your clientId"
client_secret = "Enter your clientSecret"
token_url = 'Enter tokenUrl from LaMa Cloud API Config'
api_url = '<Enter url from LaMa Cloud API Config>/systems'
# Get access token using client credentials
def get_access_token():
payload = {
'grant_type': 'client_credentials',
'client_id': client_id,
'client_secret': client_secret
}
response = requests.post(token_url, data=payload)
access_token = response.json()['access_token']
return access_token
# Extract "name" entries from JSON data and reformat
def extract_names(data):
names = []
if isinstance(data, dict):
if "group" in data:
del data["group"] # Exclude the "group" section
for key, value in data.items():
if key == "name":
names.append(value)
elif isinstance(value, (dict, list)):
names.extend(extract_names(value))
elif isinstance(data, list):
for item in data:
names.extend(extract_names(item))
names.sort()
return names
@app.route("/")
def hello_world():
return "Hello, World!"
@app.route('/api/v1/systems', methods=['GET'])
def get_data():
access_token = get_access_token()
headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json'
}
response = requests.get(api_url, headers=headers)
data = response.json()
names = extract_names(data)
# Create a dictionary
sid_components = {}
for name in names:
parts = name.split(":")
if len(parts) > 1:
sid = parts[0].strip()
component = parts[1].strip()
if sid in sid_components:
sid_components[sid].append(component)
else:
sid_components[sid] = [component]
# Create a list of lists containing "SID" and "COMPONENTS" columns
table_data = [["SID", "COMPONENTS"]]
for sid, components in sid_components.items():
table_data.append([sid, ', '.join(components)])
# Generate the HTML table with borders
table_html = tabulate(table_data, headers="firstrow", tablefmt="html")
# Render the template with the table
return render_template('table.html', table_html=table_html)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=port)
<!DOCTYPE html>
<html>
<head>
<title>GET Call to LaMa Cloud</title>
<style>
table {
border-collapse: collapse;
}
th, td {
border: 1px solid black;
padding: 8px;
}
</style>
</head>
<body>
<h1>GET Call to LaMa Cloud</h1>
{{ table_html | safe }}
</body>
</html>
% pip install flask
% pip install tabulate
% python server.py
% pip install virtualenv
---
applications:
- name: myapp
random-route: true
path: ./
memory: 128M
buildpack: python_buildpack
command: python server.py
Flask==2.0.1
flask-restful
flask
requests
tabulate
python-3.10.11
% cf api https://api.cf.us10-001.hana.ondemand.com (change to your own region URL)
% cf login
% cf push
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
24 | |
20 | |
20 | |
19 | |
14 | |
12 | |
10 | |
9 | |
7 | |
7 |