import requests
import json


def _get_repo_contents(repo_url: str):
    """Returns a dictionary of repository contents via GitHub API.
    
    Args:
        repo_url (url): the url of a GitHub repository.
        
    Returns:
        dict: the parsed json as a dictionary.

    """

    # Flexibly drop the beginning of the repository url.
    url_tail_ = repo_url.split("github.com/")[1]

    # Get the repository owner and name/path.
    owner_, path_ = url_tail_.split("/")
    
    # Modify this string to access the repo contents via GitHub Contents API.
    contents_api_url_ = "https://api.github.com/repos/{owner}/{path}/contents"

    # Retrieve the JSON with requests.
    response_ = requests.get(contents_api_url_.format(owner=owner_, path=path_))

    # If the JSON was successfully retrieved, parse to a dict and return.
    if response_.ok:
        return json.loads(response_.text)
    else:
        # print("ERROR [{err}]: Repo content not retrieved for '{url}'".format(
        #     err=response_.status_code, 
        #     url=repo_url))
        raise(Exception)