Skip to content

Commit

Permalink
feat: add get_username function (#27)
Browse files Browse the repository at this point in the history
* chore: added url for get_username function

* feat: added get_username function

* test: added test for get_username function

* chore: updated README.md

* chore: added url for get_username function

* feat: added get_username function

* test: added test for get_username function

* chore: updated README.md
  • Loading branch information
Lucino772 authored Feb 10, 2022
1 parent 84a381e commit 5d196a0
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Checkout the [documentation](https://pymojang.readthedocs.io/en/latest/)
New features:
- [x] `get_sales`: This function retrieve sales statistics
- [x] `get_blocked_servers` : This function retrieve blocked server hashes
- [ ] `get_username` : This function return the username for a given uuid
- [x] `get_username` : This function return the username for a given uuid

The following functions are going to be renamed:
- [x] `status` → `get_status`
Expand Down
1 change: 1 addition & 0 deletions mojang/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from .api import (
get_uuid,
get_uuids,
get_username,
get_names,
get_status,
get_blocked_servers,
Expand Down
1 change: 1 addition & 0 deletions mojang/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
get_blocked_servers,
get_uuid,
get_uuids,
get_username,
get_names,
get_profile,
)
Expand Down
20 changes: 20 additions & 0 deletions mojang/api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,26 @@ def get_uuids(usernames: Iterable[str]) -> Dict[str, Optional[str]]:
return ret


def get_username(uuid: str) -> Optional[str]:
"""Get username for a uuid
:param uuid str: The uuid you want the username of
:Example:
>>> import mojang
>>> mojang.get_username('069a79f444e94726a5befca90e38aaf5')
'Notch'
"""
response = requests.get(urls.api_get_username(uuid))
code, data = helpers.err_check(response, (400, ValueError))

if code == 204:
return None

return data["name"]


def get_names(uuid: str) -> List[NameInfo]:
"""Get the user's name history
Expand Down
1 change: 1 addition & 0 deletions mojang/api/utils/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
lambda username: f"https://api.mojang.com/users/profiles/minecraft/{username}"
)
api_get_uuids = "https://api.mojang.com/profiles/minecraft"
api_get_username = lambda uuid: f"https://api.mojang.com/user/profile/{uuid}"
api_name_history = (
lambda uuid: f"https://api.mojang.com/user/profiles/{uuid}/names"
)
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ versionfile_build = mojang/_version.py
tag_prefix = v

[flake8]
ignore = E203, E266, E501, W503, F403, F401
ignore = E203, E266, E501, W503, F403, F401, E731
max-line-length = 79
select = B,C,E,F,W,T4,B9
exclude = docs/*,versioneer.py,_version.py
Expand Down
18 changes: 18 additions & 0 deletions tests/test_mojang_username.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import unittest

import mojang


class TestMojangUsername(unittest.TestCase):
def setUp(self) -> None:
self.notch = mojang.get_username("069a79f444e94726a5befca90e38aaf5")
self.unkown = mojang.get_username("069a79f444e94726a5befca90e38aaf6")

def test_know_uuid(self):
self.assertEqual(self.notch, "Notch")

def test_unkown_uuid(self):
self.assertEqual(self.unkown, None)

def test_invalid_uuid(self):
self.assertRaises(ValueError, mojang.get_username, ["thisisnotauuid"])

0 comments on commit 5d196a0

Please sign in to comment.