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
42 lines (36 loc) · 1.25 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
# Uncomment the imports below before you add the function code
import requests
import os
from dotenv import load_dotenv
from .restapis import get_request, analyze_review_sentiments, post_review
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):
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")
# def analyze_review_sentiments(text):
# request_url = sentiment_analyzer_url+"analyze/"+text
# Add code for retrieving sentiments
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")