Skip to content

Commit

Permalink
Merge pull request #162 from facundobatista/v2api-improvements
Browse files Browse the repository at this point in the history
Used external URLs and better format for datetime.
  • Loading branch information
facundobatista authored Aug 24, 2024
2 parents 90cf6e9 + 166f68c commit b89abcf
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 33 deletions.
2 changes: 1 addition & 1 deletion kilink/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def build_tree_from_root_id(self, linkode_id):

for treenode in self._get_kilink_tree(linkode_id):
node_dict = {
'timestamp': str(treenode.timestamp),
'timestamp': treenode.timestamp.isoformat(),
'linkode_id': treenode.linkode_id,
'parent': treenode.parent,
}
Expand Down
32 changes: 16 additions & 16 deletions kilink/templates/_tools.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ <h4 id="api-create">To create a new linkode</h4>
> "https://linkode.org/api/2/linkode"
{
"linkode_id": "SrDotVY03btcJBxSAtINC6",
"linkode_url": "/api/2/linkode/SrDotVY03btcJBxSAtINC6",
"linkode_url": "https://linkode.org/api/2/linkode/SrDotVY03btcJBxSAtINC6",
"root_id": "SrDotVY03btcJBxSAtINC6",
"root_url": "/api/2/linkode/SrDotVY03btcJBxSAtINC6"
"root_url": "https://linkode.org/api/2/linkode/SrDotVY03btcJBxSAtINC6"
}
</pre>
</div>
Expand Down Expand Up @@ -134,9 +134,9 @@ <h4 id="api-createchild">To create a child of an existing linkode node</h4>
> "https://linkode.org/api/2/linkode/SrDotVY03btcJBxSAtINC6/"
{
"linkode_id": "x1F110pkRqWclAhXVcqtD1",
"linkode_url": "/api/2/linkode/x1F110pkRqWclAhXVcqtD1",
"linkode_url": "https://linkode.org/api/2/linkode/x1F110pkRqWclAhXVcqtD1",
"root_id": "SrDotVY03btcJBxSAtINC6",
"root_url": "/api/2/linkode/SrDotVY03btcJBxSAtINC6"
"root_url": "https://linkode.org/api/2/linkode/SrDotVY03btcJBxSAtINC6"
}
$
$ # a children of the just created node
Expand All @@ -145,9 +145,9 @@ <h4 id="api-createchild">To create a child of an existing linkode node</h4>
> "https://linkode.org/api/2/linkode/x1F110pkRqWclAhXVcqtD1/"
{
"linkode_id": "ww9RtsVzx6srGGjYmlpiY5",
"linkode_url": "/api/2/linkode/ww9RtsVzx6srGGjYmlpiY5",
"linkode_url": "https://linkode.org/api/2/linkode/ww9RtsVzx6srGGjYmlpiY5",
"root_id": "SrDotVY03btcJBxSAtINC6",
"root_url": "/api/2/linkode/SrDotVY03btcJBxSAtINC6"
"root_url": "https://linkode.org/api/2/linkode/SrDotVY03btcJBxSAtINC6"
</pre>
</div>
<div class="tab-pane" id="tab29">
Expand Down Expand Up @@ -192,19 +192,19 @@ <h4 id="api-get">To get a specific linkode node</h4>
'root_id': '7bxvJqtvFIggJnyNXba1K4',
'root_url': '/api/2/linkode/7bxvJqtvFIggJnyNXba1K4',
'text_type': 'python',
'timestamp': 'Thu, 22 Aug 2024 19:57:22 GMT'}
'timestamp': '2024-08-22T19:57:22.244297'}
</pre>
</div>
<div class="tab-pane" id="tab32">
<pre>
$ curl -s "https://linkode.org/api/2/linkode/x1F110pkRqWclAhXVcqtD1/"{
"content": "new content",
"linkode_id": "x1F110pkRqWclAhXVcqtD1",
"linkode_url": "/api/2/linkode/x1F110pkRqWclAhXVcqtD1",
"linkode_url": "https://linkode.org/api/2/linkode/x1F110pkRqWclAhXVcqtD1",
"root_id": "SrDotVY03btcJBxSAtINC6",
"root_url": "/api/2/linkode/SrDotVY03btcJBxSAtINC6",
"root_url": "https://linkode.org/api/2/linkode/SrDotVY03btcJBxSAtINC6",
"text_type": "python",
"timestamp": "Thu, 22 Aug 2024 20:12:38 GMT"
"timestamp": "2024-08-22T20:12:38.023853"
}
</pre>
</div>
Expand Down Expand Up @@ -243,12 +243,12 @@ <h4 id="api-gettree">To get all the tree of a linkode</h4>
200
>>> response.json()
{'linkode_id': '7bxvJqtvFIggJnyNXba1K4',
'timestamp': '2024-08-22 12:31:53.237094',
'timestamp': '2024-08-22T12:31:53.237094',
'children': [{'children': [{'children': [],
'linkode_id': 'dkaj4P3pJrdpMZomSKuQb7',
'timestamp': '2024-08-22 19:57:54.454925'}],
'timestamp': '2024-08-22T19:57:54.454925'}],
'linkode_id': 'TRgCnj5fXrcEC66dALDxt7',
'timestamp': '2024-08-22 19:57:22.270264'}]}
'timestamp': '2024-08-22T19:57:22.270264'}]}
</pre>
</div>
<div class="tab-pane" id="tab42">
Expand All @@ -261,15 +261,15 @@ <h4 id="api-gettree">To get all the tree of a linkode</h4>
{
"children": [],
"linkode_id": "ww9RtsVzx6srGGjYmlpiY5",
"timestamp": "2024-08-22 20:13:24.673839"
"timestamp": "2024-08-22T20:13:24.673839"
}
],
"linkode_id": "x1F110pkRqWclAhXVcqtD1",
"timestamp": "2024-08-22 20:12:38.499254"
"timestamp": "2024-08-22T20:12:38.499254"
}
],
"linkode_id": "SrDotVY03btcJBxSAtINC6",
"timestamp": "2024-08-22 12:49:29.845979"
"timestamp": "2024-08-22T12:49:29.845979"
}
</pre>
</div>
Expand Down
18 changes: 13 additions & 5 deletions kilink/views_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
linkode_v2 = Blueprint("linkode_v2", __name__)


