|
1 | | -from __future__ import annotations |
| 1 | +# from __future__ import annotations |
2 | 2 |
|
3 | | -from typing import TYPE_CHECKING, ClassVar |
| 3 | +# from typing import TYPE_CHECKING, ClassVar |
4 | 4 |
|
5 | | -import msgspec |
6 | | -from aiohttp import ClientSession |
| 5 | +# import msgspec |
| 6 | +# from aiohttp import ClientSession |
| 7 | +# from yarl import URL |
| 8 | +# from .library import Library |
7 | 9 |
|
8 | | -from .preset import PresetLibrary |
| 10 | +# if TYPE_CHECKING: |
| 11 | +# from aiohttp import ClientSession |
9 | 12 |
|
10 | | -if TYPE_CHECKING: |
11 | | - from aiohttp import ClientSession |
12 | 13 |
|
| 14 | +# class SearchHitHierarchy(msgspec.Struct): |
| 15 | +# lvl0: str | None |
| 16 | +# lvl1: str | None |
| 17 | +# lvl2: str | None |
| 18 | +# lvl3: str | None |
| 19 | +# lvl4: str | None |
| 20 | +# lvl5: str | None |
| 21 | +# lvl6: str | None |
13 | 22 |
|
14 | | -class SearchHitHierarchy(msgspec.Struct): |
15 | | - lvl0: str | None |
16 | | - lvl1: str | None |
17 | | - lvl2: str | None |
18 | | - lvl3: str | None |
19 | | - lvl4: str | None |
20 | | - lvl5: str | None |
21 | | - lvl6: str | None |
| 23 | +# def to_text(self) -> str: |
| 24 | +# return " - ".join( |
| 25 | +# [ |
| 26 | +# lvl |
| 27 | +# for lvl in reversed( |
| 28 | +# ( |
| 29 | +# self.lvl0, |
| 30 | +# self.lvl1, |
| 31 | +# self.lvl2, |
| 32 | +# self.lvl3, |
| 33 | +# self.lvl4, |
| 34 | +# self.lvl5, |
| 35 | +# self.lvl6, |
| 36 | +# ) |
| 37 | +# ) |
| 38 | +# if lvl is not None |
| 39 | +# ] |
| 40 | +# ) |
22 | 41 |
|
23 | | - def to_text(self) -> str: |
24 | | - return " - ".join( |
25 | | - [ |
26 | | - lvl |
27 | | - for lvl in reversed( |
28 | | - ( |
29 | | - self.lvl0, |
30 | | - self.lvl1, |
31 | | - self.lvl2, |
32 | | - self.lvl3, |
33 | | - self.lvl4, |
34 | | - self.lvl5, |
35 | | - self.lvl6, |
36 | | - ) |
37 | | - ) |
38 | | - if lvl is not None |
39 | | - ] |
40 | | - ) |
41 | 42 |
|
| 43 | +# class SearchHit(msgspec.Struct): |
| 44 | +# url: str |
| 45 | +# hierarchy: SearchHitHierarchy |
42 | 46 |
|
43 | | -class SearchHit(msgspec.Struct): |
44 | | - url: str |
45 | | - hierarchy: SearchHitHierarchy |
46 | 47 |
|
| 48 | +# class SearchResult(msgspec.Struct): |
| 49 | +# hits: list[SearchHit] |
47 | 50 |
|
48 | | -class SearchResult(msgspec.Struct): |
49 | | - hits: list[SearchHit] |
50 | 51 |
|
| 52 | +# class SearchResponse(msgspec.Struct): |
| 53 | +# results: list[SearchResult] |
51 | 54 |
|
52 | | -class SearchResponse(msgspec.Struct): |
53 | | - results: list[SearchResult] |
54 | 55 |
|
| 56 | +# response_decoder = msgspec.json.Decoder(type=SearchResponse) |
| 57 | +# Jsonable = dict[str, "Jsonable"] | list["Jsonable"] | int | str |
55 | 58 |
|
56 | | -response_decoder = msgspec.json.Decoder(type=SearchResponse) |
57 | | -Jsonable = dict[str, "Jsonable"] | list["Jsonable"] | int | str |
58 | 59 |
|
| 60 | +# class AlgoliaConfig(msgspec.Struct): |
| 61 | +# url: str |
| 62 | +# index_name: str |
| 63 | +# kwargs: dict[str, Jsonable] = {} |
59 | 64 |
|
60 | | -class AlgoliaConfig(msgspec.Struct): |
61 | | - url: str |
62 | | - index_name: str |
63 | | - kwargs: dict[str, Jsonable] = {} |
64 | 65 |
|
| 66 | +# class AlgoliaBase(Library): |
| 67 | +# is_api: ClassVar[bool] = True |
| 68 | +# algolia_config: ClassVar[AlgoliaConfig] |
65 | 69 |
|
66 | | -class AlgoliaBase(PresetLibrary): |
67 | | - is_preset: ClassVar[bool] = True |
68 | | - is_api: ClassVar[bool] = True |
69 | | - algolia_config: ClassVar[AlgoliaConfig] |
| 70 | +# def __init__(self, name: str, *, use_cache: bool) -> None: |
| 71 | +# super().__init__(name, URL(self.base_url), use_cache=False) |
70 | 72 |
|
71 | | - def __init__(self, name: str, *, use_cache: bool) -> None: |
72 | | - super().__init__(name, use_cache=False) |
| 73 | +# def __init_subclass__( |
| 74 | +# cls, |
| 75 | +# config: AlgoliaConfig, |
| 76 | +# base_url: str | None = None, |
| 77 | +# favicon_url: str | None = None, |
| 78 | +# ) -> None: |
| 79 | +# cls.algolia_config = config |
| 80 | +# return super().__init_subclass__(base_url, favicon_url) |
73 | 81 |
|
74 | | - def __init_subclass__( |
75 | | - cls, |
76 | | - config: AlgoliaConfig, |
77 | | - base_url: str | None = None, |
78 | | - favicon_url: str | None = None, |
79 | | - ) -> None: |
80 | | - cls.algolia_config = config |
81 | | - return super().__init_subclass__(base_url, favicon_url) |
| 82 | +# async def make_request(self, session: ClientSession, query: str) -> None: |
| 83 | +# payload = { |
| 84 | +# "requests": [ |
| 85 | +# { |
| 86 | +# "query": query, |
| 87 | +# "indexName": self.algolia_config.index_name, |
| 88 | +# } |
| 89 | +# | self.algolia_config.kwargs |
| 90 | +# ] |
| 91 | +# } |
| 92 | +# async with session.post(self.algolia_config.url, json=payload) as res: |
| 93 | +# raw = await res.content.read() |
82 | 94 |
|
83 | | - async def make_request(self, session: ClientSession, query: str) -> None: |
84 | | - payload = { |
85 | | - "requests": [ |
86 | | - { |
87 | | - "query": query, |
88 | | - "indexName": self.algolia_config.index_name, |
89 | | - } |
90 | | - | self.algolia_config.kwargs |
91 | | - ] |
92 | | - } |
93 | | - async with session.post(self.algolia_config.url, json=payload) as res: |
94 | | - raw = await res.content.read() |
| 95 | +# resp = response_decoder.decode(raw) |
95 | 96 |
|
96 | | - resp = response_decoder.decode(raw) |
| 97 | +# cache = {} |
97 | 98 |
|
98 | | - cache = {} |
| 99 | +# for result in resp.results: |
| 100 | +# for hit in result.hits: |
| 101 | +# cache[hit.hierarchy.to_text()] = hit.url |
99 | 102 |
|
100 | | - for result in resp.results: |
101 | | - for hit in result.hits: |
102 | | - cache[hit.hierarchy.to_text()] = hit.url |
103 | | - |
104 | | - self.cache = cache |
| 103 | +# self.cache = cache |
0 commit comments