# Standard library imports
import collections

# Local imports.
import uplink

# Constants
BASE_URL = "https://api.github.com/"

# Schemas
Repo = collections.namedtuple("Repo", "owner name")

# Converters


@uplink.loads.from_json(Repo)
def repo_loader(cls, json):
    return cls(**json)


# Service


class GitHub(uplink.Consumer):
    @uplink.returns.from_json(type=Repo)
    @uplink.get("/users/{user}/repos/{repo}")
    def get_repo(self, user, repo):
        pass

    @uplink.returns.from_json(type=uplink.types.List[Repo], key="data")
    @uplink.get("/users/{user}/repos")
    def get_repos(self, user):
        pass


# Tests


def test_returns_json_with_type(mock_client, mock_response):
    # Setup
    mock_response.with_json({"owner": "prkumar", "name": "uplink"})
    mock_client.with_response(mock_response)
    github = GitHub(
        base_url=BASE_URL, client=mock_client, converters=repo_loader
    )

    # Run
    repo = github.get_repo("prkumar", "uplink")

    # Verify
    assert Repo(owner="prkumar", name="uplink") == repo


def test_returns_json_with_list(mock_client, mock_response):
    # Setup
    mock_response.with_json(
        {
            "data": [
                {"owner": "prkumar", "name": "uplink"},
                {"owner": "prkumar", "name": "uplink-protobuf"},
            ],
            "errors": [],
        }
    )
    mock_client.with_response(mock_response)
    github = GitHub(
        base_url=BASE_URL, client=mock_client, converters=repo_loader
    )

    # Run
    repo = github.get_repos("prkumar")

    # Verify
    assert [
        Repo(owner="prkumar", name="uplink"),
        Repo(owner="prkumar", name="uplink-protobuf"),
    ] == repo

@@ -283,16 +283,124 @@
 json)%0A%0A%0A
+@uplink.dumps.to_json(Repo)%0Adef repo_dumper(_, repo):%0A    return %7B%22owner%22: repo.owner, %22name%22: repo.name%7D%0A%0A%0A
 # Servic
@@ -727,16 +727,160 @@
   pass%0A%0A
+    @uplink.json%0A    @uplink.post(%22/users/%7Buser%7D/repos%22, args=%7B%22repo%22: uplink.Body(Repo)%7D)%0A    def create_repo(self, user, repo):%0A        pass%0A%0A
 %0A# Tests
@@ -1934,16 +1934,335 @@
 ,%0A    %5D == repo%0A
+%0A%0Adef test_post_json(mock_client):%0A    # Setup%0A    github = GitHub(%0A        base_url=BASE_URL, client=mock_client, converters=repo_dumper%0A    )%0A    github.create_repo(%22prkumar%22, Repo(owner=%22prkumar%22, name=%22uplink%22))%0A    request = mock_client.history%5B0%5D%0A    assert request.json == %7B%22owner%22: %22prkumar%22, %22name%22: %22uplink%22%7D%0A

Add integration test for @json