-
Notifications
You must be signed in to change notification settings - Fork 24
Add Wasm support #90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
carlopi
wants to merge
5
commits into
evidence-dev:main
Choose a base branch
from
carlopi:wasm
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add Wasm support #90
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
84ad6f8
Handle Wasm linking
carlopi 7d2931c
Add ClientContext to functions interfacing with HTTP, and use HTTPUtil
carlopi 62a2eab
Add more ClientContexts around
carlopi 1fdd3f3
Avoid initializing Openssl
carlopi 7807d6d
Add option gsheets_endpoint
carlopi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,8 @@ | ||
| #include "gsheets_requests.hpp" | ||
| #include "duckdb/common/exception.hpp" | ||
| #include "duckdb/common/http_util.hpp" | ||
| #include "duckdb/main/client_context.hpp" | ||
| #include "duckdb/main/database.hpp" | ||
| #include <chrono> | ||
| #include <openssl/ssl.h> | ||
| #include <openssl/err.h> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we remove SSL? |
||
|
|
@@ -8,100 +11,60 @@ | |
|
|
||
| namespace duckdb { | ||
|
|
||
| std::string perform_https_request(const std::string &host, const std::string &path, const std::string &token, | ||
| HttpMethod method, const std::string &body, const std::string &content_type) { | ||
| // NOTE: implements an exponential backoff retry strategy as per https://developers.google.com/sheets/api/limits | ||
| const int MAX_RETRIES = 10; | ||
| int retry_count = 0; | ||
| int backoff_s = 1; | ||
| while (retry_count < MAX_RETRIES) { | ||
| SSL_CTX *ctx = SSL_CTX_new(TLS_client_method()); | ||
| if (!ctx) { | ||
| throw duckdb::IOException("Failed to create SSL context"); | ||
| } | ||
|
|
||
| BIO *bio = BIO_new_ssl_connect(ctx); | ||
| SSL *ssl; | ||
| BIO_get_ssl(bio, &ssl); | ||
| SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY); | ||
|
|
||
| BIO_set_conn_hostname(bio, (host + ":443").c_str()); | ||
|
|
||
| if (BIO_do_connect(bio) <= 0) { | ||
| BIO_free_all(bio); | ||
| SSL_CTX_free(ctx); | ||
| throw duckdb::IOException("Failed to connect"); | ||
| } | ||
|
|
||
| std::string method_str; | ||
| switch (method) { | ||
| case HttpMethod::GET: | ||
| method_str = "GET"; | ||
| break; | ||
| case HttpMethod::POST: | ||
| method_str = "POST"; | ||
| break; | ||
| case HttpMethod::PUT: | ||
| method_str = "PUT"; | ||
| break; | ||
| } | ||
|
|
||
| std::string request = method_str + " " + path + " HTTP/1.0\r\n"; | ||
| request += "Host: " + host + "\r\n"; | ||
| request += "Authorization: Bearer " + token + "\r\n"; | ||
| request += "Connection: close\r\n"; | ||
|
|
||
| if (!body.empty()) { | ||
| request += "Content-Type: " + content_type + "\r\n"; | ||
| request += "Content-Length: " + std::to_string(body.length()) + "\r\n"; | ||
| } | ||
|
|
||
| request += "\r\n"; | ||
|
|
||
| if (!body.empty()) { | ||
| request += body; | ||
| } | ||
|
|
||
| if (BIO_write(bio, request.c_str(), request.length()) <= 0) { | ||
| BIO_free_all(bio); | ||
| SSL_CTX_free(ctx); | ||
| throw duckdb::IOException("Failed to write request"); | ||
| } | ||
|
|
||
| std::string response; | ||
| char buffer[1024]; | ||
| int len; | ||
| while ((len = BIO_read(bio, buffer, sizeof(buffer))) > 0) { | ||
| response.append(buffer, len); | ||
| } | ||
|
|
||
| BIO_free_all(bio); | ||
| SSL_CTX_free(ctx); | ||
|
|
||
| // Check for rate limit exceeded | ||
| if (response.find("429 Too Many Requests") != std::string::npos) { | ||
| std::this_thread::sleep_for(std::chrono::seconds(backoff_s)); | ||
| backoff_s *= 2; | ||
| retry_count++; | ||
| continue; | ||
| } | ||
|
|
||
| // Extract body from response | ||
| size_t body_start = response.find("\r\n\r\n"); | ||
| if (body_start != std::string::npos) { | ||
| return response.substr(body_start + 4); | ||
| } | ||
|
|
||
| return response; | ||
| } | ||
| // TODO: refactor HTTPS to catch more than just rate limit exceeded or else | ||
| // this could give false positives. | ||
| throw duckdb::IOException("Google Sheets API rate limit exceeded"); | ||
|
|
||
| std::string perform_https_request(ClientContext &context, const std::string &host, const std::string &path, | ||
| const std::string &token, HttpMethod method, const std::string &body, | ||
| const std::string &content_type) { | ||
| auto &db = DatabaseInstance::GetDatabase(context); | ||
|
|
||
| HTTPHeaders headers(db); | ||
| headers.Insert("Host", StringUtil::Format("%s", "http://127.0.0.1:8080/")); | ||
| headers.Insert("Authorization", StringUtil::Format("Bearer %s", token)); | ||
| // headers.Insert("Connection", StringUtil::Format("close")); | ||
|
|
||
| if (!body.empty()) { | ||
| headers.Insert("Content-Type", StringUtil::Format("%s", content_type)); | ||
| headers.Insert("Content-Length", StringUtil::Format("%s", std::to_string(body.length()))); | ||
| } | ||
| auto &http_util = HTTPUtil::Get(db); | ||
| unique_ptr<HTTPParams> params; | ||
| std::string x = string("https://") + host + path; | ||
| params = http_util.InitializeParameters(context, x); | ||
| // Unclear what's peculiar about extension install flow, but those two parameters are needed | ||
| // to avoid lengthy retry on 304 | ||
| params->follow_location = false; | ||
| params->keep_alive = false; | ||
|
|
||
| switch (method) { | ||
| case HttpMethod::GET: { | ||
|
|
||
| GetRequestInfo get_request(x, headers, *params, nullptr, nullptr); | ||
| get_request.try_request = true; | ||
|
|
||
| auto response = http_util.Request(get_request); | ||
|
|
||
| return response->body; | ||
|
|
||
| break; | ||
| } | ||
| case HttpMethod::POST: | ||
| case HttpMethod::PUT: | ||
| throw IOException("POST or PUT not implemented"); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. presume still have to implement these? GET= |
||
| break; | ||
| } | ||
|
|
||
| // TODO: refactor HTTPS to catch more than just rate limit exceeded or else | ||
| // this could give false positives. | ||
| throw duckdb::IOException("Google Sheets API rate limit exceeded"); | ||
| } | ||
|
|
||
| std::string call_sheets_api(const std::string &spreadsheet_id, const std::string &token, const std::string &sheet_name, | ||
| std::string call_sheets_api(ClientContext &context, const std::string &spreadsheet_id, const std::string &token, const std::string &sheet_name, | ||
| const std::string &sheet_range, HttpMethod method, const std::string &body) { | ||
| std::string host = "sheets.googleapis.com"; | ||
| Value value; | ||
| if (context.TryGetCurrentSetting("gsheets_endpoint", value)) { | ||
| host = value.ToString(); | ||
| } | ||
| std::string path = "/v4/spreadsheets/" + spreadsheet_id + "/values/" + sheet_name; | ||
|
|
||
| if (!sheet_range.empty()) { | ||
|
|
@@ -113,21 +76,29 @@ std::string call_sheets_api(const std::string &spreadsheet_id, const std::string | |
| path += "?valueInputOption=USER_ENTERED"; | ||
| } | ||
|
|
||
| return perform_https_request(host, path, token, method, body); | ||
| return perform_https_request(context, host, path, token, method, body); | ||
| } | ||
|
|
||
| std::string delete_sheet_data(const std::string &spreadsheet_id, const std::string &token, | ||
| std::string delete_sheet_data(ClientContext &context, const std::string &spreadsheet_id, const std::string &token, | ||
| const std::string &sheet_name, const std::string &sheet_range) { | ||
| std::string host = "sheets.googleapis.com"; | ||
| Value value; | ||
| if (context.TryGetCurrentSetting("gsheets_endpoint", value)) { | ||
| host = value.ToString(); | ||
| } | ||
| std::string sheet_and_range = sheet_range.empty() ? sheet_name : sheet_name + "!" + sheet_range; | ||
| std::string path = "/v4/spreadsheets/" + spreadsheet_id + "/values/" + sheet_and_range + ":clear"; | ||
|
|
||
| return perform_https_request(host, path, token, HttpMethod::POST, "{}"); | ||
| return perform_https_request(context, host, path, token, HttpMethod::POST, "{}"); | ||
| } | ||
|
|
||
| std::string get_spreadsheet_metadata(const std::string &spreadsheet_id, const std::string &token) { | ||
| std::string get_spreadsheet_metadata(ClientContext &context, const std::string &spreadsheet_id, const std::string &token) { | ||
| std::string host = "sheets.googleapis.com"; | ||
| Value value; | ||
| if (context.TryGetCurrentSetting("gsheets_endpoint", value)) { | ||
| host = value.ToString(); | ||
| } | ||
| std::string path = "/v4/spreadsheets/" + spreadsheet_id + "?&fields=sheets.properties"; | ||
| return perform_https_request(host, path, token, HttpMethod::GET, ""); | ||
| return perform_https_request(context, host, path, token, HttpMethod::GET, ""); | ||
| } | ||
| } // namespace duckdb | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need SSL at all now if we are using the duckdb util instead?