@@ -6,6 +6,47 @@ class CharmHub:
66 def __init__ (self , model ):
77 self .model = model
88
9+ def request_charmhub_with_retry (self , url , retries ):
10+ for attempt in range (retries ):
11+ _response = requests .get (url )
12+ if _response .status_code == 200 :
13+ return _response
14+ jasyncio .sleep (5 )
15+ raise JujuError ("Got {} from {}" .format (_response .status_code , url ))
16+
17+ async def get_charm_id (self , charm_name ):
18+ conn , headers , path_prefix = self .model .connection ().https_connection ()
19+
20+ model_conf = await self .model .get_config ()
21+ charmhub_url = model_conf ['charmhub-url' ]
22+ url = "{}/v2/charms/info/{}" .format (charmhub_url .value , charm_name )
23+ _response = self .request_charmhub_with_retry (url , 5 )
24+ response = json .loads (_response .text )
25+ return response ['id' ], response ['name' ]
26+
27+ async def is_subordinate (self , charm_name ):
28+ conn , headers , path_prefix = self .model .connection ().https_connection ()
29+
30+ model_conf = await self .model .get_config ()
31+ charmhub_url = model_conf ['charmhub-url' ]
32+ url = "{}/v2/charms/info/{}?fields=default-release.revision.subordinate" .format (charmhub_url .value , charm_name )
33+ _response = self .request_charmhub_with_retry (url , 5 )
34+ response = json .loads (_response .text )
35+ return 'subordinate' in response ['default-release' ]['revision' ]
36+
37+ # TODO (caner) : we should be able to recreate the channel-map through the
38+ # api call without needing the CharmHub facade
39+
40+ async def list_resources (self , charm_name ):
41+ conn , headers , path_prefix = self .model .connection ().https_connection ()
42+
43+ model_conf = await self .model .get_config ()
44+ charmhub_url = model_conf ['charmhub-url' ]
45+ url = "{}/v2/charms/info/{}?fields=default-release.resources" .format (charmhub_url .value , charm_name )
46+ _response = self .request_charmhub_with_retry (url , 5 )
47+ response = json .loads (_response .text )
48+ return response ['default-release' ]['resources' ]
49+
950 async def info (self , name , channel = None ):
1051 """info displays detailed information about a CharmHub charm. The charm
1152 can be specified by the exact name.
0 commit comments