Skip to content

Commit

Permalink
Add get public URL method for kv store
Browse files Browse the repository at this point in the history
  • Loading branch information
Akshay Avinash committed Oct 6, 2024
1 parent cc5af44 commit 1ffa698
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/crawlee/storages/_key_value_store.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import os
from typing import TYPE_CHECKING, Any, AsyncIterator, TypeVar, overload

from typing_extensions import override
Expand Down Expand Up @@ -157,3 +158,14 @@ async def set_value(
return await self._resource_client.delete_record(key)

return await self._resource_client.set_record(key, value, content_type)

def get_public_url(self, key: str) -> str:
"""Get the public URL for the given key.
Args:
key: Key of the record for which URL is required
Returns:
The public URL for the given key.
"""
name = self.name or self._configuration.default_key_value_store_id
return f'file://{os.getcwd()}/storage/key_value_stores/{name}/{key}'
7 changes: 7 additions & 0 deletions tests/unit/storages/test_key_value_store.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import os
from typing import AsyncGenerator

import pytest
Expand Down Expand Up @@ -100,3 +101,9 @@ async def test_static_get_set_value(key_value_store: KeyValueStore) -> None:
await key_value_store.set_value('test-static', 'static')
value = await key_value_store.get_value('test-static')
assert value == 'static'


async def test_static_get_public_url(key_value_store: KeyValueStore) -> None:
await key_value_store.set_value('test-static', 'static')
public_url = key_value_store.get_public_url('test-static')
assert public_url == f'file://{os.getcwd()}/storage/key_value_stores/default/test-static'

0 comments on commit 1ffa698

Please sign in to comment.