Class: SurrealDB::HTTPClient

Inherits:
Object
  • Object
show all
Defined in:
lib/surrealdb/clients/http.rb

Overview

HTTP client for SurrealDB.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, namespace:, database:, username:, password:) {|HTTPClient| ... } ⇒ HTTPClient

Returns The HTTPClient instance.

Parameters:

  • The URL of the SurrealDB server.

  • The namespace to use.

  • The database to use.

  • The username to authenticate with.

  • The password to authenticate with.

Yields:



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

#databaseObject

Returns the value of attribute database.



11
12
13
# File 'lib/surrealdb/clients/http.rb', line 11

def database
  @database
end

#namespaceObject

Returns the value of attribute namespace.



11
12
13
# File 'lib/surrealdb/clients/http.rb', line 11

def namespace
  @namespace
end

#passwordObject

Returns the value of attribute password.



11
12
13
# File 'lib/surrealdb/clients/http.rb', line 11

def password
  @password
end

#urlObject

Returns the value of attribute url.



11
12
13
# File 'lib/surrealdb/clients/http.rb', line 11

def url
  @url
end

#usernameObject

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.

Parameters:

  • The HTTP method to use.

  • The endpoint to request.

  • (defaults to: nil)

    The body of the request.

Returns:

  • The response from the server.

Raises:

  • If the server returns an error.



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.message,
  ) 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.

Parameters:

  • The table to insert records into.

  • The data to insert. Each element must be a hash with an “id” key representing a unique identifier.

Raises:

  • If there is an issue with the request.



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.

Parameters:

  • The table to insert a record into.

  • The ID of the record to insert.

  • The data to insert.

Returns:

  • The new record.

Raises:

  • If there is an issue with the request.



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.

Parameters:

  • The table to delete records from.

Raises:

  • If there is an issue with the request.



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.

Parameters:

  • The table to delete records from.

  • The ID of the record to delete.

Raises:

  • If there is an issue with the request.



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.

Parameters:

  • The query to execute.

Returns:

  • The result of the query.

Raises:

  • If the query is invalid.



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.

Parameters:

  • The table to replace a record in.

  • The ID of the record to replace.

  • The data to replace.

Returns:

  • The replaced record.

Raises:

  • If there is an issue with the request.



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.

Parameters:

  • The table to get records from.

Returns:

  • The records.

Raises:

  • If there is an issue with the request.



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.

Parameters:

  • The table to get records from.

  • The ID of the record to get.

Returns:

  • The record.

Raises:

  • If there is an issue with the request.



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.

Parameters:

  • The table to upsert a record in.

  • The ID of the record to upsert.

  • The data to upsert.

Returns:

  • The upserted record.

Raises:

  • If there is an issue with the request.



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