Skip to content

Commit 08ac482

Browse files
authored
fix: monkey patch SQLiteDict in order to fix sqlite3.InterfaceError (#725)
1 parent f525803 commit 08ac482

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

airbyte_cdk/sources/streams/http/http_client.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,27 @@
5656
BODY_REQUEST_METHODS = ("GET", "POST", "PUT", "PATCH")
5757

5858

59+
def monkey_patched_get_item(self, key): # type: ignore # this interface is a copy/paste from the requests_cache lib
60+
"""
61+
con.execute can lead to `sqlite3.InterfaceError: bad parameter or other API misuse`. There was a fix implemented
62+
[here](https://github.com/requests-cache/requests-cache/commit/5ca6b9cdcb2797dd2fed485872110ccd72aee55d#diff-f43db4a5edf931647c32dec28ea7557aae4cae8444af4b26c8ecbe88d8c925aaL330-R332)
63+
but there is still no official releases of requests_cache that this is part of. Hence, we will monkeypatch it for now.
64+
"""
65+
with self.connection() as con:
66+
# Using placeholders here with python 3.12+ and concurrency results in the error:
67+
# sqlite3.InterfaceError: bad parameter or other API misuse
68+
cur = con.execute(f"SELECT value FROM {self.table_name} WHERE key='{key}'")
69+
row = cur.fetchone()
70+
cur.close()
71+
if not row:
72+
raise KeyError(key)
73+
74+
return self.deserialize(key, row[0])
75+
76+
77+
requests_cache.SQLiteDict.__getitem__ = monkey_patched_get_item # type: ignore # see the method doc for more information
78+
79+
5980
class MessageRepresentationAirbyteTracedErrors(AirbyteTracedException):
6081
"""
6182
Before the migration to the HttpClient in low-code, the exception raised was
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import requests_cache
2+
3+
4+
def test_assert_requests_cache_version():
5+
"""
6+
We need to be alerted once the requests_cache version is updated. The reason is that we monkey patch one of the
7+
method in order to fix a bug until a new version is released.
8+
9+
For more information about the reasons of this test, see monkey_patched_get_item in http_client.py
10+
"""
11+
assert requests_cache.__version__ == "1.2.1"

0 commit comments

Comments
 (0)