Skip to content

Commit d784c81

Browse files
committed
user address api from dapp added
1 parent 7cd6ef0 commit d784c81

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

metering/services.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import json
12
import logging
23

4+
import boto3 as boto3
35
import requests
46

5-
from settings import MARKETPLACE_CHANNEL_USER_URL
7+
from settings import MARKETPLACE_CHANNEL_USER_URL, CONTRACT_API_ARN, CONTRACT_API_STAGE
68
from storage import DatabaseStorage
79
from utils import is_free_call
810

@@ -32,22 +34,31 @@ def save_usage_details(self, usage_details_dict):
3234
if is_free_call(usage_details_dict):
3335
channel_id = usage_details_dict['channel_id']
3436
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
3739
self.storage_service.add_usage_data(usage_details_dict)
3840
return
3941

4042

4143
class APIUtilityService:
44+
lambda_client = boto3.client('lambda')
4245

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)
4859
try:
49-
username = user_data[0]['username']
60+
user_address = response_body['data'][0]['sender']
5061
except Exception as e:
5162
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
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import unittest
2+
3+
from services import APIUtilityService
4+
5+
6+
class TestContractAPI(unittest.TestCase):
7+
def test_contract_api(self):
8+
APIUtilityService().get_user_address('m5FKWq4hW0foGW5qSbzGSjgZRuKs7A1ZwbIrJ9e96rc=', 0)

0 commit comments

Comments
 (0)