|
3 | 3 | import os |
4 | 4 | from dotenv import load_dotenv |
5 | 5 |
|
| 6 | +# Load environment variables |
6 | 7 | load_dotenv() |
7 | 8 |
|
8 | | -backend_url = os.getenv( |
9 | | - 'backend_url', default="http://localhost:3030") |
| 9 | +backend_url = os.getenv('backend_url', default="http://localhost:3030") |
10 | 10 | sentiment_analyzer_url = os.getenv( |
11 | | - 'sentiment_analyzer_url', |
12 | | - default="http://localhost:5050/") |
| 11 | + 'sentiment_analyzer_url', default="http://localhost:5050/" |
| 12 | +) |
13 | 13 |
|
14 | 14 |
|
15 | | -# Add code for get requests to back end |
| 15 | +# Add code for GET requests to backend |
16 | 16 | def get_request(endpoint, **kwargs): |
17 | 17 | params = "" |
18 | | - if(kwargs): |
19 | | - for key,value in kwargs.items(): |
20 | | - params=params+key+"="+value+"&" |
| 18 | + if kwargs: |
| 19 | + for key, value in kwargs.items(): |
| 20 | + params = params + key + "=" + value + "&" |
21 | 21 |
|
22 | | - request_url = backend_url+endpoint+"?"+params |
| 22 | + request_url = backend_url + endpoint + "?" + params |
| 23 | + print(f"GET from {request_url}") |
23 | 24 |
|
24 | | - print("GET from {} ".format(request_url)) |
25 | 25 | try: |
26 | 26 | # Call get method of requests library with URL and parameters |
27 | 27 | response = requests.get(request_url) |
28 | 28 | return response.json() |
29 | | - except: |
30 | | - # If any error occurs |
31 | | - print("Network exception occurred") |
| 29 | + except Exception as err: |
| 30 | + # Handle network or JSON errors gracefully |
| 31 | + print(f"Network exception occurred: {err}") |
| 32 | + |
32 | 33 |
|
33 | 34 | def analyze_review_sentiments(text): |
34 | | - request_url = sentiment_analyzer_url+"analyze/"+text |
| 35 | + request_url = sentiment_analyzer_url + "analyze/" + text |
35 | 36 | try: |
36 | | - # Call get method of requests library with URL and parameters |
37 | 37 | response = requests.get(request_url) |
38 | 38 | print(response) |
39 | 39 | return response.json() |
40 | 40 | except Exception as err: |
41 | | - print(f"Unexpected {err=}, {type(err)=}") |
| 41 | + print(f"Unexpected error: {err} ({type(err)})") |
42 | 42 | print("Network exception occurred") |
43 | 43 |
|
| 44 | + |
44 | 45 | # Add code for posting review |
45 | 46 | def post_review(data_dict): |
46 | | - request_url = backend_url+"/insert_review" |
| 47 | + request_url = backend_url + "/insert_review" |
47 | 48 | try: |
48 | | - response = requests.post(request_url,json=data_dict) |
| 49 | + response = requests.post(request_url, json=data_dict) |
49 | 50 | print(response.json()) |
50 | 51 | return response.json() |
51 | | - except: |
52 | | - print("Network exception occurred") |
| 52 | + except Exception as err: |
| 53 | + print(f"Network exception occurred: {err}") |
0 commit comments