-
Notifications
You must be signed in to change notification settings - Fork 10
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
adds support for query parameters #625
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* Tableland Validator - OpenAPI 3.0 | ||
* | ||
* In Tableland, Validators are the execution unit/actors of the protocol. They have the following responsibilities: - Listen to onchain events to materialize Tableland-compliant SQL queries in a database engine (currently, SQLite by default). - Serve read-queries (e.g., SELECT * FROM foo_69_1) to the external world. - Serve state queries (e.g., list tables, get receipts, etc) to the external world. In the 1.0.0 release of the Tableland Validator API, we've switched to a design first approach! You can now help us improve the API whether it's by making changes to the definition itself or to the code. That way, with time, we can improve the API in general, and expose some of the new features in OAS3. The API includes the following endpoints: - `/health`: Returns OK if the validator considers itself healthy. - `/version`: Returns version information about the validator daemon. - `/query`: Returns the results of a SQL read query against the Tableland network. - `/receipt/{chainId}/{transactionHash}`: Returns the status of a given transaction receipt by hash. - `/tables/{chainId}/{tableId}`: Returns information about a single table, including schema information. | ||
* | ||
* API version: 1.1.0 | ||
* Contact: carson@textile.io | ||
* Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git) | ||
*/ | ||
package apiv1 | ||
|
||
type OneOfQueryParamsItems struct { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,8 @@ package apiv1 | |
type Query struct { | ||
// The SQL read query statement | ||
Statement string `json:"statement,omitempty"` | ||
// The values of query parameters | ||
Params []interface{} `json:"params,omitempty"` | ||
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. this code was generated by running this |
||
// The requested response format: * `objects` - Returns the query results as a JSON array of JSON objects. * `table` - Return the query results as a JSON object with columns and rows properties. | ||
Format string `json:"format,omitempty"` | ||
// Whether to extract the JSON object from the single property of the surrounding JSON object. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -231,9 +231,14 @@ func (c *Controller) GetTableQuery(rw http.ResponseWriter, r *http.Request) { | |
rw.Header().Set("Content-Type", "application/json") | ||
|
||
stm := r.URL.Query().Get("statement") | ||
params := []string{} | ||
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. capturing the |
||
|
||
if r.URL.Query().Has("params") { | ||
params = r.URL.Query()["params"] | ||
} | ||
|
||
start := time.Now() | ||
res, ok := c.runReadRequest(r.Context(), stm, rw) | ||
res, ok := c.runReadRequest(r.Context(), stm, params, rw) | ||
if !ok { | ||
return | ||
} | ||
|
@@ -276,6 +281,7 @@ func (c *Controller) PostTableQuery(rw http.ResponseWriter, r *http.Request) { | |
|
||
// setting a default body because these options could be missing from JSON | ||
body := &apiv1.Query{ | ||
Params: []any{}, | ||
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. the body can contain now a |
||
Format: string(formatter.Objects), | ||
Extract: false, | ||
Unwrap: false, | ||
|
@@ -289,8 +295,31 @@ func (c *Controller) PostTableQuery(rw http.ResponseWriter, r *http.Request) { | |
} | ||
_ = r.Body.Close() | ||
|
||
params := make([]string, len(body.Params)) | ||
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. we try to standardize to a |
||
for i, p := range body.Params { | ||
switch v := p.(type) { | ||
case float64: | ||
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. A JSON Number is converted |
||
params[i] = fmt.Sprint(v) | ||
case string: | ||
params[i] = fmt.Sprintf("\"%s\"", v) | ||
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. We must enclose the string with double quotes so that it behaves similarly with |
||
case nil: | ||
params[i] = "null" | ||
case bool: | ||
params[i] = "false" | ||
if v { | ||
params[i] = "true" | ||
} | ||
default: | ||
rw.WriteHeader(http.StatusBadRequest) | ||
msg := fmt.Sprintf("invalid type (%T) of parameter", v) | ||
log.Ctx(r.Context()).Error().Msg(msg) | ||
_ = json.NewEncoder(rw).Encode(errors.ServiceError{Message: msg}) | ||
return | ||
} | ||
} | ||
|
||
start := time.Now() | ||
res, ok := c.runReadRequest(r.Context(), body.Statement, rw) | ||
res, ok := c.runReadRequest(r.Context(), body.Statement, params, rw) | ||
if !ok { | ||
return | ||
} | ||
|
@@ -332,9 +361,10 @@ func (c *Controller) PostTableQuery(rw http.ResponseWriter, r *http.Request) { | |
func (c *Controller) runReadRequest( | ||
ctx context.Context, | ||
stm string, | ||
params []string, | ||
rw http.ResponseWriter, | ||
) (*gateway.TableData, bool) { | ||
res, err := c.gateway.RunReadQuery(ctx, stm) | ||
res, err := c.gateway.RunReadQuery(ctx, stm, params) | ||
if err != nil { | ||
rw.WriteHeader(http.StatusBadRequest) | ||
log.Ctx(ctx). | ||
|
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.
This is a trick we do because the type of the field
params
in the Post body can be anything. Seemode_query.go:L16