havenondemand 1.2.1 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bf8402d5a6a70ec39e6addf0955a060ad4efa7fa
4
- data.tar.gz: af5d71ee51055958c8e9964dc3d8fe9ec323837f
3
+ metadata.gz: 4ab5518ee1bc39d91414832b7b7302c3463e44a3
4
+ data.tar.gz: eea077070860c98331959cde25dd8a57ff7a6643
5
5
  SHA512:
6
- metadata.gz: 754c02e14e1e72521a319d220d519860faeaabcf7ece763a39f96eb248021b9172ad54791f9d454e684aa314509ec9e2e521a06397116e4e83d8279b5a46e93e
7
- data.tar.gz: 740809e9c54e707f4bfe832bbf732eff527e45a11256a4507251859397d6c54f298023288e1e343f85a77131dea50a6a7b8ccdd09d83bbef83af0d0567d939d1
6
+ metadata.gz: ec8d3d6ccbdfcea63f1ef51b2a15318c81259f271b7e5cedf890d770e2513079fef9b3dd38979055c16cbad6a9db4cddfdbf814cc54cfaa86a2a30cac518a25b
7
+ data.tar.gz: 68120f53a1e6f514132e02a097d54362623b04649dc9c7c977c77d7aca3c01a936b103a87c8133cf201c442862f15f0fa24e304da246b6345b09428e5f53b88a
data/README.md CHANGED
@@ -56,145 +56,160 @@ You can find your API key [here](https://www.haveondemand.com/account/api-keys.h
56
56
 
57
57
  `version` is an optional parameter (defaults to `'v1'`) and can be either `'v1'` or `'v2'`.
58
58
 
59
- ###Sending requests
59
+ ## Sending requests to the API - POST and GET
60
+ You can send requests to the API with either a POST or GET request, where POST requests are required for uploading files and recommended for larger size queries and GET requests are recommended for smaller size queries.
60
61
 
62
+ ### POST request
61
63
  ```ruby
62
- r = client.post('analyzesentiment', {:text=>'I like cats'})
64
+ client.get_request(hodApp, params, async, method(:callback))
63
65
  ```
64
- The client's *post* method takes the apipath that you're sending your request to as well as an object containing the parameters you want to send to the api. You do not need to send your apikey each time as the client will handle that automatically
66
+ * `hodApp` is the name of the API you are calling (see this [list]() for available endpoints and our [documentation](https://dev.havenondemand.com/apis) for descriptions of each of the APIs)
67
+ * `params` is a dictionary of parameters passed to the API
68
+ * `async` specifies if you are calling the API asynchronously or synchronously, which is either `true` or `false`, respectively
69
+ * `callback` which is a callback function which is executed when the response from the API is received. Specify 'nil' for returning response.
65
70
 
66
- ###Posting files
71
+ ### GET request
67
72
  ```ruby
68
- r = client.post('ocrdocument', {:file=>File.new("/path/to/file", 'rb')})
73
+ client.get_request(hodApp, params, async, method(:callback))
69
74
  ```
70
- Sending files is just as easy.
75
+ * `hodApp` is the name of the API you are calling (see this [list]() for available endpoints and our [documentation](https://dev.havenondemand.com/apis) for descriptions of each of the APIs)
76
+ * `params` is a dictionary of parameters passed to the API
77
+ * `async` specifies if you are calling the API asynchronously or synchronously, which is either `true` or `false`, respectively
78
+ * `callback` which is a callback function which is executed when the response from the API is received. Specify 'nil' for returning response.
71
79
 
80
+ ### POST request for combinations
72
81
  ```ruby
73
- r = client.post('ocrdocument', {:mode=>'photo',:file=>File.new("/path/to/file", 'rb')})
74
- r = client.post('ocrdocument', {:mode=>'photo',:file=>File.new("/path/to/file", 'rb')})
82
+ client.post_request_combination(hodApp, params, async, method(:callback))
75
83
  ```
76
- Any extra parameters should be added in the same way as regular calls, or in the data parameter.
84
+ * `hodApp` is the name of the combination API you are calling
85
+ * `params` is a dictionary of parameters passed to the API
86
+ * `async` specifies if you are calling the API asynchronously or synchronously, which is either `true` or `false`, respectively
87
+ * `callback` which is a callback function which is executed when the response from the API is received. Specify 'nil' for returning response.
77
88
 
78
- ###Parsing the output
79
89
 
90
+ ### GET request for combinations
80
91
  ```ruby
81
- myjson = r.json()
92
+ client.get_request_combination(hodApp, params, async, method(:callback))
82
93
  ```
94
+ * `hodApp` is the name of the combination API you are calling
95
+ * `params` is a dictionary of parameters passed to the API
96
+ * `async` specifies if you are calling the API asynchronously or synchronously, which is either `true` or `false`, respectively
97
+ * `callback` which is a callback function which is executed when the response from the API is received. Specify 'nil' for returning response.
83
98
 
84
- The object returned is a response object from the python [requests library](http://docs.python-requests.org/en/latest/) and can easily be turned to json.
99
+ ## Synchronous vs Asynchronous
100
+ Haven OnDemand's API can be called either synchronously or asynchronously. Users are encouraged to call asynchronously if they are POSTing large files that may require a lot of time to process. If not, calling them synchronously should suffice. For more information on the two, see [here](https://dev.havenondemand.com/docs/AsynchronousAPI.htm).
85
101
 
86
- ```ruby
87
- docs = myjson["documents"]
88
- array.each {|doc| puts doc["title"] }
89
- ```
90
-
91
- ### Combinations
92
-
93
- Haven OnDemand allows to chain two ore more APIs together to create customizable, reusable services. These combinations enable one data input to have unlimited transformations and processing all from a single API call.
102
+ ### Synchronous
103
+ To make a synchronous GET request to our Sentiment Analysis API
94
104
 
95
105
  ```ruby
96
- data = { :parameters => { :name => "name_of_input", :value => "value_of_input"} }
97
- r = client.post_combination('name_of_combination', data)
98
- puts r.json()
106
+ params = {:text=> 'I love Haven OnDemand!'}
107
+ hodApp = "analyzesentiment"
108
+ response = client.get_request(hodApp, params, false, nil)
99
109
  ```
110
+ where the response will be in the `response` variable.
100
111
 
101
- **Note: using local files and publicly accessible URLs is not supported by this wrapper**
102
-
103
- To find out more about combinations and how to create one, see [here](https://dev.havenondemand.com/combination/home).
104
-
105
- ###Indexing
106
-
107
- **Creating an index**
112
+ ### Asynchronous
113
+ To make an asynchronous POST request to our Sentiment Analysis API
108
114
 
109
115
  ```ruby
110
- index = client.createIndex("mytestindex", flavor="explorer")
116
+ params = {:text=> 'I love Haven OnDemand!'}
117
+ hodApp = "analyzesentiment"
118
+ response_async = post_request(hodApp, params, async=true, nil)
119
+ jobID = response_async['jobID']
111
120
  ```
121
+ which will return back the job ID of your call. Use the job ID to call the get_job_status() or get_job_result() to get the result.
112
122
 
113
- An Index object can easily be created
123
+ #### Getting the results of an asynchronous request - Status API and Result API
114
124
 
115
- **Fetching indexes/an index**
125
+ ##### Status API
126
+ The Status API checks to see the status of your job request. If it is finished processing, it will return the result. If not, it will return you the status of the job.
116
127
 
117
128
  ```ruby
118
- index = client.getIndex('myindex')
129
+ client.get_job_status(jobID, method(:callback))
119
130
  ```
120
- The getIndex call will return an hodindex Index object but will not check for existence.
131
+ * `jobID` is the job ID of request returned after performing an asynchronous request
132
+ * `callback` which is a callback function which is executed when the response from the API is received. Specify 'nil' for returning response.
121
133
 
134
+ To get the status, or job result if the job is complete
122
135
  ```ruby
123
- indexes = client.listIndexes()
124
- indexes.fetch('myindex', client.createIndex('myindex'))
136
+ client.get_job_status(jobID, method(:callback))
125
137
  ```
126
138
 
127
- Here we first check the list of our indexes and return a newly created index if the index does not already exist
128
-
129
- **Deleting an index**
139
+ ##### Result API
140
+ The Result API checks the result of your job request. If it is finished processing, it will return the result. If it not, the call the wait until the result is returned or until it times out. **It is recommended to use the Status API over the Result API to avoid time outs**
130
141
 
131
142
  ```ruby
132
- index.delete()
133
- client.deleteIndex('myindex')
143
+ client.get_job_result(jobID, method(:callback))
134
144
  ```
135
- An index can be deleted in two equivalent ways
136
-
137
- **Indexing documents**
145
+ * `jobID` is the job ID of request returned after performing an asynchronous request
146
+ * `callback` which is a callback function which is executed when the response from the API is received. Specify 'nil' for returning response.
138
147
 
148
+ To get the result
139
149
  ```ruby
140
- doc1 = HODDoc.new({title: "title1", reference: "doc1", content: "my content 1"})
141
- doc2 = HODDoc.new({title: "title2", reference: "doc2", content: "my content 2"})
142
- ```
143
- Documents can be created as regular python objects
144
-
145
- ```
146
- index.addDoc(doc1)
147
- index.addDocs([doc1,doc2])
148
- ```
149
-
150
- They can be added directly one at a time or in a batch.
151
-
152
- ```
153
- for doc in docs:
154
- index.pushDoc(doc)
155
- index.commit()
156
- ```
157
-
158
- An alternative to *addDocs* and easy way to keep batch documents is to use the pushDoc method, the index will keep in memory a list of the documents it needs to index.
159
-
160
- ```
161
- if index.countDocs()>10:
162
- index.commit()
150
+ response = client.get_job_result(jobID, nil)
163
151
  ```
164
152
 
165
- It makes it easy to batch together groups of documents.
166
-
167
- ####Indexing - Connectors
153
+ ## Using a callback function
154
+ Most methods allow optional callback functions which are executed when the response of the API is received.
168
155
 
169
156
  ```ruby
170
- client = HODClient.new(apikey, version)
171
- conn = HODConnector.new("mytestconnector", client)
172
- conn.create(type="web", config = { "url" => "http://www.havenondemand.com" })
173
- conn.delete()
174
- ```
157
+ def syncCallback(response)
158
+ response = $parser.parse_payload(response)
159
+ if response != nil
160
+ puts response
161
+ else
162
+ errors = $parser.get_last_errors()
163
+ errors.each { |error|
164
+ eCode = error["error"]
165
+ if eCode == ErrorCode::QUEUED
166
+ jobID = error["jobID"]
167
+ client.get_job_status(jobID, method(:syncCallback))
168
+ elsif eCode == ErrorCode::IN_PROGRESS
169
+ jobID = error["jobID"]
170
+ client.get_job_status(jobID, method(:syncCallback))
171
+ else
172
+ puts eCode
173
+ puts error["detail"]
174
+ puts error["reason"]
175
+ end
176
+ }
177
+ end
178
+ end
175
179
 
180
+ def asyncCallback(response)
181
+ jobID = $parser.parse_jobid(response)
182
+ client.get_job_status(jobID, method(:syncCallback))
183
+ end
176
184
 
177
- ### Asynchronous request
185
+ params = {:text=> 'I love Haven OnDemand!'}
186
+ hodApp = "analyzesentiment"
187
+ client.post_request(hodApp, params, true, method(:asyncCallback))
188
+ ```
178
189
 
179
- For each call the Async parameter can be set to true to send an asynchronous request.
190
+ ## POSTing files
191
+ POSTing files is just as easy. Simply include the path to the file you're POSTing in the parameters
180
192
 
181
193
  ```ruby
182
- r = client.post('analyzesentiment', {:text => 'I like cats'}, async = true)
183
- print r.json()
184
-
185
- # will return status of call, queued or finished
186
- puts r.status().json()
187
- # Will wait until result to return
188
- puts r.result().json()
194
+ params = {:file=> 'path/to/file.jpg'}
195
+ hodApp = "ocrdocument"
196
+ response = hodClient.post_request(hodApp, params, false, nil)
197
+ puts response
189
198
  ```
190
199
 
191
- Same thing for indexing.
200
+ ## POSTing files with post_request_combination
201
+ POSTing files to a combination API is slightly different from POSting files to a standalone API.
192
202
 
193
203
  ```ruby
194
- r = index.commit(async = true)
204
+ files = [{"file1_input_name"=>"file1name.xxx"},{"file2_input_name"=>"file2name.xxx"}]
205
+ params = {}
206
+ params[:file] = files
207
+ hodApp = "name_of_combination_api"
208
+ response = client.post_request_combination(hodApp, params, false, nil)
195
209
  ```
196
210
 
197
-
211
+ ## License
212
+ Licensed under the MIT License.
198
213
 
199
214
  ## Contributing
200
215
  We encourage you to contribute to this repo! Please send pull requests with modified and updated code.
@@ -0,0 +1,61 @@
1
+ require_relative "../lib/havenondemand.rb"
2
+
3
+ $client = HODClient.new("YOUR_API_KEY", "v1")
4
+ $parser = HODResponseParser.new()
5
+
6
+ def syncCallback(response)
7
+ response = $parser.parse_payload(response)
8
+ if response != nil
9
+ puts response
10
+ else
11
+ errors = $parser.get_last_errors()
12
+ errors.each { |error|
13
+ eCode = error["error"]
14
+ if eCode == ErrorCode::QUEUED
15
+ jobID = error["jobID"]
16
+ sleep(2)
17
+ $client.get_job_status(jobID, method(:syncCallback))
18
+ elsif eCode == ErrorCode::IN_PROGRESS
19
+ jobID = error["jobID"]
20
+ sleep(5)
21
+ $client.get_job_status(jobID, method(:syncCallback))
22
+ else
23
+ puts eCode
24
+ puts error["detail"]
25
+ puts error["reason"]
26
+ end
27
+ }
28
+ end
29
+ end
30
+
31
+ def asyncCallback(response)
32
+ jobID = $parser.parse_jobid(response)
33
+ if jobID != nil
34
+ $client.get_job_status(jobID, method(:syncCallback))
35
+ else
36
+ errors = $parser.get_last_errors()
37
+ errors.each { |error|
38
+ eCode = error["error"]
39
+ puts eCode
40
+ puts error["detail"]
41
+ puts error["reason"]
42
+ }
43
+ end
44
+ end
45
+
46
+ # supposed that a combination API takes 2 input files. ! image file for extracting text from image
47
+ # and 1 audio file for extracting text from speech.
48
+ # And the names of the file input are ocrFile and speechFile.
49
+ # And the name of the combination API is "multiplefileinput"
50
+
51
+ files = [{"ocrFile"=>"review.jpg"},{"speechFile"=>"attendant_test.mp3"}]
52
+ # OR
53
+ =begin
54
+ #file1 = {"ocrFile"=>"review.jpg"}
55
+ #file2 = {"speechFile"=>"attendant_test.mp3"}
56
+ files = []
57
+ files.push(file1)
58
+ files.push(file2)
59
+ =end
60
+ params["file"] = files
61
+ $client.post_request_combination('multiplefileinput', params, true, method(:asyncCallback))
@@ -0,0 +1,50 @@
1
+ require_relative "../lib/havenondemand.rb"
2
+
3
+ $client = HODClient.new("YOUR_API_KEY", "v1")
4
+ $parser = HODResponseParser.new()
5
+
6
+ # call Haven OnDemand APIs using asynchronous mode
7
+
8
+ def syncCallback(response)
9
+ response = $parser.parse_payload(response)
10
+ if response != nil
11
+ puts response
12
+ else
13
+ errors = $parser.get_last_errors()
14
+ errors.each { |error|
15
+ eCode = error["error"]
16
+ if eCode == ErrorCode::QUEUED
17
+ jobID = error["jobID"]
18
+ $client.get_job_status(jobID, method(:syncCallback))
19
+ elsif eCode == ErrorCode::IN_PROGRESS
20
+ jobID = error["jobID"]
21
+ $client.get_job_status(jobID, method(:syncCallback))
22
+ else
23
+ puts eCode
24
+ puts error["detail"]
25
+ puts error["reason"]
26
+ end
27
+ }
28
+ end
29
+ end
30
+
31
+ def asyncCallback(response)
32
+ jobID = $parser.parse_jobid(response)
33
+ if jobID != nil
34
+ $client.get_job_status(jobID, method(:syncCallback))
35
+ else
36
+ errors = $parser.get_last_errors()
37
+ errors.each { |error|
38
+ eCode = error["error"]
39
+ puts eCode
40
+ puts error["detail"]
41
+ puts error["reason"]
42
+ }
43
+ end
44
+ end
45
+
46
+ params = {}
47
+ params["url"] = "http://www.cnn.com"
48
+ params["entity_type"] = ['people_eng','places_eng']
49
+ hodApp = 'extractentities'
50
+ $client.post_request(hodApp, params, true, method(:asyncCallback))
@@ -0,0 +1,34 @@
1
+ require_relative "../lib/havenondemand.rb"
2
+
3
+ $client = HODClient.new("YOUR_API_KEY", "v1")
4
+ $parser = HODResponseParser.new()
5
+
6
+ def syncCallback(response)
7
+ response = $parser.parse_payload(response)
8
+ if response != nil
9
+ puts response
10
+ else
11
+ errors = $parser.get_last_errors()
12
+ errors.each { |error|
13
+ eCode = error["error"]
14
+ if eCode == ErrorCode::QUEUED
15
+ jobID = error["jobID"]
16
+ $client.get_job_status(jobID, method(:syncCallback))
17
+ elsif eCode == ErrorCode::IN_PROGRESS
18
+ jobID = error["jobID"]
19
+ $client.get_job_status(jobID, method(:syncCallback))
20
+ else
21
+ puts eCode
22
+ puts error["detail"]
23
+ puts error["reason"]
24
+ end
25
+ }
26
+ end
27
+ end
28
+
29
+ # call Haven OnDemand APIs using synchronous mode
30
+ params = {}
31
+ params["url"] = "http://www.cnn.com"
32
+ params["entity_type"] = ['people_eng','places_eng']
33
+ hodApp = 'extractentities'
34
+ $client.post_request(hodApp, params, false, method(:syncCallback))
@@ -0,0 +1,52 @@
1
+ require_relative "../lib/havenondemand.rb"
2
+
3
+ $client = HODClient.new("YOUR_API_KEY", "v1")
4
+ $parser = HODResponseParser.new()
5
+
6
+ # call Haven OnDemand APIs using asynchronous mode
7
+
8
+ def syncCallback(response)
9
+ response = $parser.parse_payload(response)
10
+ if response != nil
11
+ puts response
12
+ else
13
+ errors = $parser.get_last_errors()
14
+ errors.each { |error|
15
+ eCode = error["error"]
16
+ if eCode == ErrorCode::QUEUED
17
+ jobID = error["jobID"]
18
+ sleep(2)
19
+ $client.get_job_status(jobID, method(:syncCallback))
20
+ elsif eCode == ErrorCode::IN_PROGRESS
21
+ jobID = error["jobID"]
22
+ sleep(5)
23
+ $client.get_job_status(jobID, method(:syncCallback))
24
+ else
25
+ puts eCode
26
+ puts error["detail"]
27
+ puts error["reason"]
28
+ end
29
+ }
30
+ end
31
+ end
32
+
33
+ def asyncCallback(response)
34
+ jobID = $parser.parse_jobid(response)
35
+ if jobID != nil
36
+ $client.get_job_status(jobID, method(:syncCallback))
37
+ else
38
+ errors = $parser.get_last_errors()
39
+ errors.each { |error|
40
+ eCode = error["error"]
41
+ puts eCode
42
+ puts error["detail"]
43
+ puts error["reason"]
44
+ }
45
+ end
46
+ end
47
+
48
+ params = {}
49
+ params["file"] = "pathto/filename.xxx"
50
+ params["entity_type"] = ['people_eng','places_eng']
51
+ hodApp = 'extractentities'
52
+ $client.post_request(hodApp, params, true, method(:asyncCallback))