Skip to content
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

feat: support timeoutMs param #28

Merged
merged 2 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/req_bigquery.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ defmodule ReqBigQuery do
alias Req.Request
alias ReqBigQuery.Result

@allowed_options ~w(goth default_dataset_id project_id bigquery max_results use_legacy_sql)a
@allowed_options ~w(goth default_dataset_id project_id bigquery max_results use_legacy_sql timeout_ms)a
@base_url "https://bigquery.googleapis.com/bigquery/v2"
@max_results 10_000
@use_legacy_sql false
@timeout_ms 10_000

@doc """
Attaches to Req request.
Expand All @@ -40,6 +41,12 @@ defmodule ReqBigQuery do
If set to false, the query will use BigQuery's GoogleSQL: https://cloud.google.com/bigquery/sql-reference/
The default value is false.

* `:timeout_ms` - Optional. How long to wait for the query to complete, in milliseconds,
before the request times out and returns. Note: The call is not guaranteed to wait for
the specified timeout. If the query takes longer to run than the timeout value, the call
returns without any results and with the 'jobComplete' flag set to false. The default
value is 10000 milliseconds (10 seconds).

If you want to set any of these options when attaching the plugin, pass them as the second argument.

## Examples
Expand Down Expand Up @@ -114,6 +121,7 @@ defmodule ReqBigQuery do
|> Keyword.put_new(:base_url, @base_url)
|> Keyword.put_new(:max_results, @max_results)
|> Keyword.put_new(:use_legacy_sql, @use_legacy_sql)
|> Keyword.put_new(:timeout_ms, @timeout_ms)

request
|> Request.prepend_request_steps(bigquery_run: &run/1)
Expand All @@ -134,6 +142,7 @@ defmodule ReqBigQuery do
|> build_request_body(options[:default_dataset_id])
|> Map.put(:maxResults, options[:max_results])
|> Map.put(:useLegacySql, options[:use_legacy_sql])
|> Map.put(:timeoutMs, options[:timeout_ms])

%{request | url: uri}
|> Request.merge_options(auth: {:bearer, token}, json: json)
Expand Down
9 changes: 6 additions & 3 deletions test/req_bigquery_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ defmodule ReqBigQueryTest do
"defaultDataset" => %{"datasetId" => "my_awesome_dataset"},
"query" => "select * from iris",
"maxResults" => 10000,
"useLegacySql" => true
"useLegacySql" => true,
"timeoutMs" => 20000
}

assert URI.to_string(request.url) ==
Expand Down Expand Up @@ -51,7 +52,8 @@ defmodule ReqBigQueryTest do
goth: ctx.test,
project_id: "my_awesome_project_id",
default_dataset_id: "my_awesome_dataset",
use_legacy_sql: true
use_legacy_sql: true,
timeout_ms: 20_000
]

assert response =
Expand Down Expand Up @@ -96,7 +98,8 @@ defmodule ReqBigQueryTest do
}
],
"useLegacySql" => false,
"maxResults" => 10000
"maxResults" => 10000,
"timeoutMs" => 10000
}

assert URI.to_string(request.url) ==
Expand Down
Loading