Class: SurrealDB::HTTPClient
- Inherits:
-
Object
- Object
- SurrealDB::HTTPClient
- Defined in:
- lib/surrealdb/clients/http.rb
Overview
HTTP client for SurrealDB.
Instance Attribute Summary collapse
-
#database ⇒ Object
Returns the value of attribute database.
-
#namespace ⇒ Object
Returns the value of attribute namespace.
-
#password ⇒ Object
Returns the value of attribute password.
-
#url ⇒ Object
Returns the value of attribute url.
-
#username ⇒ Object
Returns the value of attribute username.
Instance Method Summary collapse
-
#_request(method, uri, data = nil) ⇒ SurrealDB::HTTPResponse
Make a request to the SurrealDB server.
-
#create_all(table, data) ⇒ Object
Insert multiple records into a table.
-
#create_one(table, id, data) ⇒ Hash
Insert a record into a table.
-
#delete_all(table) ⇒ Object
Delete all records from a table.
-
#delete_one(table, id) ⇒ Object
Delete one record from a table.
-
#execute(query) ⇒ Array<Hash>
Execute a query.
-
#initialize(url, namespace:, database:, username:, password:) {|HTTPClient| ... } ⇒ HTTPClient
constructor
The HTTPClient instance.
-
#replace_one(table, id, data) ⇒ Hash
Replace a record in a table.
-
#select_all(table) ⇒ Array<Hash>
Get all records from a table.
-
#select_one(table, id) ⇒ Hash
Get one record from a table.
-
#upsert_one(table, id, data) ⇒ Hash
Upsert a record in a table.
Constructor Details
#initialize(url, namespace:, database:, username:, password:) {|HTTPClient| ... } ⇒ HTTPClient
Returns The HTTPClient instance.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/surrealdb/clients/http.rb', line 20 def initialize(url, namespace:, database:, username:, password:) if url.end_with? "/" then @url = url else @url = "#{url}/" end @namespace = namespace @database = database @username = username @password = password @headers = { "NS" => @namespace, "DB" => @database, "Content-Type" => "application/json", "Accept" => "application/json", } yield self if block_given? end |
Instance Attribute Details
#database ⇒ Object
Returns the value of attribute database.
11 12 13 |
# File 'lib/surrealdb/clients/http.rb', line 11 def database @database end |
#namespace ⇒ Object
Returns the value of attribute namespace.
11 12 13 |
# File 'lib/surrealdb/clients/http.rb', line 11 def namespace @namespace end |
#password ⇒ Object
Returns the value of attribute password.
11 12 13 |
# File 'lib/surrealdb/clients/http.rb', line 11 def password @password end |
#url ⇒ Object
Returns the value of attribute url.
11 12 13 |
# File 'lib/surrealdb/clients/http.rb', line 11 def url @url end |
#username ⇒ Object
Returns the value of attribute username.
11 12 13 |
# File 'lib/surrealdb/clients/http.rb', line 11 def username @username end |
Instance Method Details
#_request(method, uri, data = nil) ⇒ SurrealDB::HTTPResponse
Make a request to the SurrealDB server.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/surrealdb/clients/http.rb', line 43 def _request(method, uri, data = nil) url = @url + uri response = HTTPX.plugin(:basic_authentication) .basic_authentication(@username, @password) .with(:headers => @headers) .request(method, url, :body => data) raise SurrealDB::SurrealError.new( response.error., ) unless response.error.nil? raise SurrealDB::SurrealError.new( response.status, response.json, ) unless (200..300).include?(response.status) and response.json[0]["status"] == "OK" SurrealDB::HTTPResponse.from_hash(response.json[0]).result end |
#create_all(table, data) ⇒ Object
Insert multiple records into a table.
77 78 79 |
# File 'lib/surrealdb/clients/http.rb', line 77 def create_all(table, data) _request("POST", "key/#{table}", JSON.generate(data)) end |
#create_one(table, id, data) ⇒ Hash
Insert a record into a table.
88 89 90 |
# File 'lib/surrealdb/clients/http.rb', line 88 def create_one(table, id, data) _request("POST", "key/#{table}/#{id}", JSON.generate(data))[0] end |
#delete_all(table) ⇒ Object
Delete all records from a table.
143 144 145 |
# File 'lib/surrealdb/clients/http.rb', line 143 def delete_all(table) _request("DELETE", "key/#{table}") end |
#delete_one(table, id) ⇒ Object
Delete one record from a table.
152 153 154 |
# File 'lib/surrealdb/clients/http.rb', line 152 def delete_one(table, id) _request("DELETE", "key/#{table}/#{id}") end |
#execute(query) ⇒ Array<Hash>
Execute a query.
67 68 69 |
# File 'lib/surrealdb/clients/http.rb', line 67 def execute(query) _request("POST", "sql", query) end |
#replace_one(table, id, data) ⇒ Hash
Replace a record in a table.
Requires all fields to be present, and will create a new record if one with the given ID does not exist.
121 122 123 |
# File 'lib/surrealdb/clients/http.rb', line 121 def replace_one(table, id, data) _request("PUT", "key/#{table}/#{id}", JSON.generate(data))[0] end |
#select_all(table) ⇒ Array<Hash>
Get all records from a table.
97 98 99 |
# File 'lib/surrealdb/clients/http.rb', line 97 def select_all(table) _request("GET", "key/#{table}") end |
#select_one(table, id) ⇒ Hash
Get one record from a table.
107 108 109 |
# File 'lib/surrealdb/clients/http.rb', line 107 def select_one(table, id) _request("GET", "key/#{table}/#{id}")[0] end |
#upsert_one(table, id, data) ⇒ Hash
Upsert a record in a table.
Requires only the fields that are being updated. If a record with the given ID does not exist, it will be created.
135 136 137 |
# File 'lib/surrealdb/clients/http.rb', line 135 def upsert_one(table, id, data) _request("PATCH", "key/#{table}/#{id}", JSON.generate(data))[0] end |