|
| 1 | +import json |
1 | 2 | import logging
|
2 | 3 |
|
| 4 | +import boto3 as boto3 |
3 | 5 | import requests
|
4 | 6 |
|
5 |
| -from settings import MARKETPLACE_CHANNEL_USER_URL |
| 7 | +from settings import MARKETPLACE_CHANNEL_USER_URL, CONTRACT_API_ARN, CONTRACT_API_STAGE |
6 | 8 | from storage import DatabaseStorage
|
7 | 9 | from utils import is_free_call
|
8 | 10 |
|
@@ -32,22 +34,31 @@ def save_usage_details(self, usage_details_dict):
|
32 | 34 | if is_free_call(usage_details_dict):
|
33 | 35 | channel_id = usage_details_dict['channel_id']
|
34 | 36 | group_id = usage_details_dict['group_id']
|
35 |
| - username = APIUtilityService().get_user_name(channel_id, group_id) |
36 |
| - usage_details_dict['username'] = username |
| 37 | + user_address = APIUtilityService().get_user_address(group_id, channel_id) |
| 38 | + usage_details_dict['user_address'] = user_address |
37 | 39 | self.storage_service.add_usage_data(usage_details_dict)
|
38 | 40 | return
|
39 | 41 |
|
40 | 42 |
|
41 | 43 | class APIUtilityService:
|
| 44 | + lambda_client = boto3.client('lambda') |
42 | 45 |
|
43 |
| - @staticmethod |
44 |
| - def get_user_name(channel_id, group_id): |
45 |
| - url = MARKETPLACE_CHANNEL_USER_URL.format(group_id, channel_id) |
46 |
| - response = requests.get(url) |
47 |
| - user_data = response.json() |
| 46 | + def get_user_address(self, group_id, channel_id): |
| 47 | + lambda_payload = { |
| 48 | + "httpMethod": "GET", |
| 49 | + "requestContext": {"stage": CONTRACT_API_STAGE}, |
| 50 | + "path": f"/contract-api/group/{group_id}/channel/{channel_id}" |
| 51 | + } |
| 52 | + response = self.lambda_client.invoke( |
| 53 | + FunctionName=CONTRACT_API_ARN, |
| 54 | + Payload=json.dumps(lambda_payload) |
| 55 | + ) |
| 56 | + response_body_raw = json.loads( |
| 57 | + response.get('Payload').read())['body'] |
| 58 | + response_body = json.loads(response_body_raw) |
48 | 59 | try:
|
49 |
| - username = user_data[0]['username'] |
| 60 | + user_address = response_body['data'][0]['sender'] |
50 | 61 | except Exception as e:
|
51 | 62 | print(e)
|
52 |
| - raise Exception("Failed to get username from marketplace") |
53 |
| - return username |
| 63 | + raise Exception("Failed to get user address from marketplace") |
| 64 | + return user_address |
0 commit comments