def external_url_for(*args, **kwargs):
"""Provide the full url (including host) for some params.
Always use this one, not 'url_for' directly.
"""
return url_for(*args, **kwargs, _external=True)


@linkode_v2.route('/linkode/', methods=['POST'])
@linkode_v2.route('/linkode/<linkode_id>/', methods=['POST'])
def create_linkode(linkode_id=None):
Expand Down Expand Up @@ -66,9 +74,9 @@ def create_linkode(linkode_id=None):

ret_json = jsonify(
linkode_id=linkode.linkode_id,
linkode_url=url_for("linkode_v2.get_linkode", linkode_id=linkode.linkode_id),
linkode_url=external_url_for("linkode_v2.get_linkode", linkode_id=linkode.linkode_id),
root_id=linkode.root,
root_url=url_for("linkode_v2.get_linkode", linkode_id=linkode.root),
root_url=external_url_for("linkode_v2.get_linkode", linkode_id=linkode.root),
)
logger.debug("API create done; linkode_id=%s", linkode.linkode_id)
return ret_json, HTTPStatus.CREATED
Expand Down Expand Up @@ -101,11 +109,11 @@ def get_linkode(linkode_id):
ret_json = jsonify(
content=linkode.content,
text_type=linkode.text_type,
timestamp=linkode.timestamp,
timestamp=linkode.timestamp.isoformat(),
linkode_id=linkode.linkode_id,
linkode_url=url_for("linkode_v2.get_linkode", linkode_id=linkode.linkode_id),
linkode_url=external_url_for("linkode_v2.get_linkode", linkode_id=linkode.linkode_id),
root_id=linkode.root,
root_url=url_for("linkode_v2.get_linkode", linkode_id=linkode.root),
root_url=external_url_for("linkode_v2.get_linkode", linkode_id=linkode.root),
)

return ret_json, HTTPStatus.OK
Expand Down
36 changes: 25 additions & 11 deletions tests/test_views_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ def test_creates_new_root_with_all_params(self):
assert response.status_code == 201

# created linkode is the root of its tree
assert response.json["linkode_id"] == response.json["root_id"]
linkode_id = response.json["linkode_id"]
root_id = response.json["root_id"]
assert linkode_id == root_id

# proper urls
assert response.json["linkode_url"] == f"http://localhost/api/2/linkode/{linkode_id}"
assert response.json["root_url"] == f"http://localhost/api/2/linkode/{root_id}"

def test_creates_new_root_without_text_type(self):
payload = {
Expand All @@ -56,9 +62,15 @@ def test_creates_new_revision(self):

assert response.status_code == 201

# new linkoded created with id different than its parent
assert response.json["linkode_id"] != parent_id
assert response.json["root_id"] == parent_id
# new linkode created with id different than its parent
linkode_id = response.json["linkode_id"]
root_id = response.json["root_id"]
assert linkode_id != parent_id
assert root_id == parent_id

# proper urls
assert response.json["linkode_url"] == f"http://localhost/api/2/linkode/{linkode_id}"
assert response.json["root_url"] == f"http://localhost/api/2/linkode/{root_id}"

def test_creates_new_revision_when_parent_not_found(self):
response = test_client.post(
Expand Down Expand Up @@ -94,9 +106,11 @@ def test_get_linkode_happy_path(self):
assert response.json["text_type"] == "python"
assert response.json["linkode_id"] == linkode.linkode_id
assert response.json["root_id"] == linkode.root
assert "timestamp" in response.json
assert "linkode_url" in response.json
assert "root_id" in response.json
assert response.json["timestamp"] == linkode.timestamp.isoformat()
assert (
response.json["linkode_url"] == f"http://localhost/api/2/linkode/{linkode.linkode_id}"
)
assert response.json["root_url"] == f"http://localhost/api/2/linkode/{linkode.root}"

def test_get_linkode_when_not_found(self):
response = test_client.get("/api/2/linkode/SomeUnexistingId/")
Expand All @@ -117,22 +131,22 @@ def test_get_tree_happy_path(self):

assert response.json == {
"linkode_id": parent.linkode_id,
"timestamp": str(parent.timestamp),
"timestamp": parent.timestamp.isoformat(),
"children": [
{
"linkode_id": child_1.linkode_id,
"timestamp": str(child_1.timestamp),
"timestamp": child_1.timestamp.isoformat(),
"children": [
{
"linkode_id": grandchild.linkode_id,
"timestamp": str(grandchild.timestamp),
"timestamp": grandchild.timestamp.isoformat(),
"children": []
}
],
},
{
"linkode_id": child_2.linkode_id,
"timestamp": str(child_2.timestamp),
"timestamp": child_2.timestamp.isoformat(),
"children": []
}
]
Expand Down

0 comments on commit b89abcf

Please sign in to comment.