11from .client import client
22from .errors import JujuError
3+ from juju import jasyncio
34
45import requests
56import json
@@ -9,23 +10,27 @@ class CharmHub:
910 def __init__ (self , model ):
1011 self .model = model
1112
13+ def request_charmhub_with_retry (self , url , retries ):
14+ for attempt in range (retries ):
15+ _response = requests .get (url )
16+ if _response .status_code == 200 :
17+ return _response
18+ jasyncio .sleep (5 )
19+ raise JujuError ("Got {} from {}" .format (_response .status_code , url ))
20+
1221 def get_charm_id (self , charm_name ):
1322 conn , headers , path_prefix = self .model .connection ().https_connection ()
1423
1524 url = "http://api.snapcraft.io/v2/charms/info/{}" .format (charm_name )
16- _response = requests .get (url )
17- if not _response .status_code == 200 :
18- raise JujuError ("Got {} from {}" .format (_response .status_code , url ))
25+ _response = self .request_charmhub_with_retry (url , 5 )
1926 response = json .loads (_response .text )
2027 return response ['id' ], response ['name' ]
2128
2229 def is_subordinate (self , charm_name ):
2330 conn , headers , path_prefix = self .model .connection ().https_connection ()
2431
2532 url = "http://api.snapcraft.io/v2/charms/info/{}?fields=default-release.revision.subordinate" .format (charm_name )
26- _response = requests .get (url )
27- if not _response .status_code == 200 :
28- raise JujuError ("Got {} from {}" .format (_response .status_code , url ))
33+ _response = self .request_charmhub_with_retry (url , 5 )
2934 response = json .loads (_response .text )
3035 return 'subordinate' in response ['default-release' ]['revision' ]
3136
0 commit comments