etre-client 0.8.0 → 0.8.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: d7ee88bbc4eaef62c80b7fe886975cc09f6fe3fa
4
- data.tar.gz: 474060e882a53a6274772f2b21854651295d43f5
2
+ SHA256:
3
+ metadata.gz: 7bb4311536146ce0c48e5a7c29ebdaac1b5280774c9b82b7f93e7be0bde54802
4
+ data.tar.gz: a991b597c0a26848422e358dd7ac1d949d61b4edd4e8b0b2d0590db66115d63c
5
5
  SHA512:
6
- metadata.gz: 5e3ec873dbc34c866833116196fce1b87a463a9f45360786bb48e6afb5c306bc1d7911a3c1d944736e81dee8dba6679793bf31afbeb63a2ad406b91930ea49d9
7
- data.tar.gz: b993c6f228ac3bc21f941d4a2cbd2b86eb0f43058a8e3e9a27da4082555507668312bcf446a257ce7eb0b9d1b19bb8cdbbd2fa3fee9fadbd19c38fef80e0413d
6
+ metadata.gz: 901d1d3f2e8692c6ccea5dea15a2f3e60fbcc86105c25124725369a14b68f57192e18a211b254d913afd6e9973f5286a7413be3dd50f40541e5074a8689336c7
7
+ data.tar.gz: 3cd429236b2b6dc3d79ef504cc3e637ba18681f7ff1966513f1ee25a970bd492b37adbc55cbead4d569d551d06a48f71d523c58bb3870f691b1ac877c2f32497
data/.gitignore CHANGED
@@ -1,2 +1 @@
1
- # ignore gems
2
1
  *.gem
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3
4
+ - 2.4
5
+ - 2.5
6
+ script: bundle exec rake spec
@@ -0,0 +1,7 @@
1
+ Change Log
2
+ ==========
3
+
4
+ Version 0.8.0 *(2017-10-31)*
5
+ ----------------------------
6
+
7
+ Initial alpha release of etre-client.
@@ -0,0 +1,17 @@
1
+ Contributing
2
+ ============
3
+
4
+ If you would like to contribute code to this project you can do so through GitHub by
5
+ forking the repository and sending a pull request.
6
+
7
+ When submitting code, please make every effort to follow existing conventions
8
+ and style in order to keep the code as readable as possible. Please also make
9
+ sure your code compiles by running `mvn clean verify`. Checkstyle failures
10
+ during compilation indicate errors in your style and can be viewed in the
11
+ `checkstyle-result.xml` file.
12
+
13
+ Before your code can be accepted into the project you must also sign the
14
+ [Individual Contributor License Agreement (CLA)][1].
15
+
16
+
17
+ [1]: https://spreadsheets.google.com/spreadsheet/viewform?formkey=dDViT2xzUHAwRkI3X3k5Z0lQM091OGc6MQ&ndplr=1
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "{}"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2017 Square, Inc.
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.md CHANGED
@@ -1,27 +1,53 @@
1
+ ![Build Status](https://travis-ci.org/square/etre-client-ruby.svg?branch=master)
2
+
1
3
  Description
2
4
  ------
3
- etre-client is a gem that can be used to interact with an [Etre](https://github.com/square/etre) instance.
5
+ etre-client is a client gem for [Etre](https://github.com/square/etre).
4
6
 
5
7
  Installation
6
8
  ------
7
9
  etre-client is hosted on rubygems.org. To install it
8
10
  1. Add "etre-client" to your Gemfile
9
- 2. Run "bunndle install"
11
+ 2. Run "bundle install"
10
12
 
11
13
  Alternatively, you can just "gem install etre-client".
12
14
 
13
15
  Usage
14
16
  ------
17
+ Require the gem
18
+ ```
19
+ require 'etre-client'
20
+ ```
21
+
15
22
  Create a new client
16
23
  ```
24
+ # Create a standard client.
17
25
  e = Etre::Client.new(entity_type: "node", url: "http://127.0.0.1:8080")
26
+
27
+ # Create a client with advanced HTTP options. These are passed down to the rest-client within the Etre Client.
28
+ # See https://github.com/rest-client/rest-client#passing-advanced-options for more details.
29
+ options = {
30
+ :ssl_client_cert => OpenSSL::X509::Certificate.new(File.read("path_to_ssl_cert")),
31
+ :ssl_client_key => OpenSSL::PKey::RSA.new(File.read("path_to_ssl_key")),
32
+ :ssl_ca_file => "path_to_ssl_ca",
33
+ :verify_ssl => OpenSSL::SSL::VERIFY_PEER,
34
+ :cookies => {:foo => "bar"},
35
+ }
36
+ e = Etre::Client.new(entity_type: "node", url: "http://127.0.0.1:8080", options: options)
18
37
  ```
19
38
 
20
39
  Insert entities
21
40
  ```
22
- entities = [{"foo" => "bar"}, {"foo" => "abc"}]
41
+ entities = [{"foo" => "bar"}, {"foo" => "abc"}, {"l1" => "a", "l2" => "b"}]
23
42
  e.insert(entities)
24
- => [{"id"=>"59f90caadd1b176f02eddcd8", "uri"=>"127.0.0.1:8080/api/v1/entity/59f90caadd1b176f02eddcd8"}, {"id"=>"59f90caadd1b176f02eddcda", "uri"=>"127.0.0.1:8080/api/v1/entity/59f90caadd1b176f02eddcda"}]
43
+ => [{"id"=>"59f90caadd1b176f02eddcd8", "uri"=>"127.0.0.1:8080/api/v1/entity/59f90caadd1b176f02eddcd8"}, {"id"=>"59f90caadd1b176f02eddcda", "uri"=>"127.0.0.1:8080/api/v1/entity/59f90caadd1b176f02eddcda"}, {"id"=>"59f90e3fdd1b176f02eddce5", "uri"=>"127.0.0.1:8080/api/v1/entity/59f90e3fdd1b176f02eddce5"}]
44
+ ```
45
+
46
+ Read entities
47
+ ```
48
+ query = "foo=bar"
49
+ e.query(query)
50
+ => [{"_id"=>"59f90caadd1b176f02eddcd8", "_rev"=>0, "_type"=>"node", "foo"=>"bar"}]
25
51
  ```
26
52
 
27
53
  Update entities
@@ -35,30 +61,38 @@ e.update(query, patch)
35
61
  Update a single entity
36
62
  ```
37
63
  id = "59f90caadd1b176f02eddcda"
38
- patch = {"foo" => "newbar"}
64
+ patch = {"foo" => "slug"}
39
65
  e.update_one(id, patch)
40
-
41
66
  => {"id"=>"59f90caadd1b176f02eddcda", "uri"=>"127.0.0.1:8080/api/v1/entity/59f90caadd1b176f02eddcda", "diff"=>{"_id"=>"59f90caadd1b176f02eddcda", "_rev"=>0, "_type"=>"node", "foo"=>"abc"}}
42
67
  ```
43
68
 
44
69
  Delete entities
45
70
  ```
46
- query = "foo=bar"
71
+ query = "foo=slug"
47
72
  e.delete(query)
73
+ => [{"id"=>"59f90caadd1b176f02eddcda", "uri"=>"127.0.0.1:8080/api/v1/entity/59f90caadd1b176f02eddcda", "diff"=>{"_id"=>"59f90caadd1b176f02eddcda", "_rev"=>1, "_type"=>"node", "foo"=>"slug"}}]
48
74
  ```
49
75
 
50
76
  Delete a single entity
51
77
  ```
52
- id = "abc"
78
+ id = "59f90caadd1b176f02eddcd8"
53
79
  e.delete_one(id)
54
- => {"_id" => "abc", "foo" => "bar"}
80
+ => {"id"=>"59f90caadd1b176f02eddcd8", "uri"=>"127.0.0.1:8080/api/v1/entity/59f90caadd1b176f02eddcd8", "diff"=>{"_id"=>"59f90caadd1b176f02eddcd8", "_rev"=>1, "_type"=>"node", "foo"=>"newbar"}}
55
81
  ```
56
82
 
57
83
  List the labels for an entity
58
84
  ```
59
- id = "abc"
85
+ id = "59f90e3fdd1b176f02eddce5"
60
86
  e.labels(id)
61
- => ["foo"]
87
+ => ["f1", "f2"]
88
+ ```
89
+
90
+ Delete the label on an entity
91
+ ```
92
+ id = "59f90e3fdd1b176f02eddce5"
93
+ label = "l1"
94
+ e.delete_label(id)
95
+ => {"id"=>"59f90e3fdd1b176f02eddce5", "uri"=>"127.0.0.1:8080/api/v1/entity/59f90e3fdd1b176f02eddce5", "diff"=>{"_id"=>"59f90e3fdd1b176f02eddce5", "_rev"=>0, "_type"=>"node", "l1"=>"a", "l2"=>"b"}}
62
96
  ```
63
97
 
64
98
  Development
@@ -68,6 +102,11 @@ Run the tests
68
102
  bundle exec rake spec
69
103
  ```
70
104
 
105
+ Publish a new version of the gem:
106
+ 1. Bump the version in lib/etre-client/version.rb
107
+ 2. Build the gem with `gem build etre-client.gemspec`
108
+ 3. Publish the new gem with `gem push etre-client-#{GEM_VERSION}.gem`
109
+
71
110
  ## License
72
111
 
73
112
  Copyright (c) 2017 Square Inc. Distributed under the Apache 2.0 License.
@@ -6,7 +6,6 @@ require 'etre-client/version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = 'etre-client'
8
8
  spec.version = Etre::Client::VERSION
9
- spec.version = '0.8.0'
10
9
  spec.license = 'Apache-2.0'
11
10
  spec.authors = ['Michael Finch']
12
11
  spec.email = ['mfinch@squareup.com']
@@ -1,264 +1,2 @@
1
+ require 'etre-client/client'
1
2
  require 'etre-client/errors'
2
- require 'rest-client'
3
- require 'json'
4
-
5
- module Etre
6
- class Client
7
- attr_reader :entity_type, :url
8
-
9
- API_ROOT = "/api/v1"
10
- META_LABEL_ID = "_id"
11
- META_LABEL_TYPE = "_type"
12
-
13
- def initialize(entity_type:, url:, ssl_cert: nil, ssl_key: nil, ssl_ca: nil, insecure: true)
14
- @entity_type = entity_type
15
- @url = url
16
-
17
- @ssl_cert = ssl_cert
18
- @ssl_key = ssl_key
19
- @ssl_ca = ssl_ca
20
- @insecure = insecure
21
- end
22
-
23
- # query returns an array of entities that satisfy a query.
24
- def query(query, filter = nil)
25
- if query.nil? || query.empty?
26
- raise QueryNotProvided
27
- end
28
-
29
- # @todo: translate filter to query params
30
-
31
- # Do the normal GET /entities?query unless the query is ~2k characters
32
- # becuase that brings the entire URL length close to the max supported
33
- # limit across most HTTP servers. In that case, switch to alternate
34
- # endpoint to POST the long query.
35
- if query.length < 2000
36
- # Always escape the query.
37
- query = CGI::escape(query)
38
-
39
- resp = etre_get("/entities/#{@entity_type}?query=#{query}")
40
- else
41
- # DO NOT ESCAPE THE QUERY! It's not sent via URL, so no escaping needed.
42
- resp = etre_post("/query/#{@entity_type}", query)
43
- end
44
-
45
- if resp.code != 200
46
- raise UnexpectedResponseCode, "expected 200, got #{resp.code}"
47
- end
48
-
49
- return JSON.parse(resp.body)
50
- end
51
-
52
- # insert inserts an array of entities.
53
- def insert(entities)
54
- if entities.nil? || !entities.any?
55
- raise EntityNotProvided
56
- end
57
-
58
- entities.each do |e|
59
- if e.key?(META_LABEL_ID)
60
- raise EntityIdSet, "entity: #{e}"
61
- end
62
-
63
- if e.key?(META_LABEL_TYPE) && e[META_LABEL_TYPE] != @entity_type
64
- raise EntityTypeMismatch, "only valid type is '#{@entity_type}', but " +
65
- "entity has type '#{e[META_LABEL_TYPE]}'"
66
- end
67
- end
68
-
69
- resp = etre_post("/entities/#{@entity_type}", entities)
70
- if ![200, 201].include?(resp.code)
71
- raise UnexpectedResponseCode, "expected 200 or 201, got #{resp.code}"
72
- end
73
-
74
- return JSON.parse(resp.body)
75
- end
76
-
77
- # update updates entities with the given patch that satisfy the given query.
78
- def update(query, patch)
79
- if query.nil? || query.empty?
80
- raise QueryNotProvided
81
- end
82
-
83
- # Always escape the query.
84
- query = CGI::escape(query)
85
-
86
- if patch.nil? || !patch.any?
87
- raise PatchNotProvided
88
- end
89
-
90
- if patch.key?(META_LABEL_ID)
91
- raise PatchIdSet, "patch: #{patch}"
92
- end
93
-
94
- if patch.key?(META_LABEL_TYPE) && patch[META_LABEL_TYPE] != @entity_type
95
- raise EntityTypeMismatch, "only valid type is '#{@entity_type}', but " +
96
- "patch has type '#{patch[META_LABEL_TYPE]}'"
97
- end
98
-
99
- resp = etre_put("/entities/#{@entity_type}?query=#{query}", patch)
100
- if ![200, 201].include?(resp.code)
101
- raise UnexpectedResponseCode, "expected 200 or 201, got #{resp.code}"
102
- end
103
-
104
- return JSON.parse(resp.body)
105
- end
106
-
107
- # update_one updates the given entity id with the provided patch.
108
- def update_one(id, patch)
109
- if id.nil? || id.empty?
110
- raise IdNotProvided
111
- end
112
-
113
- if patch.nil? || !patch.any?
114
- raise PatchNotProvided
115
- end
116
-
117
- if patch.key?(META_LABEL_ID)
118
- raise PatchIdSet, "patch: #{patch}"
119
- end
120
-
121
- if patch.key?(META_LABEL_TYPE) && patch[META_LABEL_TYPE] != @entity_type
122
- raise EntityTypeMismatch, "only valid type is '#{@entity_type}', but " +
123
- "patch has type '#{patch[META_LABEL_TYPE]}'"
124
- end
125
-
126
- resp = etre_put("/entity/#{@entity_type}/#{id}", patch)
127
- if ![200, 201].include?(resp.code)
128
- raise UnexpectedResponseCode, "expected 200 or 201, got #{resp.code}"
129
- end
130
-
131
- return JSON.parse(resp.body)
132
- end
133
-
134
- # delete deletes the entities that satisfy the given query.
135
- def delete(query)
136
- if query.nil? || query.empty?
137
- raise QueryNotProvided
138
- end
139
-
140
- # Always escape the query.
141
- query = CGI::escape(query)
142
-
143
- resp = etre_delete("/entities/#{@entity_type}?query=#{query}")
144
- if resp.code != 200
145
- raise UnexpectedResponseCode, "expected 200, got #{resp.code}"
146
- end
147
-
148
- return JSON.parse(resp.body)
149
- end
150
-
151
- # delete_one deletes the entity with the given id.
152
- def delete_one(id)
153
- if id.nil? || id.empty?
154
- raise IdNotProvided
155
- end
156
-
157
- resp = etre_delete("/entity/#{@entity_type}/#{id}")
158
- if resp.code != 200
159
- raise UnexpectedResponseCode, "expected 200, got #{resp.code}"
160
- end
161
-
162
- return JSON.parse(resp.body)
163
- end
164
-
165
- # labels returns an array of labels for the given entity id.
166
- def labels(id)
167
- if id.nil? || id.empty?
168
- raise IdNotProvided
169
- end
170
-
171
- resp = etre_get("/entity/#{@entity_type}/#{id}/labels")
172
-
173
- if resp.code != 200
174
- raise UnexpectedResponseCode, "expected 200, got #{resp.code}"
175
- end
176
-
177
- return JSON.parse(resp.body)
178
- end
179
-
180
- # delete_label deletes the given label on the provided entity id.
181
- def delete_label(id, label)
182
- if id.nil? || id.empty?
183
- raise IdNotProvided
184
- end
185
-
186
- if label.nil? || label.empty?
187
- raise LabelNotSet
188
- end
189
-
190
- resp = etre_delete("/entity/#{@entity_type}/#{id}/labels/#{label}")
191
- if resp.code != 200
192
- raise UnexpectedResponseCode, "expected 200, got #{resp.code}"
193
- end
194
-
195
- return JSON.parse(resp.body)
196
- end
197
-
198
- private
199
-
200
- def etre_get(route)
201
- resource_for_route(route).get(
202
- get_headers,
203
- )
204
- end
205
-
206
- def etre_post(route, params = nil)
207
- resource_for_route(route).post(
208
- params.to_json,
209
- post_headers,
210
- )
211
- end
212
-
213
- def etre_put(route, params = nil)
214
- resource_for_route(route).put(
215
- params.to_json,
216
- put_headers,
217
- )
218
- end
219
-
220
- def etre_delete(route)
221
- resource_for_route(route).delete(
222
- delete_headers,
223
- )
224
- end
225
-
226
- def get_headers
227
- {:accept => 'application/json'}
228
- end
229
-
230
- def post_headers
231
- get_headers.merge!(:content_type => 'application/json')
232
- end
233
-
234
- def put_headers
235
- post_headers
236
- end
237
-
238
- def delete_headers
239
- get_headers
240
- end
241
-
242
- def resource_for_route(route)
243
- opts = {}
244
- opts.merge!(ssl_options) unless @insecure
245
- RestClient::Resource.new(
246
- @url + API_ROOT + route,
247
- opts
248
- )
249
- end
250
-
251
- def ssl_options
252
- {
253
- :ssl_client_cert => OpenSSL::X509::Certificate.new(File.read(@ssl_cert)),
254
- :ssl_client_key => OpenSSL::PKey::RSA.new(File.read(@ssl_key)),
255
- :ssl_ca_file => @ssl_ca,
256
- :verify_ssl => OpenSSL::SSL::VERIFY_PEER,
257
- }
258
- end
259
-
260
- def parse_response(response)
261
- JSON.parse(response)
262
- end
263
- end
264
- end
@@ -0,0 +1,324 @@
1
+ require 'etre-client/errors'
2
+ require 'logger'
3
+ require 'rest-client'
4
+ require 'json'
5
+
6
+ module Etre
7
+ class Client
8
+ attr_reader :entity_type, :url
9
+
10
+ API_ROOT = "/api/v1"
11
+ META_LABEL_ID = "_id"
12
+ META_LABEL_TYPE = "_type"
13
+
14
+ def initialize(entity_type:, url:, query_timeout: 5, retry_count: 0, retry_wait: 1, options: {})
15
+ @entity_type = entity_type
16
+ @url = url
17
+ @query_timeout = query_timeout # http request timeout in seconds
18
+ @retry_count = retry_count # retry count on network or API error
19
+ @retry_wait = retry_wait # wait time between retries in seconds
20
+ @options = options
21
+
22
+ @logger = Logger.new(STDOUT)
23
+ end
24
+
25
+ # query returns an array of entities that satisfy a query.
26
+ def query(query, filter = nil)
27
+ if query.nil? || query.empty?
28
+ raise QueryNotProvided
29
+ end
30
+
31
+ # @todo: translate filter to query params
32
+
33
+ # Do the normal GET /entities?query unless the query is ~2k characters
34
+ # becuase that brings the entire URL length close to the max supported
35
+ # limit across most HTTP servers. In that case, switch to alternate
36
+ # endpoint to POST the long query.
37
+ if query.length < 2000
38
+ # Always escape the query.
39
+ query = CGI::escape(query)
40
+
41
+ begin
42
+ resp = etre_get("/entities/#{@entity_type}?query=#{query}")
43
+ rescue RestClient::ExceptionWithResponse => e
44
+ raise RequestFailed, e.response
45
+ end
46
+ else
47
+ # DO NOT ESCAPE THE QUERY! It's not sent via URL, so no escaping needed.
48
+ begin
49
+ resp = etre_post("/query/#{@entity_type}", query)
50
+ rescue RestClient::ExceptionWithResponse => e
51
+ raise RequestFailed, e.response
52
+ end
53
+ end
54
+
55
+ if resp.code != 200
56
+ raise UnexpectedResponseCode, "expected 200, got #{resp.code}"
57
+ end
58
+
59
+ return JSON.parse(resp.body)
60
+ end
61
+
62
+ # insert inserts an array of entities.
63
+ def insert(entities)
64
+ if entities.nil? || !entities.any?
65
+ raise EntityNotProvided
66
+ end
67
+
68
+ entities.each do |e|
69
+ if e.key?(META_LABEL_ID)
70
+ raise EntityIdSet, "entity: #{e}"
71
+ end
72
+
73
+ if e.key?(META_LABEL_TYPE) && e[META_LABEL_TYPE] != @entity_type
74
+ raise EntityTypeMismatch, "only valid type is '#{@entity_type}', but " +
75
+ "entity has type '#{e[META_LABEL_TYPE]}'"
76
+ end
77
+ end
78
+
79
+ begin
80
+ resp = etre_post("/entities/#{@entity_type}", entities)
81
+ rescue RestClient::ExceptionWithResponse => e
82
+ raise RequestFailed, e.response
83
+ end
84
+
85
+ if ![200, 201].include?(resp.code)
86
+ raise UnexpectedResponseCode, "expected 200 or 201, got #{resp.code}"
87
+ end
88
+
89
+ return JSON.parse(resp.body)
90
+ end
91
+
92
+ # update updates entities with the given patch that satisfy the given query.
93
+ def update(query, patch)
94
+ if query.nil? || query.empty?
95
+ raise QueryNotProvided
96
+ end
97
+
98
+ # Always escape the query.
99
+ query = CGI::escape(query)
100
+
101
+ if patch.nil? || !patch.any?
102
+ raise PatchNotProvided
103
+ end
104
+
105
+ if patch.key?(META_LABEL_ID)
106
+ raise PatchIdSet, "patch: #{patch}"
107
+ end
108
+
109
+ if patch.key?(META_LABEL_TYPE) && patch[META_LABEL_TYPE] != @entity_type
110
+ raise EntityTypeMismatch, "only valid type is '#{@entity_type}', but " +
111
+ "patch has type '#{patch[META_LABEL_TYPE]}'"
112
+ end
113
+
114
+ begin
115
+ resp = etre_put("/entities/#{@entity_type}?query=#{query}", patch)
116
+ rescue RestClient::ExceptionWithResponse => e
117
+ raise RequestFailed, e.response
118
+ end
119
+
120
+ if ![200, 201].include?(resp.code)
121
+ raise UnexpectedResponseCode, "expected 200 or 201, got #{resp.code}"
122
+ end
123
+
124
+ return JSON.parse(resp.body)
125
+ end
126
+
127
+ # update_one updates the given entity id with the provided patch.
128
+ def update_one(id, patch)
129
+ if id.nil? || id.empty?
130
+ raise IdNotProvided
131
+ end
132
+
133
+ if patch.nil? || !patch.any?
134
+ raise PatchNotProvided
135
+ end
136
+
137
+ if patch.key?(META_LABEL_ID)
138
+ raise PatchIdSet, "patch: #{patch}"
139
+ end
140
+
141
+ if patch.key?(META_LABEL_TYPE) && patch[META_LABEL_TYPE] != @entity_type
142
+ raise EntityTypeMismatch, "only valid type is '#{@entity_type}', but " +
143
+ "patch has type '#{patch[META_LABEL_TYPE]}'"
144
+ end
145
+
146
+ begin
147
+ resp = etre_put("/entity/#{@entity_type}/#{id}", patch)
148
+ rescue RestClient::ExceptionWithResponse => e
149
+ raise RequestFailed, e.response
150
+ end
151
+
152
+ if ![200, 201].include?(resp.code)
153
+ raise UnexpectedResponseCode, "expected 200 or 201, got #{resp.code}"
154
+ end
155
+
156
+ return JSON.parse(resp.body)
157
+ end
158
+
159
+ # delete deletes the entities that satisfy the given query.
160
+ def delete(query)
161
+ if query.nil? || query.empty?
162
+ raise QueryNotProvided
163
+ end
164
+
165
+ # Always escape the query.
166
+ query = CGI::escape(query)
167
+
168
+ begin
169
+ resp = etre_delete("/entities/#{@entity_type}?query=#{query}")
170
+ rescue RestClient::ExceptionWithResponse => e
171
+ raise RequestFailed, e.response
172
+ end
173
+
174
+ if resp.code != 200
175
+ raise UnexpectedResponseCode, "expected 200, got #{resp.code}"
176
+ end
177
+
178
+ return JSON.parse(resp.body)
179
+ end
180
+
181
+ # delete_one deletes the entity with the given id.
182
+ def delete_one(id)
183
+ if id.nil? || id.empty?
184
+ raise IdNotProvided
185
+ end
186
+
187
+ begin
188
+ resp = etre_delete("/entity/#{@entity_type}/#{id}")
189
+ rescue RestClient::ExceptionWithResponse => e
190
+ raise RequestFailed, e.response
191
+ end
192
+
193
+ if resp.code != 200
194
+ raise UnexpectedResponseCode, "expected 200, got #{resp.code}"
195
+ end
196
+
197
+ return JSON.parse(resp.body)
198
+ end
199
+
200
+ # labels returns an array of labels for the given entity id.
201
+ def labels(id)
202
+ if id.nil? || id.empty?
203
+ raise IdNotProvided
204
+ end
205
+
206
+ begin
207
+ resp = etre_get("/entity/#{@entity_type}/#{id}/labels")
208
+ rescue RestClient::ExceptionWithResponse => e
209
+ raise RequestFailed, e.response
210
+ end
211
+
212
+ if resp.code != 200
213
+ raise UnexpectedResponseCode, "expected 200, got #{resp.code}"
214
+ end
215
+
216
+ return JSON.parse(resp.body)
217
+ end
218
+
219
+ # delete_label deletes the given label on the provided entity id.
220
+ def delete_label(id, label)
221
+ if id.nil? || id.empty?
222
+ raise IdNotProvided
223
+ end
224
+
225
+ if label.nil? || label.empty?
226
+ raise LabelNotSet
227
+ end
228
+
229
+ begin
230
+ resp = etre_delete("/entity/#{@entity_type}/#{id}/labels/#{label}")
231
+ rescue RestClient::ExceptionWithResponse => e
232
+ raise RequestFailed, e.response
233
+ end
234
+
235
+ if resp.code != 200
236
+ raise UnexpectedResponseCode, "expected 200, got #{resp.code}"
237
+ end
238
+
239
+ return JSON.parse(resp.body)
240
+ end
241
+
242
+ private
243
+
244
+ def etre_get(route)
245
+ rest_retry {
246
+ resource_for_route(route).get(
247
+ get_headers,
248
+ )
249
+ }
250
+ end
251
+
252
+ def etre_post(route, params = nil)
253
+ rest_retry {
254
+ resource_for_route(route).post(
255
+ params.to_json,
256
+ post_headers,
257
+ )
258
+ }
259
+ end
260
+
261
+ def etre_put(route, params = nil)
262
+ rest_retry {
263
+ resource_for_route(route).put(
264
+ params.to_json,
265
+ put_headers,
266
+ )
267
+ }
268
+ end
269
+
270
+ def etre_delete(route)
271
+ rest_retry {
272
+ resource_for_route(route).delete(
273
+ delete_headers,
274
+ )
275
+ }
276
+ end
277
+
278
+ def get_headers
279
+ {
280
+ :accept => 'application/json',
281
+ :x_etre_query_timeout => @query_timeout
282
+ }
283
+ end
284
+
285
+ def post_headers
286
+ get_headers.merge!(:content_type => 'application/json')
287
+ end
288
+
289
+ def put_headers
290
+ post_headers
291
+ end
292
+
293
+ def delete_headers
294
+ get_headers
295
+ end
296
+
297
+ def resource_for_route(route)
298
+ RestClient::Resource.new(
299
+ @url + API_ROOT + route,
300
+ @options
301
+ )
302
+ end
303
+
304
+ def parse_response(response)
305
+ JSON.parse(response)
306
+ end
307
+
308
+ def rest_retry(&block)
309
+ retries = 0
310
+
311
+ begin
312
+ yield
313
+ rescue => e
314
+ if (retries += 1) <= @retry_count
315
+ @logger.warn("Error querying etre (#{e}). Sleeping for #{@retry_wait} seconds before trying again (attmept #{retries}/#{@retry_count}).")
316
+ sleep(@retry_wait)
317
+ retry
318
+ else
319
+ raise
320
+ end
321
+ end
322
+ end
323
+ end
324
+ end
@@ -8,6 +8,7 @@ module Etre
8
8
  class PatchIdSet < StandardError; end
9
9
  class PatchNotProvided < StandardError; end
10
10
  class QueryNotProvided < StandardError; end
11
+ class RequestFailed < StandardError; end
11
12
  class UnexpectedResponseCode < StandardError; end
12
13
  end
13
14
  end
@@ -1,5 +1,5 @@
1
1
  module Etre
2
2
  class Client
3
- VERSION = "0.8.0"
3
+ VERSION = "0.8.5"
4
4
  end
5
5
  end
@@ -2,13 +2,11 @@ require 'etre-client'
2
2
  require 'ostruct'
3
3
 
4
4
  describe Etre::Client do
5
- let(:etre_client) { Etre::Client.new(entity_type: "node", url: "http://localhost:3000", insecure: true) }
6
- let(:get_headers) { {:accept => "application/json"} }
5
+ let(:etre_client) { Etre::Client.new(entity_type: "node", url: "http://localhost:3000") }
6
+ let(:get_headers) { {:accept => "application/json", :x_etre_query_timeout => 5} }
7
7
  let(:post_headers) { get_headers.merge({:content_type => "application/json"}) }
8
8
  let(:put_headers) { post_headers }
9
9
  let(:delete_headers) { get_headers }
10
- let(:migration_id) { 3 }
11
- let(:cluster) { "cluster-001" }
12
10
  let(:entity1) { {"_id" => "abc", "foo" => "bar"} }
13
11
  let(:entity2) { {"oof" => "rab"} }
14
12
  let(:entity3) { {"blah" => "slug"} }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: etre-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.8.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Finch
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-31 00:00:00.000000000 Z
11
+ date: 2020-11-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -42,22 +42,22 @@ dependencies:
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: '12.2'
48
45
  - - ">="
49
46
  - !ruby/object:Gem::Version
50
47
  version: 12.2.0
48
+ - - "~>"
49
+ - !ruby/object:Gem::Version
50
+ version: '12.2'
51
51
  type: :development
52
52
  prerelease: false
53
53
  version_requirements: !ruby/object:Gem::Requirement
54
54
  requirements:
55
- - - "~>"
56
- - !ruby/object:Gem::Version
57
- version: '12.2'
58
55
  - - ">="
59
56
  - !ruby/object:Gem::Version
60
57
  version: 12.2.0
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '12.2'
61
61
  - !ruby/object:Gem::Dependency
62
62
  name: rspec
63
63
  requirement: !ruby/object:Gem::Requirement
@@ -72,7 +72,7 @@ dependencies:
72
72
  - - "~>"
73
73
  - !ruby/object:Gem::Version
74
74
  version: '3.0'
75
- description:
75
+ description:
76
76
  email:
77
77
  - mfinch@squareup.com
78
78
  executables: []
@@ -80,12 +80,16 @@ extensions: []
80
80
  extra_rdoc_files: []
81
81
  files:
82
82
  - ".gitignore"
83
+ - ".travis.yml"
84
+ - CHANGELOG.md
85
+ - CONTRIBUTING.md
83
86
  - Gemfile
84
- - Gemfile.lock
87
+ - LICENSE
85
88
  - README.md
86
89
  - Rakefile
87
90
  - etre-client.gemspec
88
91
  - lib/etre-client.rb
92
+ - lib/etre-client/client.rb
89
93
  - lib/etre-client/errors.rb
90
94
  - lib/etre-client/version.rb
91
95
  - spec/client_spec.rb
@@ -94,7 +98,7 @@ homepage: https://github.com/square/etre-client-ruby
94
98
  licenses:
95
99
  - Apache-2.0
96
100
  metadata: {}
97
- post_install_message:
101
+ post_install_message:
98
102
  rdoc_options: []
99
103
  require_paths:
100
104
  - lib
@@ -109,9 +113,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
109
113
  - !ruby/object:Gem::Version
110
114
  version: '0'
111
115
  requirements: []
112
- rubyforge_project:
113
- rubygems_version: 2.5.2
114
- signing_key:
116
+ rubygems_version: 3.0.3
117
+ signing_key:
115
118
  specification_version: 4
116
119
  summary: Client gem for interacting with Etre
117
120
  test_files:
@@ -1,97 +0,0 @@
1
- GEM
2
- remote: https://rubygems.org/
3
- specs:
4
- actionpack (5.1.4)
5
- actionview (= 5.1.4)
6
- activesupport (= 5.1.4)
7
- rack (~> 2.0)
8
- rack-test (>= 0.6.3)
9
- rails-dom-testing (~> 2.0)
10
- rails-html-sanitizer (~> 1.0, >= 1.0.2)
11
- actionview (5.1.4)
12
- activesupport (= 5.1.4)
13
- builder (~> 3.1)
14
- erubi (~> 1.4)
15
- rails-dom-testing (~> 2.0)
16
- rails-html-sanitizer (~> 1.0, >= 1.0.3)
17
- activesupport (5.1.4)
18
- concurrent-ruby (~> 1.0, >= 1.0.2)
19
- i18n (~> 0.7)
20
- minitest (~> 5.1)
21
- tzinfo (~> 1.1)
22
- builder (3.2.3)
23
- concurrent-ruby (1.0.5)
24
- crass (1.0.2)
25
- diff-lcs (1.3)
26
- domain_name (0.5.20170404)
27
- unf (>= 0.0.5, < 1.0.0)
28
- erubi (1.7.0)
29
- http-cookie (1.0.3)
30
- domain_name (~> 0.5)
31
- i18n (0.9.0)
32
- concurrent-ruby (~> 1.0)
33
- loofah (2.1.1)
34
- crass (~> 1.0.2)
35
- nokogiri (>= 1.5.9)
36
- method_source (0.9.0)
37
- mime-types (3.1)
38
- mime-types-data (~> 3.2015)
39
- mime-types-data (3.2016.0521)
40
- mini_portile2 (2.3.0)
41
- minitest (5.10.3)
42
- netrc (0.11.0)
43
- nokogiri (1.8.1)
44
- mini_portile2 (~> 2.3.0)
45
- rack (2.0.3)
46
- rack-test (0.7.0)
47
- rack (>= 1.0, < 3)
48
- rails-dom-testing (2.0.3)
49
- activesupport (>= 4.2.0)
50
- nokogiri (>= 1.6)
51
- rails-html-sanitizer (1.0.3)
52
- loofah (~> 2.0)
53
- railties (5.1.4)
54
- actionpack (= 5.1.4)
55
- activesupport (= 5.1.4)
56
- method_source
57
- rake (>= 0.8.7)
58
- thor (>= 0.18.1, < 2.0)
59
- rake (12.2.1)
60
- rest-client (2.0.2)
61
- http-cookie (>= 1.0.2, < 2.0)
62
- mime-types (>= 1.16, < 4.0)
63
- netrc (~> 0.8)
64
- rspec-core (3.7.0)
65
- rspec-support (~> 3.7.0)
66
- rspec-expectations (3.7.0)
67
- diff-lcs (>= 1.2.0, < 2.0)
68
- rspec-support (~> 3.7.0)
69
- rspec-mocks (3.7.0)
70
- diff-lcs (>= 1.2.0, < 2.0)
71
- rspec-support (~> 3.7.0)
72
- rspec-rails (3.7.1)
73
- actionpack (>= 3.0)
74
- activesupport (>= 3.0)
75
- railties (>= 3.0)
76
- rspec-core (~> 3.7.0)
77
- rspec-expectations (~> 3.7.0)
78
- rspec-mocks (~> 3.7.0)
79
- rspec-support (~> 3.7.0)
80
- rspec-support (3.7.0)
81
- thor (0.20.0)
82
- thread_safe (0.3.6)
83
- tzinfo (1.2.4)
84
- thread_safe (~> 0.1)
85
- unf (0.1.4)
86
- unf_ext
87
- unf_ext (0.0.7.4)
88
-
89
- PLATFORMS
90
- ruby
91
-
92
- DEPENDENCIES
93
- rest-client
94
- rspec-rails
95
-
96
- BUNDLED WITH
97
- 1.15.4