diff options
author | Charles Oliveira <charles.oliveira@linaro.org> | 2020-12-17 16:24:11 -0300 |
---|---|---|
committer | Charles Oliveira <charles.oliveira@linaro.org> | 2020-12-17 16:33:13 -0300 |
commit | e30b694d02010f990772301a384f4fb6b2fdbdfb (patch) | |
tree | 528140d54e9b2fd75f20a8f3e967eef895c014c9 | |
parent | 7c0723432f7de55ce34ca63db06eb5811255d1da (diff) |
utils: add getid utility
-rw-r--r-- | squad_client/utils.py | 10 | ||||
-rw-r--r-- | tests/test_utils.py | 6 |
2 files changed, 15 insertions, 1 deletions
diff --git a/squad_client/utils.py b/squad_client/utils.py index b218eea..906954b 100644 --- a/squad_client/utils.py +++ b/squad_client/utils.py @@ -1,4 +1,5 @@ import json +import re def first(_dict): @@ -32,3 +33,12 @@ def to_json(thing): def get_class_name(obj): return obj.__class__.__name__ + + +def getid(url): + matches = re.search(r'^.*/(\d+)/$', url) + try: + _id = int(matches.group(1)) + return _id + except ValueError: + return -1 diff --git a/tests/test_utils.py b/tests/test_utils.py index 9d34e9f..531fd28 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,5 +1,9 @@ from unittest import TestCase +from squad_client.utils import getid class UtilsTest(TestCase): - pass + + def test_getid(self): + url = 'https://some-squad-url.com/api/objects/42/' + self.assertEqual(42, getid(url)) |