<commit_before>from .client import client
from .errors import JujuError
from juju import jasyncio

import requests
import json


class CharmHub:
    def __init__(self, model):
        self.model = model

    def request_charmhub_with_retry(self, url, retries):
        for attempt in range(retries):
            _response = requests.get(url)
            if _response.status_code == 200:
                return _response
            jasyncio.sleep(5)
        raise JujuError("Got {} from {}".format(_response.status_code, url))

    async def get_charm_id(self, charm_name):
        conn, headers, path_prefix = self.model.connection().https_connection()

        model_conf = await self.model.get_config()
        charmhub_url = model_conf['charmhub-url']
        url = "{}/v2/charms/info/{}".format(charmhub_url.value, charm_name)
        _response = self.request_charmhub_with_retry(url, 5)
        response = json.loads(_response.text)
        return response['id'], response['name']

    async def is_subordinate(self, charm_name):
        conn, headers, path_prefix = self.model.connection().https_connection()

        model_conf = await self.model.get_config()
        charmhub_url = model_conf['charmhub-url']
        url = "{}/v2/charms/info/{}?fields=default-release.revision.subordinate".format(charmhub_url.value, charm_name)
        _response = self.request_charmhub_with_retry(url, 5)
        response = json.loads(_response.text)
        return 'subordinate' in response['default-release']['revision']

    # TODO (caner) : we should be able to recreate the channel-map through the
    #  api call without needing the CharmHub facade
<commit_msg>Add list_resources method for charmhub module<commit_after>from .client import client
from .errors import JujuError
from juju import jasyncio

import requests
import json


class CharmHub:
    def __init__(self, model):
        self.model = model

    def request_charmhub_with_retry(self, url, retries):
        for attempt in range(retries):
            _response = requests.get(url)
            if _response.status_code == 200:
                return _response
            jasyncio.sleep(5)
        raise JujuError("Got {} from {}".format(_response.status_code, url))

    async def get_charm_id(self, charm_name):
        conn, headers, path_prefix = self.model.connection().https_connection()

        model_conf = await self.model.get_config()
        charmhub_url = model_conf['charmhub-url']
        url = "{}/v2/charms/info/{}".format(charmhub_url.value, charm_name)
        _response = self.request_charmhub_with_retry(url, 5)
        response = json.loads(_response.text)
        return response['id'], response['name']

    async def is_subordinate(self, charm_name):
        conn, headers, path_prefix = self.model.connection().https_connection()

        model_conf = await self.model.get_config()
        charmhub_url = model_conf['charmhub-url']
        url = "{}/v2/charms/info/{}?fields=default-release.revision.subordinate".format(charmhub_url.value, charm_name)
        _response = self.request_charmhub_with_retry(url, 5)
        response = json.loads(_response.text)
        return 'subordinate' in response['default-release']['revision']

    # TODO (caner) : we should be able to recreate the channel-map through the
    #  api call without needing the CharmHub facade

    async def list_resources(self, charm_name):
        conn, headers, path_prefix = self.model.connection().https_connection()

        model_conf = await self.model.get_config()
        charmhub_url = model_conf['charmhub-url']
        url = "{}/v2/charms/info/{}?fields=default-release.resources".format(charmhub_url.value, charm_name)
        _response = self.request_charmhub_with_retry(url, 5)
        response = json.loads(_response.text)
        return response['default-release']['resources']

{'fraction_non_alphanumeric': 0.10837438423645321, 'fraction_numerical': 0.00466683951257454, 'mean_word_length': 3.253583241455347, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '60692', 'n_tokens_mistral': 1176, 'n_tokens_neox': 1149, 'n_words': 252}