Class: Gcloud::Datastore::Connection
- Inherits:
-
Object
- Object
- Gcloud::Datastore::Connection
- Defined in:
- lib/gcloud/datastore/connection.rb
Overview
Represent the HTTP connection to the Datastore, as well as the Datastore API calls.
This class only deals with Protocol Buffer objects, and is not part of the public API.
Constant Summary collapse
- API_VERSION =
"v1beta2"- API_URL =
"https://www.googleapis.com"
Instance Attribute Summary collapse
-
#credentials ⇒ Object
The Credentials object for signing HTTP requests.
-
#dataset_id ⇒ Object
The project/dataset_id connected to.
-
#default_http_headers ⇒ Object
The default HTTP headers to be sent on all API calls.
-
#http ⇒ Object
The HTTP object that makes calls to Datastore.
Instance Method Summary collapse
-
#allocate_ids(*incomplete_keys) ⇒ Object
Allocate IDs for incomplete keys.
-
#begin_transaction ⇒ Object
Begin a new transaction.
-
#commit(mutation, transaction = nil) ⇒ Object
Commit a transaction, optionally creating, deleting or modifying some entities.
-
#http_host ⇒ Object
The Datastore API URL.
-
#http_host=(new_http_host) ⇒ Object
Update the Datastore API URL.
-
#initialize(dataset_id, credentials) ⇒ Connection
constructor
Create a new Connection instance.
- #inspect ⇒ Object
-
#lookup(*keys) ⇒ Object
Look up entities by keys.
-
#rollback(transaction) ⇒ Object
Roll back a transaction.
-
#run_query(query, partition = nil) ⇒ Object
Query for entities.
Constructor Details
#initialize(dataset_id, credentials) ⇒ Connection
Create a new Connection instance.
47 48 49 50 |
# File 'lib/gcloud/datastore/connection.rb', line 47 def initialize dataset_id, credentials @dataset_id = dataset_id @credentials = credentials end |
Instance Attribute Details
#credentials ⇒ Object
The Credentials object for signing HTTP requests.
38 39 40 |
# File 'lib/gcloud/datastore/connection.rb', line 38 def credentials @credentials end |
#dataset_id ⇒ Object
The project/dataset_id connected to.
34 35 36 |
# File 'lib/gcloud/datastore/connection.rb', line 34 def dataset_id @dataset_id end |
#default_http_headers ⇒ Object
The default HTTP headers to be sent on all API calls.
121 122 123 124 125 |
# File 'lib/gcloud/datastore/connection.rb', line 121 def default_http_headers @default_http_headers ||= { "User-Agent" => "gcloud-node/#{Gcloud::VERSION}", "Content-Type" => "application/x-protobuf" } end |
#http ⇒ Object
The HTTP object that makes calls to Datastore. This must be a Faraday object.
133 134 135 |
# File 'lib/gcloud/datastore/connection.rb', line 133 def http @http ||= Faraday.new url: http_host end |
Instance Method Details
#allocate_ids(*incomplete_keys) ⇒ Object
Allocate IDs for incomplete keys. (This is useful for referencing an entity before it is inserted.)
55 56 57 58 59 60 61 62 |
# File 'lib/gcloud/datastore/connection.rb', line 55 def allocate_ids *incomplete_keys allocate_ids = Proto::AllocateIdsRequest.new.tap do |ai| ai.key = incomplete_keys end rpc_response = rpc("allocateIds", allocate_ids) Proto::AllocateIdsResponse.decode rpc_response end |
#begin_transaction ⇒ Object
Begin a new transaction.
86 87 88 89 90 91 |
# File 'lib/gcloud/datastore/connection.rb', line 86 def begin_transaction tx_request = Proto::BeginTransactionRequest.new response_rpc = rpc "beginTransaction", tx_request Proto::BeginTransactionResponse.decode response_rpc end |
#commit(mutation, transaction = nil) ⇒ Object
Commit a transaction, optionally creating, deleting or modifying some entities.
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/gcloud/datastore/connection.rb', line 96 def commit mutation, transaction = nil mode = Proto::CommitRequest::Mode::NON_TRANSACTIONAL mode = Proto::CommitRequest::Mode::TRANSACTIONAL if transaction commit = Proto::CommitRequest.new.tap do |c| c.mutation = mutation c.mode = mode c.transaction = transaction end Proto::CommitResponse.decode rpc("commit", commit) end |
#http_host ⇒ Object
The Datastore API URL.
142 143 144 |
# File 'lib/gcloud/datastore/connection.rb', line 142 def http_host @http_host || ENV["DATASTORE_HOST"] || API_URL end |
#http_host=(new_http_host) ⇒ Object
Update the Datastore API URL.
148 149 150 151 |
# File 'lib/gcloud/datastore/connection.rb', line 148 def http_host= new_http_host @http = nil # Reset the HTTP connection when host is set @http_host = new_http_host end |
#inspect ⇒ Object
153 154 155 |
# File 'lib/gcloud/datastore/connection.rb', line 153 def inspect "#{self.class}(#{@dataset_id})" end |
#lookup(*keys) ⇒ Object
Look up entities by keys.
66 67 68 69 70 71 |
# File 'lib/gcloud/datastore/connection.rb', line 66 def lookup *keys lookup = Proto::LookupRequest.new lookup.key = keys Proto::LookupResponse.decode rpc("lookup", lookup) end |
#rollback(transaction) ⇒ Object
Roll back a transaction.
111 112 113 114 115 116 117 |
# File 'lib/gcloud/datastore/connection.rb', line 111 def rollback transaction rollback = Proto::RollbackRequest.new.tap do |r| r.transaction = transaction end Proto::RollbackResponse.decode rpc("rollback", rollback) end |
#run_query(query, partition = nil) ⇒ Object
Query for entities.
74 75 76 77 78 79 80 81 82 |
# File 'lib/gcloud/datastore/connection.rb', line 74 def run_query query, partition = nil run_query = Proto::RunQueryRequest.new.tap do |rq| rq.query = query rq.partition_id = partition if partition rq end Proto::RunQueryResponse.decode rpc("runQuery", run_query) end |