Last active
September 12, 2024 14:58
-
-
Save hkboujrida/dea256dd85fb5234c05d308b00bf0a57 to your computer and use it in GitHub Desktop.
extract.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# read a json local file from local disk and extract the build information | |
import json | |
import requests | |
json_url = 'http://localhost:8080/v1/rules' | |
# load json form url and extract the build information | |
json_file = requests.get(json_url).json() | |
# json_file = 'test.json' | |
# get base adress from json_url | |
base_address = json_url.split('/v1')[0] | |
apis = [] | |
# extract all the line starting with ""rule": "PathPrefixStrip: " like "rule": "PathPrefixStrip: /api/v1" and store it an array | |
with open(json_file) as f: | |
lines = f.readlines() | |
for line in lines: | |
if '"rule": "PathPrefixStrip: ' in line: | |
remove_prefix = line.replace('"rule": "PathPrefixStrip: ', '') | |
# remove empty space and new line | |
remove_prefix = remove_prefix.replace('"', '') | |
remove_prefix = remove_prefix.strip() | |
apis.append(remove_prefix) | |
print(apis) | |
# build a json array from the extracted array of object target | |
# target { | |
# name = "api1" # name of the api | |
# address = "${base_address}${path_prefix_strip[0]}" # address of the api | |
# module = "http_2xx" | |
# labels = { | |
# "name" = "api1" # name of the api | |
# } | |
json_array = [] | |
for api in apis: | |
json_array.append({ | |
"target": { | |
"name": api.split('/')[1], | |
"address": f"{base_address}{api}", | |
"module": "http_2xx", | |
"labels": { | |
"name": api.split('/')[1] | |
} | |
} | |
}) | |
print(json_array) | |
# save the json array to a file | |
with open('build.json', 'w') as f: | |
json.dump(json_array, f, indent=4) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apis = ["api1", "api2", "api3"] | |
base_address = "https://api.com" | |
# # build a a file from the extracted array of object target | |
# # target { | |
# # name = "api1" # name of the api | |
# # address = "${base_address}${api}" # address of the api | |
# # module = "http_2xx" | |
# # labels = { | |
# # "name" = "api1" # name of the api | |
# # } | |
for api in apis: | |
block = f""" | |
target {{ | |
name = "{api}" | |
address = "${base_address}${api}" | |
module = "http_2xx" | |
labels = {{ | |
"name" = "{api}" | |
}} | |
}}""" | |
# save to file to disk | |
with open("targets.txt", "a") as file: | |
file.write(block) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment