generated from ibm-developer-skills-network/coding-project-template
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathrestapis.py
55 lines (48 loc) · 1.68 KB
/
restapis.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
# Uncomment the imports below before you add the function code
import requests
import os
from dotenv import load_dotenv
load_dotenv()
backend_url = os.getenv(
'backend_url', default="http://localhost:3030")
sentiment_analyzer_url = os.getenv(
'sentiment_analyzer_url',
default="http://localhost:5050/")
# def get_request(endpoint, **kwargs):
def get_request(endpoint, **kwargs):
params = ""
if(kwargs):
for key,value in kwargs.items():
params=params+key+"="+value+"&"
request_url = backend_url+endpoint+"?"+params
print("GET from {} ".format(request_url))
try:
# Call get method of requests library with URL and parameters
response = requests.get(request_url)
return response.json()
except:
# If any error occurs
print("Network exception occurred")
# Add code for get requests to back end
# def analyze_review_sentiments(text):
def analyze_review_sentiments(text):
request_url = sentiment_analyzer_url+"analyze/"+text
try:
# Call get method of requests library with URL and parameters
response = requests.get(request_url)
return response.json()
except Exception as err:
print(f"Unexpected {err=}, {type(err)=}")
print("Network exception occurred")
# request_url = sentiment_analyzer_url+"analyze/"+text
# Add code for retrieving sentiments
# def post_review(data_dict):
def post_review(data_dict):
request_url = backend_url+"/insert_review"
try:
response = requests.post(request_url,json=data_dict)
print(response.json())
return response.json()
except:
print("Network exception occurred")
# Add code for posting review