@@ -26,6 +26,7 @@ static size_t cacert_len = sizeof(cacert_pem) - 1;
2626
2727#ifndef DBMEM_OMIT_CURL
2828static size_t dbmem_remote_receive_data (void * contents , size_t size , size_t nmemb , void * xdata );
29+ static struct curl_slist * dbmem_remote_build_headers (const char * api_key );
2930#endif
3031
3132struct dbmem_remote_engine_t {
@@ -67,6 +68,27 @@ struct dbmem_remote_engine_t {
6768#include <stdbool.h>
6869#include <stddef.h>
6970
71+ #ifndef DBMEM_OMIT_CURL
72+ static struct curl_slist * dbmem_remote_build_headers (const char * api_key ) {
73+ char auth_header [512 ];
74+ struct curl_slist * headers = NULL ;
75+ struct curl_slist * next = NULL ;
76+
77+ snprintf (auth_header , sizeof (auth_header ), "Authorization: Bearer %s" , api_key );
78+ headers = curl_slist_append (headers , auth_header );
79+ if (!headers ) return NULL ;
80+
81+ next = curl_slist_append (headers , "Content-Type: application/json" );
82+ if (!next ) {
83+ curl_slist_free_all (headers );
84+ return NULL ;
85+ }
86+ headers = next ;
87+
88+ return headers ;
89+ }
90+ #endif
91+
7092static bool text_needs_json_escape (const char * text , size_t * len ) {
7193 size_t original_len = * len ;
7294 size_t required_len = 0 ;
@@ -263,11 +285,7 @@ dbmem_remote_engine_t *dbmem_remote_engine_init (void *ctx, const char *provider
263285 #endif
264286
265287 // set up headers
266- char auth_header [512 ];
267- snprintf (auth_header , sizeof (auth_header ), "Authorization: Bearer %s" , api_key );
268- struct curl_slist * headers = NULL ;
269- headers = curl_slist_append (headers , auth_header );
270- if (headers ) headers = curl_slist_append (headers , "Content-Type: application/json" );
288+ struct curl_slist * headers = dbmem_remote_build_headers (api_key );
271289 if (!headers ) {
272290 snprintf (err_msg , DBMEM_ERRBUF_SIZE , "Failed to allocate HTTP headers" );
273291 curl_easy_cleanup (curl );
@@ -522,6 +540,36 @@ int dbmem_remote_compute_embedding (dbmem_remote_engine_t *engine, const char *t
522540 return 0 ;
523541}
524542
543+ int dbmem_remote_engine_set_apikey (dbmem_remote_engine_t * engine , const char * api_key , char err_msg [DBMEM_ERRBUF_SIZE ]) {
544+ if (!engine || !api_key ) {
545+ if (err_msg ) snprintf (err_msg , DBMEM_ERRBUF_SIZE , "Invalid remote engine or API key" );
546+ return SQLITE_MISUSE ;
547+ }
548+
549+ #ifndef DBMEM_OMIT_CURL
550+ struct curl_slist * headers = dbmem_remote_build_headers (api_key );
551+ if (!headers ) {
552+ if (err_msg ) snprintf (err_msg , DBMEM_ERRBUF_SIZE , "Failed to allocate HTTP headers" );
553+ return SQLITE_NOMEM ;
554+ }
555+
556+ curl_easy_setopt (engine -> curl , CURLOPT_HTTPHEADER , headers );
557+ if (engine -> headers ) curl_slist_free_all (engine -> headers );
558+ engine -> headers = headers ;
559+ #else
560+ char * copy = dbmem_strdup (api_key );
561+ if (!copy ) {
562+ if (err_msg ) snprintf (err_msg , DBMEM_ERRBUF_SIZE , "Unable to duplicate API key (insufficient memory)" );
563+ return SQLITE_NOMEM ;
564+ }
565+
566+ if (engine -> api_key ) dbmemory_free (engine -> api_key );
567+ engine -> api_key = copy ;
568+ #endif
569+
570+ return SQLITE_OK ;
571+ }
572+
525573void dbmem_remote_engine_free (dbmem_remote_engine_t * engine ) {
526574 if (!engine ) return ;
527575
0 commit comments