-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4-4RESTProxy.py
78 lines (54 loc) · 2.14 KB
/
4-4RESTProxy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# Please complete the TODO items in the code
import json
import requests
REST_PROXY_URL = "http://localhost:8082"
def get_topics():
"""Gets topics from REST Proxy"""
# TODO: See: https://docs.confluent.io/current/kafka-rest/api.html#get--topics
resp = requests.get(f"{REST_PROXY_URL}/topics") # TODO
try:
resp.raise_for_status()
except:
print("Failed to get topics {json.dumps(resp.json(), indent=2)})")
return []
print("Fetched topics from Kafka:")
print(json.dumps(resp.json(), indent=2))
return resp.json()
def get_topic(topic_name):
"""Get specific details on a topic"""
# TODO: See: https://docs.confluent.io/current/kafka-rest/api.html#get--topics
resp = requests.get(f"{REST_PROXY_URL}/topics/{topic_name}") # TODO
try:
resp.raise_for_status()
except:
print("Failed to get topics {json.dumps(resp.json(), indent=2)})")
print("Fetched topics from Kafka:")
print(json.dumps(resp.json(), indent=2))
def get_brokers():
"""Gets broker information"""
# TODO See: https://docs.confluent.io/current/kafka-rest/api.html#get--brokers
resp = requests.get(f"{REST_PROXY_URL}/brokers") # TODO
try:
resp.raise_for_status()
except:
print("Failed to get brokers {json.dumps(resp.json(), indent=2)})")
print("Fetched brokers from Kafka:")
print(json.dumps(resp.json(), indent=2))
def get_partitions(topic_name):
resp = requests.get(f"{REST_PROXY_URL}/topics/{topic_name}/partitions") # TODO
"""Prints partition information for a topic"""
# TODO: Using the above endpoints as an example, list
# partitions for a given topic name using the API
#
# See: https://docs.confluent.io/current/kafka-rest/api.html#get--topics-(string-topic_name)-partitions
try:
resp.raise_for_status()
except:
print("Failed to get partitions {json.dumps(resp.json(), indent=2)})")
print("Fetched brokers from Kafka:")
print(json.dumps(resp.json(), indent=2))
if __name__ == "__main__":
topics = get_topics()
get_topic(topics[0])
get_brokers()
get_partitions(topics[-1])