22import sys
33
44import aioredis
5- from sanic import Sanic , Blueprint
6- from sanic import response
5+ from sanic import Sanic , Blueprint , request , response
76
87import logging
98
109sys .path .insert (0 , os .path .abspath (os .path .join (os .path .dirname (__file__ ), '..' )))
1110
11+ from fdk_client .platform import PlatformClient
1212from fdk_extension import setup_fdk
13- from examples .extension_handlers import extension_handler
1413from fdk_extension .utilities .logger import get_logger
1514from fdk_extension .storage .redis_storage import RedisStorage
1615
16+ from examples .extension_handlers import extension_handler
17+
1718logger = get_logger ()
1819
1920
2021app = Sanic ("test" )
2122
2223redis_connection = aioredis .from_url ("redis://localhost" )
2324
24- base_url = "http://0.0.0.0:8000"
25-
2625
26+ # webhook handlers
2727async def handle_coupon_edit (event_name , payload , company_id , application_id ):
2828 logging .debug (f"Event received for { company_id } and { application_id } " )
2929 logging .debug (payload )
@@ -45,21 +45,14 @@ async def handle_location_event(event_name, payload, company_id):
4545 logging .debug (payload )
4646
4747
48- async def handle_ext_install (payload , company_id ):
49- logging .debug (f"Event received for { company_id } " )
50- logging .debug (payload )
51-
52-
5348fdk_extension_client = setup_fdk ({
54- "api_key" : "6220daa4a5414621b975a41f" ,
55- "api_secret" : "EbeGBRC~Fthv5om" ,
56- "base_url" : base_url , # this is optional
57- "scopes" : ["company/product" ], # this is optional
49+ "api_key" : "<API_KEY>" ,
50+ "api_secret" : "<API_SECRET>" ,
5851 "callbacks" : extension_handler ,
59- "storage" : RedisStorage (redis_connection ),
52+ "storage" : RedisStorage (redis_connection , "example_app" ),
6053 "access_mode" : "offline" ,
6154 "debug" : True ,
62- "cluster" : "https://api.fyndx0.de" , # this is optional by default it points to prod.
55+ "cluster" : "https://api.fynd.com" ,
6356 "webhook_config" : {
6457 "api_path" : "/webhook" ,
6558 "notification_email" : "test2@abc.com" , # required
@@ -129,14 +122,23 @@ async def disable_sales_channel_webhook_handler(request, application_id):
129122 return response .json ({"error_message" : str (e ), "success" : False }, 500 )
130123
131124
125+ # Adding `fdk_route` to handle extension install/auth
132126app .blueprint (fdk_extension_client .fdk_route )
133127
134- platform_bp = Blueprint ("platform blueprint" )
128+
129+ # platform blueprints
130+ platform_bp = Blueprint ("platform_blueprint" )
135131platform_bp .add_route (test_route_handler , "/test/routes" )
132+ platform_bp_group = Blueprint .group (platform_bp )
136133
137- application_bp = Blueprint ("application blueprint" )
134+
135+ # application blueprints
136+ application_bp = Blueprint ("application_blueprint" )
138137application_bp .add_route (test_route_handler , "/1234" )
138+ application_bp_group = Blueprint .group (application_bp )
139+
139140
141+ # webhook handler
140142app .add_route (webhook_handler , "/webhook" , methods = ["POST" ])
141143
142144platform_bp .add_route (enable_sales_channel_webhook_handler ,
@@ -146,12 +148,33 @@ async def disable_sales_channel_webhook_handler(request, application_id):
146148 "/webhook/application/<application_id>/unsubscribe" ,
147149 methods = ["POST" ])
148150
149- fdk_extension_client .platform_api_routes .append (platform_bp )
150- fdk_extension_client .application_proxy_routes .append (application_bp )
151+ # registering blueprints
152+ fdk_extension_client .platform_api_routes .append (platform_bp_group )
153+ fdk_extension_client .application_proxy_routes .append (application_bp_group )
154+
151155
152156app .blueprint (fdk_extension_client .platform_api_routes )
153157app .blueprint (fdk_extension_client .application_proxy_routes )
154158
159+
160+ # Example of How to use platform client in `offline` access mode
161+ @app .post ("/client" )
162+ async def client_handler (request : request .Request ):
163+ try :
164+ client : PlatformClient = await fdk_extension_client .get_platform_client ("<COMPANY_ID>" )
165+ res = await client .application ("<APPLICATION_ID>" ).theme .getAppliedTheme ()
166+ return response .json (body = {"message" : "OK" }, status = 200 )
167+ except Exception as e :
168+ print (e )
169+ return response .json (body = {"message" : "Error" }, status = 400 )
170+
171+
172+ # home page
173+ @app .get ("company/<company_id>" )
174+ async def home_page_handler (request : request .Request , company_id ):
175+ return response .html (f"<h1>Extension launched on Company ID: { company_id } </h1>" )
176+
177+
155178# debug logs enabled with debug = True
156179if __name__ == '__main__' :
157- app .run (host = "0 .0.0.0 " , port = 8000 , debug = True )
180+ app .run (host = "127 .0.0.1 " , port = 8000 , debug = True )
0 commit comments