Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
gadhagod committed Dec 7, 2020
0 parents commit 0143444
Show file tree
Hide file tree
Showing 5 changed files with 393 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.gem
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2020 Aarav Borthakur

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
176 changes: 176 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
# Rockset Ruby Client
A Ruby client library for Rockset.

![](https://img.shields.io/gem/v/rockset)

## Installation

gem install rockset

## Usage

# Include library
require "rockset"

# Authorize instance
Rockset.auth(api_key="TOKEN HERE", server="api.rs2.usw2.rockset.com")

# To test everything is working, get your organization info
puts Rockset.get_org

## Documentation

---------------

#### `query`
**Description**: Make an SQL query \
**Parameters**:
* query: The query to be performed, required, string

**Example**: `Rockset.query("select * from commons.famous_ppl where _id='94'")`

---------------

#### `validate_query`
**Description**: Validate an SQL query \
**Parameters**:
* query: The query to be validated, requrired, string

**Example**: `Rockset.validate_query("select * from commons.famous_ppl where _id='94'")`

---------------

#### `add_docs`
**Description**: Add documents to a collection
**Parameters**:
* docs: The documents to be added, required, list containing hashes
* collection: The collection to be added documents to, requrired, string
* workspace: The workspace that holds the collection to be modified, optional, defaults to "commons", string

**Example**: `Rockset.add_docs([{name: "John Cena", _id:"94"}], collection="famous_ppl")`

---------------

#### `del_docs`
**Description**: Delete documents
**Parameters**:
* docs: The documents to be deleted, required, list containing hashes
* collection: The collection of documents to be removed, required, string
* workspace: The workspace that holds the collection to be modified, optional, defaults to "commons", string

**Example**: `Rockset.del_docs([{_id:"94"}], collection="famous_ppl")`

---------------

#### `patch_docs`
**Description**: Patch documents
**Parameters**:
* docs: The documents to be patched, required, list containing hashes, hashes must have key `op` for patch operation, `path` for field path, and `value`.
* collection: The collection of documents to be patched, required, string
* workspace: The workspace that holds the collection to be modified, optional, defaults to "commons", string

**Example**: `Rockset.patch_docs([{op: "add", path: "occupation", value: "wrestler"}])`

---------------

#### `add_collection`
**Description**: Add a collection
**Parameters**:
* collection_metadata: Metadata for collection to be added, must have key `name`, can have `description`, `sources`, `retention_secs`, `event_time_info`, `field_mappings` (Check [Rockset Docs](https://docs.rockset.com/rest-api/#createcollection) for more info)
* workspace: The workspace that holds the collection to be added, optional, defaults to "commons", string

**Example**: `Rockset.add_collection({name: "famous_ppl"})`

---------------

#### `del_collection`
**Description**: Delete a collection
**Parameters**:
* collection: The collection to be deleted, required, string
* workspace: The workspace that holds the collection to be deleted, optional, defaults to "commons", string

**Example**: `Rockset.del_collection("famous_ppl")`

---------------

#### `get_collection`
**Description**: Get collection info
**Parameters**:
* collection: The collection to be got, required, string
* workspace: The workspace that holds the collection to be got, optional, defaults to "commons", string

**Example**: `Rockset.get_collection("famous_ppl")`

---------------

#### `get_collection_qlambdas`
**Description**: Get collection query lambdas
**Parameters**:
* collection: The collection with the query lambdas to be got, required, string
* workspace: The workspace that holds the collection with the query lambdas, optional, defaults to "commons", string

**Example**: `Rockset.get_collection_qlambdas("famous_ppl")`

---------------

#### `get_collections`
**Description**: Get info on all collections
**Example**: `Rockset.get_collections`

---------------

#### `get_collection_qlambdas`
**Description**: Get collection query lambdas
**Parameters**:
* collection: The collection with the query lambdas to be got, required, string
* workspace: The workspace that holds the collection with the query lambdas, optional, defaults to "commons", string

**Example**: `Rockset.get_workspace_collections("famous_ppl_projects")`

---------------

#### `get_org`
**Description**: Get organization
**Example**: `Rockset.get_org`

---------------

#### `add_qlambda`
**Description**: Add a query lambda
**Parameters**:
* name: The name of the query lambda, required, string
* query: The query of the query lambda, required, string
* description: The description of the query lambda, optional, string
* default_params: The default parameters of the query lambda, optional, list of hashes with keys `name`, `type`, `value` \
* workspace: The workspace that holds the query lambda, optional, defaults to "commons", string

**Example**: `Rockset.add_qlambda("john_cena_search", query="select * from commons.famous_ppl where name='John Cena'")`

---------------

#### `del_qlambda`
**Description**: Delete a query lambda
**Parameters**:
* qlambda: The name of the query lamda to be removed, string, required
* workspace: The workspace that holds the query lambda, optional, defaults to "commons", string

**Example**: `Rockset.del_qlambda("john_cena_search")`

---------------

#### `exec_qlambda`
(qlambda, version, parameters=[], workspace="commons"
**Description**: Execute a query lambda
**Parameters**:
* qlambda: The name of the query lambda to be executed, required, string
* version: The version of the query lambda to be executed, required, string
* parameters: The parameters of the query lambda, optional, list
* workspace: The workspace that holds the query lambda, optional, defaults to "commons", string

**Example**: `Rockset.exec_qlambda("john_cena_search", version="24cw39j")`

## To do
* `get_qlamda` function
* support data source integrations
* support aliases
* more query lambda options
199 changes: 199 additions & 0 deletions lib/rockset.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
module Rockset
require "json"
require "uri"
require "net/http"

class InvalidQuery < StandardError
end

def Rockset.auth(api_key, server)
$key = api_key
$server = "https://#{server}/v1/orgs/self"
end

def Rockset.query(query)
uri = URI("#{$server}/queries")
res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
req = Net::HTTP::Post.new(uri)
req["Content-Type"] = "application/json"
req["Authorization"] = "ApiKey #{$key}"
req.body = "{\"sql\": {\"query\": \"#{query}\"}}"
http.request(req)
end
return(JSON.parse(res.body))
end

def Rockset.validate_query(query)
uri = URI("#{$server}/queries/validations")
res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
req = Net::HTTP::Post.new(uri)
req["Content-Type"] = "application/json"
req["Authorization"] = "ApiKey #{$key}"
req.body = "{\"sql\": {\"query\": \"#{query}\"}}"
http.request(req)
end
if res.code == "200"
return("Valid")
else
raise(InvalidQuery.new(res.body))
end
end

def Rockset.add_docs(docs, collection, workspace="commons")
uri = URI("#{$server}/ws/#{workspace}/collections/#{collection}/docs")
res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
req = Net::HTTP::Post.new(uri)
req["Content-Type"] = "application/json"
req["Authorization"] = "ApiKey #{$key}"
req.body = "{\"data\": #{docs.to_json}}"
http.request(req)
end
return(JSON.parse(res.body))
end

def Rockset.del_docs(docs, collection, workspace="commons")
uri = URI("#{$server}/ws/#{workspace}/collections/#{collection}/docs")
res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
req = Net::HTTP::Delete.new(uri)
req["Content-Type"] = "application/json"
req["Authorization"] = "ApiKey #{$key}"
req.body = "{\"data\": #{docs.to_json}}"
http.request(req)
end
return(JSON.parse(res.body))
end

def Rockset.patch_docs(docs, collection, workspace="commons")
uri = URI("#{$server}/ws/#{workspace}/collections/#{collection}/docs")
res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
req = Net::HTTP::Patch.new(uri)
req["Content-Type"] = "application/json"
req["Authorization"] = "ApiKey #{$key}"
req.body = "{\"data\": #{docs.to_json}}"
http.request(req)
end
return(JSON.parse(res.body))
end

def Rockset.add_collection(collection_metadata, workspace="commons")
uri = URI("#{$server}/ws/#{workspace}/collections")
res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
req = Net::HTTP::Post.new(uri)
req["Content-Type"] = "application/json"
req["Authorization"] = "ApiKey #{$key}"
req.body = "#{collection_metadata.to_json}"
http.request(req)
end
return(JSON.parse(res.body))
end

def Rockset.del_collection(collection, workspace="commons")
uri = URI("#{$server}/ws/#{workspace}/collections/#{collection}")
res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
req = Net::HTTP::Delete.new(uri)
req["Content-Type"] = "application/json"
req["Authorization"] = "ApiKey #{$key}"
http.request(req)
end
return(JSON.parse(res.body))
end

def Rockset.get_collection(collection, workspace="commons")
uri = URI("#{$server}/ws/#{workspace}/collections/#{collection}")
res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
req = Net::HTTP::Get.new(uri)
req["Content-Type"] = "application/json"
req["Authorization"] = "ApiKey #{$key}"
http.request(req)
end
return(JSON.parse(res.body))
end

def Rockset.get_collection_qlambdas(collection, workspace="commons")
uri = URI("#{$server}/ws/#{workspace}/collections/#{collection}/lambdas")
res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
req = Net::HTTP::Get.new(uri)
req["Content-Type"] = "application/json"
req["Authorization"] = "ApiKey #{$key}"
http.request(req)
end
return(JSON.parse(res.body))
end

def Rockset.get_collections
uri = URI("#{$server}/collections")
res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
req = Net::HTTP::Get.new(uri)
req["Content-Type"] = "application/json"
req["Authorization"] = "ApiKey #{$key}"
http.request(req)
end
return(JSON.parse(res.body))
end

def Rockset.get_workspace_collections(workspace="commons")
uri = URI("#{server}/ws/#{workspace}/collections")
res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
req = Net::HTTP::Get.new(uri)
req["Content-Type"] = "application/json"
req["Authorization"] = "ApiKey #{$key}"
http.request(req)
end
return(JSON.parse(res.body))
end

def Rockset.get_org
uri = URI("#{$server}")
res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
req = Net::HTTP::Get.new(uri)
req["Content-Type"] = "application/json"
req["Authorization"] = "ApiKey #{$key}"
http.request(req)
end
return(JSON.parse(res.body))
end

def Rockset.add_qlambda(name, query, description="", default_params=[], workspace="commons")
uri = URI("#{$server}/ws/#{workspace}/lambdas")
res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
req = Net::HTTP::Post.new(uri)
req["Content-Type"] = "application/json"
req["Authorization"] = "ApiKey #{$key}"
req.body = "{
\"name\": \"#{name}\",
\"description\": \"#{description}\",
\"sql\": {
\"query\": \"#{query}\",
\"default_parameters\": #{default_params.to_json}
}
}"
http.request(req)
end
return(JSON.parse(res.body))
end

def Rockset.del_qlambda(qlambda, workspace="commons")
uri = URI("#{$server}/ws/#{workspace}/lambdas/#{qlambda}")
res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
req = Net::HTTP::Delete.new(uri)
req["Content-Type"] = "application/json"
req["Authorization"] = "ApiKey #{$key}"
http.request(req)
end
return(JSON.parse(res.body))
end

def Rockset.exec_qlambda(qlambda, version, parameters=[], workspace="commons")
uri = URI("#{$server}/ws/#{workspace}/lambdas/#{qlambda}/versions/#{version}")
res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
req = Net::HTTP::Post.new(uri)
req["Content-Type"] = "application/json"
req["Authorization"] = "ApiKey #{$key}"
req.body = "{
\"parameters\": #{parameters.to_json}
}"
http.request(req)
end
return(JSON.parse(res.body))
end
end
Loading

0 comments on commit 0143444

Please sign in to comment.