nexosis_api 2.0.0 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/lib/nexosis_api/algorithm.rb +22 -22
  3. data/lib/nexosis_api/algorithm_contestant.rb +43 -43
  4. data/lib/nexosis_api/calendar_jointarget.rb +35 -35
  5. data/lib/nexosis_api/classifier_result.rb +25 -25
  6. data/lib/nexosis_api/client/contest.rb +66 -66
  7. data/lib/nexosis_api/client/datasets.rb +155 -155
  8. data/lib/nexosis_api/client/imports.rb +141 -141
  9. data/lib/nexosis_api/client/models.rb +108 -108
  10. data/lib/nexosis_api/client/sessions.rb +213 -213
  11. data/lib/nexosis_api/client/views.rb +105 -105
  12. data/lib/nexosis_api/client.rb +118 -118
  13. data/lib/nexosis_api/column.rb +50 -50
  14. data/lib/nexosis_api/column_options.rb +38 -38
  15. data/lib/nexosis_api/column_role.rb +19 -19
  16. data/lib/nexosis_api/column_type.rb +23 -19
  17. data/lib/nexosis_api/dataset_data.rb +22 -22
  18. data/lib/nexosis_api/dataset_jointarget.rb +18 -18
  19. data/lib/nexosis_api/dataset_model.rb +26 -26
  20. data/lib/nexosis_api/dataset_summary.rb +33 -33
  21. data/lib/nexosis_api/http_exception.rb +28 -28
  22. data/lib/nexosis_api/impact_metric.rb +22 -22
  23. data/lib/nexosis_api/imports_response.rb +74 -79
  24. data/lib/nexosis_api/join.rb +63 -63
  25. data/lib/nexosis_api/link.rb +18 -18
  26. data/lib/nexosis_api/message.rb +19 -19
  27. data/lib/nexosis_api/metric.rb +16 -16
  28. data/lib/nexosis_api/model_summary.rb +66 -66
  29. data/lib/nexosis_api/paged_array.rb +35 -35
  30. data/lib/nexosis_api/predict_response.rb +35 -35
  31. data/lib/nexosis_api/session.rb +118 -118
  32. data/lib/nexosis_api/session_contest.rb +30 -30
  33. data/lib/nexosis_api/session_response.rb +33 -33
  34. data/lib/nexosis_api/session_result.rb +27 -27
  35. data/lib/nexosis_api/session_selection_metrics.rb +20 -20
  36. data/lib/nexosis_api/time_interval.rb +15 -15
  37. data/lib/nexosis_api/view_data.rb +14 -14
  38. data/lib/nexosis_api/view_definition.rb +64 -64
  39. data/lib/nexosis_api.rb +11 -11
  40. data/nexosisapi.gemspec +20 -20
  41. metadata +3 -3
@@ -1,155 +1,155 @@
1
- require 'csv'
2
-
3
- module NexosisApi
4
- class Client
5
- # Dataset-based API operations
6
- #
7
- # @see http://docs.nexosis.com/
8
- module Datasets
9
- # save data in a named dataset
10
- #
11
- # @param dataset_name [String] name to save the dataset
12
- # @param json_data [Hash] parsed json data
13
- # @return [NexosisApi::DatasetSummary] information about your upload
14
- # @note input json is to be a hash, do not send a json string via to_json.
15
- def create_dataset_json(dataset_name, json_data)
16
- create_dataset dataset_name, json_data.to_json, 'application/json'
17
- end
18
-
19
- # save data in a named dataset from csv content
20
- #
21
- # @param dataset_name [String] name to save the dataset
22
- # @param csv [CSV] csv content ready for reading
23
- # @return [NexosisApi::DatasetSummary] information about your upload
24
- def create_dataset_csv(dataset_name, csv)
25
- content = process_csv_to_s csv
26
- create_dataset dataset_name, content, 'text/csv'
27
- end
28
-
29
- # Gets the list of data sets that have been saved to the system, optionally filtering by partial name match.
30
- #
31
- # @param partial_name [String] if provided, all datasets returned will contain this string
32
- # @param page [int] page number for items in list
33
- # @param page_size [int] number of items in each page
34
- # @return [NexosisApi::PagedArray of NexosisApi::DatasetSummary] array of datasets found
35
- # @since 1.4 - added paging parameters
36
- def list_datasets(partial_name = '', page = 0, page_size = 50)
37
- list_dataset_url = '/data'
38
- query = {
39
- page: page,
40
- pageSize: page_size
41
- }
42
- query['partialName'] = partial_name unless partial_name.empty?
43
- response = self.class.get(list_dataset_url, headers: @headers, query: query)
44
- if response.success?
45
- NexosisApi::PagedArray.new(response.parsed_response,
46
- response.parsed_response['items']
47
- .map { |dr| NexosisApi::DatasetSummary.new(dr) })
48
- else
49
- raise HttpException.new("There was a problem listing datasets: #{response.code}.", "listing datasets with partial name #{partial_name}", response)
50
- end
51
- end
52
-
53
- # Get the data in the set, with paging, and optional projection.
54
- #
55
- # @param dataset_name [String] name of the dataset for which to retrieve data.
56
- # @param page_number [Integer] zero-based page number of results to retrieve
57
- # @param page_size [Integer] Count of results to retrieve in each page (max 1000).
58
- # @param query_options [Hash] options hash for limiting and projecting returned results
59
- # @note Query Options includes start_date as a DateTime or ISO 8601 compliant string,
60
- # end_date, also as a DateTime or string, and :include as an Array of strings indicating the columns to return.
61
- # The dates can be used independently and are inclusive. Lack of options returns all values within the given page.
62
- def get_dataset(dataset_name, page_number = 0, page_size = 50, query_options = {})
63
- response = get_dataset_internal(dataset_name, page_number, page_size, query_options)
64
- return NexosisApi::DatasetData.new(response.parsed_response) if response.success?
65
- raise HttpException.new("There was a problem getting the dataset: #{response.code}.", "getting dataset #{dataset_name}", response)
66
- end
67
-
68
- # Get the data in the set, written to a CSV file, optionally filtering it.
69
- #
70
- # @param dataset_name [String] name of the dataset for which to retrieve data.
71
- # @param page_number [Integer] zero-based page number of results to retrieve
72
- # @param page_size [Integer] Count of results to retrieve in each page (max 1000).
73
- # @param query_options [Hash] options hash for limiting and projecting returned results
74
- # @note Query Options includes start_date as a DateTime or ISO 8601 compliant string,
75
- # end_date, also as a DateTime or string, and :include as an Array of strings indicating the columns to return.
76
- # The dates can be used independently and are inclusive. Lack of options returns all values within the given page.
77
- # @example get page 1 with 20 results each page
78
- # NexosisApi.client.get_dataset_csv('MyDataset', 1, 20, {:include => 'sales'})
79
- def get_dataset_csv(dataset_name, page_number = 0, page_size = 50, query_options = {})
80
- response = get_dataset_internal(dataset_name, page_number, page_size, query_options, 'text/csv')
81
- return response.body if response.success?
82
- raise HttpException.new("There was a problem getting the dataset: #{response.code}.", "getting dataset #{dataset_name}", response)
83
- end
84
-
85
- # Remove data from a data set or the entire set.
86
- #
87
- # @param dataset_name [String] the name of the dataset from which to remove data
88
- # @param filter_options [Hash] filtering which data to remove
89
- # @note Options: start_date, end_date, cascade_forecast, cascade_sessions, cascade
90
- # - start_date - the first date on which to start removing data
91
- # - end_date - the last date on which to finish removing data
92
- # - cascade_forecast - will cascade deletes to all related forecasts
93
- # - cascade_session - will cascade deletes to all related sessions
94
- # - cascade_view - will cascade deletes to all related views (any part of join - think twice)
95
- # - cascase_model - will cascade deletes to all models created from this dataset
96
- # - cascade - will cascade deletes to all related forecasts and sessions
97
- # @example - request delete with cascade forecast
98
- # NexosisApi.client.remove_dataset('mydataset', {:cascade_forecast => true})
99
- def remove_dataset(dataset_name, filter_options = {})
100
- raise ArgumentError, 'dataset_name was not provided and is not optional ' if dataset_name.to_s.empty?
101
- dataset_remove_url = "/data/#{dataset_name}"
102
- query = {}
103
- if filter_options.empty? == false
104
- cascade_query = create_cascade_options(filter_options)
105
- query['cascade'] = cascade_query unless cascade_query.nil?
106
- query['startDate'] = [filter_options[:start_date].to_s] unless filter_options[:start_date].nil?
107
- query['endDate'] = [filter_options[:end_date].to_s] unless filter_options[:end_date].nil?
108
- end
109
- # normalizer = proc { |query_set| query_set.map { |key, value| value.map { |v| "#{key}=#{v}" } }.join('&') }
110
- response = self.class.delete(dataset_remove_url,
111
- headers: @headers,
112
- query: query,
113
- query_string_normalizer: ->(query_map) {array_query_normalizer(query_map)})
114
- return if response.success?
115
- raise HttpException.new("There was a problem removing the dataset: #{response.code}.", "removing dataset #{dataset_name}", response)
116
- end
117
-
118
- private
119
-
120
- # @private
121
- def create_dataset(dataset_name, content, content_type)
122
- raise ArgumentError, 'dataset_name was not provided and is not optional ' if dataset_name.to_s.empty?
123
- dataset_url = "/data/#{dataset_name}"
124
- headers = { 'api-key' => @api_key, 'Content-Type' => content_type }
125
- response = self.class.put(dataset_url, headers: headers, body: content)
126
- if response.success?
127
- NexosisApi::DatasetSummary.new(response)
128
- else
129
- raise HttpException.new("There was a problem uploading the dataset: #{response.code}.", "uploading dataset #{dataset_name}", response)
130
- end
131
- end
132
-
133
- # @private
134
- def get_dataset_internal(dataset_name, page_number = 0, page_size = 50, query_options = {}, content_type = 'application/json')
135
- raise ArgumentError, 'page size must be <= 1000 items per page' unless page_size <= 1000
136
- raise ArgumentError, 'dataset_name was not provided and is not optional' unless dataset_name.to_s.empty? == false
137
- dataset_url = "/data/#{dataset_name}"
138
- headers = { 'api-key' => @api_key, 'Accept' => content_type }
139
- self.class.get(dataset_url, headers: headers,
140
- query: create_query(page_number, page_size, query_options),
141
- query_string_normalizer: ->(query_map) { array_query_normalizer(query_map) })
142
- end
143
-
144
- # @private
145
- def create_cascade_options(option_hash)
146
- return nil if option_hash.nil?
147
- return %w[session view forecast model] if option_hash.key?(:cascade)
148
- options_set = []
149
- option_hash.each_key { |k| options_set << k.to_s.gsub(/cascade_/, '') if k.to_s.include? 'cascade_' }
150
- # HACK: required to be backward compatible with incorrect key names
151
- options_set.map { |s| s.chomp('s') }
152
- end
153
- end
154
- end
155
- end
1
+ require 'csv'
2
+
3
+ module NexosisApi
4
+ class Client
5
+ # Dataset-based API operations
6
+ #
7
+ # @see http://docs.nexosis.com/
8
+ module Datasets
9
+ # save data in a named dataset
10
+ #
11
+ # @param dataset_name [String] name to save the dataset
12
+ # @param json_data [Hash] parsed json data
13
+ # @return [NexosisApi::DatasetSummary] information about your upload
14
+ # @note input json is to be a hash, do not send a json string via to_json.
15
+ def create_dataset_json(dataset_name, json_data)
16
+ create_dataset dataset_name, json_data.to_json, 'application/json'
17
+ end
18
+
19
+ # save data in a named dataset from csv content
20
+ #
21
+ # @param dataset_name [String] name to save the dataset
22
+ # @param csv [CSV] csv content ready for reading
23
+ # @return [NexosisApi::DatasetSummary] information about your upload
24
+ def create_dataset_csv(dataset_name, csv)
25
+ content = process_csv_to_s csv
26
+ create_dataset dataset_name, content, 'text/csv'
27
+ end
28
+
29
+ # Gets the list of data sets that have been saved to the system, optionally filtering by partial name match.
30
+ #
31
+ # @param partial_name [String] if provided, all datasets returned will contain this string
32
+ # @param page [int] page number for items in list
33
+ # @param page_size [int] number of items in each page
34
+ # @return [NexosisApi::PagedArray of NexosisApi::DatasetSummary] array of datasets found
35
+ # @since 1.4 - added paging parameters
36
+ def list_datasets(partial_name = '', page = 0, page_size = 50)
37
+ list_dataset_url = '/data'
38
+ query = {
39
+ page: page,
40
+ pageSize: page_size
41
+ }
42
+ query['partialName'] = partial_name unless partial_name.empty?
43
+ response = self.class.get(list_dataset_url, headers: @headers, query: query)
44
+ if response.success?
45
+ NexosisApi::PagedArray.new(response.parsed_response,
46
+ response.parsed_response['items']
47
+ .map { |dr| NexosisApi::DatasetSummary.new(dr) })
48
+ else
49
+ raise HttpException.new("There was a problem listing datasets: #{response.code}.", "listing datasets with partial name #{partial_name}", response)
50
+ end
51
+ end
52
+
53
+ # Get the data in the set, with paging, and optional projection.
54
+ #
55
+ # @param dataset_name [String] name of the dataset for which to retrieve data.
56
+ # @param page_number [Integer] zero-based page number of results to retrieve
57
+ # @param page_size [Integer] Count of results to retrieve in each page (max 1000).
58
+ # @param query_options [Hash] options hash for limiting and projecting returned results
59
+ # @note Query Options includes start_date as a DateTime or ISO 8601 compliant string,
60
+ # end_date, also as a DateTime or string, and :include as an Array of strings indicating the columns to return.
61
+ # The dates can be used independently and are inclusive. Lack of options returns all values within the given page.
62
+ def get_dataset(dataset_name, page_number = 0, page_size = 50, query_options = {})
63
+ response = get_dataset_internal(dataset_name, page_number, page_size, query_options)
64
+ return NexosisApi::DatasetData.new(response.parsed_response) if response.success?
65
+ raise HttpException.new("There was a problem getting the dataset: #{response.code}.", "getting dataset #{dataset_name}", response)
66
+ end
67
+
68
+ # Get the data in the set, written to a CSV file, optionally filtering it.
69
+ #
70
+ # @param dataset_name [String] name of the dataset for which to retrieve data.
71
+ # @param page_number [Integer] zero-based page number of results to retrieve
72
+ # @param page_size [Integer] Count of results to retrieve in each page (max 1000).
73
+ # @param query_options [Hash] options hash for limiting and projecting returned results
74
+ # @note Query Options includes start_date as a DateTime or ISO 8601 compliant string,
75
+ # end_date, also as a DateTime or string, and :include as an Array of strings indicating the columns to return.
76
+ # The dates can be used independently and are inclusive. Lack of options returns all values within the given page.
77
+ # @example get page 1 with 20 results each page
78
+ # NexosisApi.client.get_dataset_csv('MyDataset', 1, 20, {:include => 'sales'})
79
+ def get_dataset_csv(dataset_name, page_number = 0, page_size = 50, query_options = {})
80
+ response = get_dataset_internal(dataset_name, page_number, page_size, query_options, 'text/csv')
81
+ return response.body if response.success?
82
+ raise HttpException.new("There was a problem getting the dataset: #{response.code}.", "getting dataset #{dataset_name}", response)
83
+ end
84
+
85
+ # Remove data from a data set or the entire set.
86
+ #
87
+ # @param dataset_name [String] the name of the dataset from which to remove data
88
+ # @param filter_options [Hash] filtering which data to remove
89
+ # @note Options: start_date, end_date, cascade_forecast, cascade_sessions, cascade
90
+ # - start_date - the first date on which to start removing data
91
+ # - end_date - the last date on which to finish removing data
92
+ # - cascade_forecast - will cascade deletes to all related forecasts
93
+ # - cascade_session - will cascade deletes to all related sessions
94
+ # - cascade_view - will cascade deletes to all related views (any part of join - think twice)
95
+ # - cascase_model - will cascade deletes to all models created from this dataset
96
+ # - cascade - will cascade deletes to all related forecasts and sessions
97
+ # @example - request delete with cascade forecast
98
+ # NexosisApi.client.remove_dataset('mydataset', {:cascade_forecast => true})
99
+ def remove_dataset(dataset_name, filter_options = {})
100
+ raise ArgumentError, 'dataset_name was not provided and is not optional ' if dataset_name.to_s.empty?
101
+ dataset_remove_url = "/data/#{dataset_name}"
102
+ query = {}
103
+ if filter_options.empty? == false
104
+ cascade_query = create_cascade_options(filter_options)
105
+ query['cascade'] = cascade_query unless cascade_query.nil?
106
+ query['startDate'] = [filter_options[:start_date].to_s] unless filter_options[:start_date].nil?
107
+ query['endDate'] = [filter_options[:end_date].to_s] unless filter_options[:end_date].nil?
108
+ end
109
+ # normalizer = proc { |query_set| query_set.map { |key, value| value.map { |v| "#{key}=#{v}" } }.join('&') }
110
+ response = self.class.delete(dataset_remove_url,
111
+ headers: @headers,
112
+ query: query,
113
+ query_string_normalizer: ->(query_map) {array_query_normalizer(query_map)})
114
+ return if response.success?
115
+ raise HttpException.new("There was a problem removing the dataset: #{response.code}.", "removing dataset #{dataset_name}", response)
116
+ end
117
+
118
+ private
119
+
120
+ # @private
121
+ def create_dataset(dataset_name, content, content_type)
122
+ raise ArgumentError, 'dataset_name was not provided and is not optional ' if dataset_name.to_s.empty?
123
+ dataset_url = "/data/#{dataset_name}"
124
+ headers = { 'api-key' => @api_key, 'Content-Type' => content_type }
125
+ response = self.class.put(dataset_url, headers: headers, body: content)
126
+ if response.success?
127
+ NexosisApi::DatasetSummary.new(response)
128
+ else
129
+ raise HttpException.new("There was a problem uploading the dataset: #{response.code}.", "uploading dataset #{dataset_name}", response)
130
+ end
131
+ end
132
+
133
+ # @private
134
+ def get_dataset_internal(dataset_name, page_number = 0, page_size = 50, query_options = {}, content_type = 'application/json')
135
+ raise ArgumentError, 'page size must be <= 1000 items per page' unless page_size <= 1000
136
+ raise ArgumentError, 'dataset_name was not provided and is not optional' unless dataset_name.to_s.empty? == false
137
+ dataset_url = "/data/#{dataset_name}"
138
+ headers = { 'api-key' => @api_key, 'Accept' => content_type }
139
+ self.class.get(dataset_url, headers: headers,
140
+ query: create_query(page_number, page_size, query_options),
141
+ query_string_normalizer: ->(query_map) { array_query_normalizer(query_map) })
142
+ end
143
+
144
+ # @private
145
+ def create_cascade_options(option_hash)
146
+ return nil if option_hash.nil?
147
+ return %w[session view forecast model] if option_hash.key?(:cascade)
148
+ options_set = []
149
+ option_hash.each_key { |k| options_set << k.to_s.gsub(/cascade_/, '') if k.to_s.include? 'cascade_' }
150
+ # HACK: required to be backward compatible with incorrect key names
151
+ options_set.map { |s| s.chomp('s') }
152
+ end
153
+ end
154
+ end
155
+ end
@@ -1,141 +1,141 @@
1
- require 'json'
2
- module NexosisApi
3
- class Client
4
- # Imports-based API operations
5
- #
6
- # @see http://docs.nexosis.com/
7
- module Imports
8
-
9
- # List all existing import requests
10
- #
11
- # @param dataset_name [String] optional name filter of dataset which was imported
12
- # @param page [int] page number for items in list
13
- # @param page_size [int] number of items in each page
14
- # @return [NexosisApi::PagedArray of NexosisApi::ImportsResponse]
15
- # @since 1.4 added paging parameters
16
- def list_imports(dataset_name = '', page = 0, page_size = 50)
17
- imports_url = '/imports'
18
- query = {
19
- dataSetName: dataset_name,
20
- page: page,
21
- pageSize: page_size
22
- }
23
- response = self.class.get(imports_url, headers: @headers, query: query)
24
- if (response.success?)
25
- NexosisApi::PagedArray.new(response.parsed_response,
26
- response.parsed_response['items']
27
- .map { |i| NexosisApi::ImportsResponse.new(i) })
28
- else
29
- raise HttpException.new("There was a problem getting the imports: #{response.code}.", "uploading dataset from s3 #{dataset_name}", response)
30
- end
31
- end
32
-
33
- # Import a file from AWS s3 as your dataset
34
- #
35
- # @param dataset_name [String] the name to give to the new dataset or existing dataset to which this data will be upserted
36
- # @param bucket_name [String] the AWS S3 bucket name in which the path will be found
37
- # @param path [String] the path within the bucket (usually file name)
38
- # @param region [String] the region in which your bucket exists. Defaults to us-east-1
39
- # @param credentials [Hash] :access_key_id and :secret_access_key for user with rights to read the target file.
40
- # @param column_metadata [Array of NexosisApi::Column] description of each column in target dataset. Optional.
41
- # @return [NexosisApi::ImportsResponse]
42
- # @see http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region for information on region names
43
- # @note If credentials are provided they will be encrypted at the server, used once, and then removed from storage.
44
- def import_from_s3(dataset_name, bucket_name, path, region = 'us-east-1', credentials = {}, column_metadata = [])
45
- raise ArgumentError, 'dataset_name was not provided and is not optional ' unless dataset_name.to_s.empty? == false
46
- raise ArgumentError, 'bucket_name was not provided and is not optional ' unless bucket_name.to_s.empty? == false
47
- raise ArgumentError, 'path was not provided and is not optional ' unless path.to_s.empty? == false
48
- s3_import_url = '/imports/s3'
49
- column_json = Column.to_json(column_metadata)
50
- body = {
51
- 'dataSetName' => dataset_name,
52
- 'bucket' => bucket_name,
53
- 'path' => path,
54
- 'region' => region,
55
- 'columns' => column_json
56
- }
57
- body['accessKeyId'] = credentials[:access_key_id] unless credentials.nil? || credentials[:access_key_id].nil?
58
- body['secretAccessKey'] = credentials[:secret_access_key] unless credentials.nil? || credentials[:secret_access_key].nil?
59
- response = self.class.post(s3_import_url, headers: @headers, body: body.to_json)
60
- raise HttpException.new("There was a problem importing from s3: #{response.code}.",
61
- "uploading dataset from s3 #{dataset_name}",
62
- response) unless response.success?
63
- NexosisApi::ImportsResponse.new(response.parsed_response)
64
- end
65
-
66
- # Get response back from import created previously. Presumably to check status.
67
- #
68
- # @param import_id [String] The id returned from a previous request to import
69
- # @return [NexosisApi::ImportsResponse]
70
- # @example get import
71
- # NexosisApi.client.retrieve_import('740dca2a-b488-4322-887e-fa473b1caa54')
72
- def retrieve_import(import_id)
73
- raise ArgumentError, 'import_id was not provided and is not optional ' unless import_id.to_s.empty? == false
74
- imports_url = "/imports/#{import_id}"
75
- response = self.class.get(imports_url, headers: @headers)
76
- raise HttpException.new("There was a problem getting the import #{response.code}.",
77
- "requesting an import #{import_id}", response) unless response.success?
78
- NexosisApi::ImportsResponse.new(response.parsed_response)
79
- end
80
-
81
- # Import a csv, json file (gzip'd or raw) from a Microsoft Azure storage blob
82
- #
83
- # @param dataset_name [String] the name to give to the new dataset or existing dataset to which this data will be upserted
84
- # @param connection_string [String] the azure blob storage connection string providing access to the file resource
85
- # @param container [String] the container in which the object is located.
86
- # @param blob_name [String] the name of the object to import, usually a file. Always csv or json content.
87
- # If folders have been used this will contain the full path within the container.
88
- # @param column_metadata [Array of NexosisApi::Column] description of each column in target dataset. Optional.
89
- # @return [NexosisApi::ImportsResponse]
90
- # @since 2.0.0
91
- # @note the connection string provided will be encrypted at the server, used once, and then removed from storage.
92
- def import_from_azure(dataset_name, connection_string, container, blob_name, column_metadata = [])
93
- raise ArgumentError, 'dataset_name was not provided and is not optional ' unless dataset_name.empty? == false
94
- raise ArgumentError, 'connection_string was not provided and is not optional ' unless connection_string.empty? == false
95
- raise ArgumentError, 'container was not provided and is not optional ' unless container.empty? == false
96
- raise ArgumentError, 'blob_name was not provided and is not optional ' unless blob_name.empty? == false
97
- azure_url = '/imports/azure'
98
- column_json = Column.to_json(column_metadata)
99
- body = {
100
- 'dataSetName' => dataset_name,
101
- 'connectionString' => connection_string,
102
- 'container' => container,
103
- 'blob' => blob_name,
104
- 'columns' => column_json
105
- }
106
- response = self.class.post(azure_url, headers: @headers, body: body.to_json)
107
- raise HttpException.new("There was a problem importing from azure: #{response.code}.",
108
- "uploading dataset from azure #{dataset_name}",
109
- response) unless response.success?
110
- NexosisApi::ImportsResponse.new(response.parsed_response)
111
- end
112
-
113
- # Import a csv or json file directly from any avaiable and reachable public endpoint.
114
- #
115
- # @param dataset_name [String] the name to give to the new dataset or existing dataset to which this data will be upserted
116
- # @param url [String] the url indicating where to find the file resource to import
117
- # @param column_metadata [Array of NexosisApi::Column] description of each column in target dataset. Optional.
118
- # @param options [Hash] may provide basic auth credentials or a 'content-type' value to identify csv or json content.
119
- # @return [NexosisApi::ImportsResponse]
120
- # @note imports depend on file extensions, so use a content type indicator if json or csv cannot be inferred.
121
- # @note Urls protected by basic auth can be accessed if given a userid and password in options
122
- # @since 2.0.0
123
- def import_from_url(dataset_name, url, column_metadata = [], options = {})
124
- raise ArgumentError, 'dataset_name was not provided and is not optional ' unless dataset_name.empty? == false
125
- raise ArgumentError, 'url was not provided and is not optional ' unless url.empty? == false
126
- endpoint_url = '/imports/url'
127
- column_json = Column.to_json(column_metadata)
128
- body = {
129
- 'dataSetName' => dataset_name,
130
- 'url' => url,
131
- 'columns' => column_json
132
- }
133
- response = self.class.post(endpoint_url, headers: @headers, body: body.to_json)
134
- raise HttpException.new("There was a problem importing from url: #{response.code}.",
135
- "uploading dataset from #{url}",
136
- response) unless response.success?
137
- NexosisApi::ImportsResponse.new(response.parsed_response)
138
- end
139
- end
140
- end
141
- end
1
+ require 'json'
2
+ module NexosisApi
3
+ class Client
4
+ # Imports-based API operations
5
+ #
6
+ # @see http://docs.nexosis.com/
7
+ module Imports
8
+
9
+ # List all existing import requests
10
+ #
11
+ # @param dataset_name [String] optional name filter of dataset which was imported
12
+ # @param page [int] page number for items in list
13
+ # @param page_size [int] number of items in each page
14
+ # @return [NexosisApi::PagedArray of NexosisApi::ImportsResponse]
15
+ # @since 1.4 added paging parameters
16
+ def list_imports(dataset_name = '', page = 0, page_size = 50)
17
+ imports_url = '/imports'
18
+ query = {
19
+ dataSetName: dataset_name,
20
+ page: page,
21
+ pageSize: page_size
22
+ }
23
+ response = self.class.get(imports_url, headers: @headers, query: query)
24
+ if (response.success?)
25
+ NexosisApi::PagedArray.new(response.parsed_response,
26
+ response.parsed_response['items']
27
+ .map { |i| NexosisApi::ImportsResponse.new(i) })
28
+ else
29
+ raise HttpException.new("There was a problem getting the imports: #{response.code}.", "uploading dataset from s3 #{dataset_name}", response)
30
+ end
31
+ end
32
+
33
+ # Import a file from AWS s3 as your dataset
34
+ #
35
+ # @param dataset_name [String] the name to give to the new dataset or existing dataset to which this data will be upserted
36
+ # @param bucket_name [String] the AWS S3 bucket name in which the path will be found
37
+ # @param path [String] the path within the bucket (usually file name)
38
+ # @param region [String] the region in which your bucket exists. Defaults to us-east-1
39
+ # @param credentials [Hash] :access_key_id and :secret_access_key for user with rights to read the target file.
40
+ # @param column_metadata [Array of NexosisApi::Column] description of each column in target dataset. Optional.
41
+ # @return [NexosisApi::ImportsResponse]
42
+ # @see http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region for information on region names
43
+ # @note If credentials are provided they will be encrypted at the server, used once, and then removed from storage.
44
+ def import_from_s3(dataset_name, bucket_name, path, region = 'us-east-1', credentials = {}, column_metadata = [])
45
+ raise ArgumentError, 'dataset_name was not provided and is not optional ' unless dataset_name.to_s.empty? == false
46
+ raise ArgumentError, 'bucket_name was not provided and is not optional ' unless bucket_name.to_s.empty? == false
47
+ raise ArgumentError, 'path was not provided and is not optional ' unless path.to_s.empty? == false
48
+ s3_import_url = '/imports/s3'
49
+ column_json = Column.to_json(column_metadata)
50
+ body = {
51
+ 'dataSetName' => dataset_name,
52
+ 'bucket' => bucket_name,
53
+ 'path' => path,
54
+ 'region' => region,
55
+ 'columns' => column_json
56
+ }
57
+ body['accessKeyId'] = credentials[:access_key_id] unless credentials.nil? || credentials[:access_key_id].nil?
58
+ body['secretAccessKey'] = credentials[:secret_access_key] unless credentials.nil? || credentials[:secret_access_key].nil?
59
+ response = self.class.post(s3_import_url, headers: @headers, body: body.to_json)
60
+ raise HttpException.new("There was a problem importing from s3: #{response.code}.",
61
+ "uploading dataset from s3 #{dataset_name}",
62
+ response) unless response.success?
63
+ NexosisApi::ImportsResponse.new(response.parsed_response)
64
+ end
65
+
66
+ # Get response back from import created previously. Presumably to check status.
67
+ #
68
+ # @param import_id [String] The id returned from a previous request to import
69
+ # @return [NexosisApi::ImportsResponse]
70
+ # @example get import
71
+ # NexosisApi.client.retrieve_import('740dca2a-b488-4322-887e-fa473b1caa54')
72
+ def retrieve_import(import_id)
73
+ raise ArgumentError, 'import_id was not provided and is not optional ' unless import_id.to_s.empty? == false
74
+ imports_url = "/imports/#{import_id}"
75
+ response = self.class.get(imports_url, headers: @headers)
76
+ raise HttpException.new("There was a problem getting the import #{response.code}.",
77
+ "requesting an import #{import_id}", response) unless response.success?
78
+ NexosisApi::ImportsResponse.new(response.parsed_response)
79
+ end
80
+
81
+ # Import a csv, json file (gzip'd or raw) from a Microsoft Azure storage blob
82
+ #
83
+ # @param dataset_name [String] the name to give to the new dataset or existing dataset to which this data will be upserted
84
+ # @param connection_string [String] the azure blob storage connection string providing access to the file resource
85
+ # @param container [String] the container in which the object is located.
86
+ # @param blob_name [String] the name of the object to import, usually a file. Always csv or json content.
87
+ # If folders have been used this will contain the full path within the container.
88
+ # @param column_metadata [Array of NexosisApi::Column] description of each column in target dataset. Optional.
89
+ # @return [NexosisApi::ImportsResponse]
90
+ # @since 2.0.0
91
+ # @note the connection string provided will be encrypted at the server, used once, and then removed from storage.
92
+ def import_from_azure(dataset_name, connection_string, container, blob_name, column_metadata = [])
93
+ raise ArgumentError, 'dataset_name was not provided and is not optional ' unless dataset_name.empty? == false
94
+ raise ArgumentError, 'connection_string was not provided and is not optional ' unless connection_string.empty? == false
95
+ raise ArgumentError, 'container was not provided and is not optional ' unless container.empty? == false
96
+ raise ArgumentError, 'blob_name was not provided and is not optional ' unless blob_name.empty? == false
97
+ azure_url = '/imports/azure'
98
+ column_json = Column.to_json(column_metadata)
99
+ body = {
100
+ 'dataSetName' => dataset_name,
101
+ 'connectionString' => connection_string,
102
+ 'container' => container,
103
+ 'blob' => blob_name,
104
+ 'columns' => column_json
105
+ }
106
+ response = self.class.post(azure_url, headers: @headers, body: body.to_json)
107
+ raise HttpException.new("There was a problem importing from azure: #{response.code}.",
108
+ "uploading dataset from azure #{dataset_name}",
109
+ response) unless response.success?
110
+ NexosisApi::ImportsResponse.new(response.parsed_response)
111
+ end
112
+
113
+ # Import a csv or json file directly from any avaiable and reachable public endpoint.
114
+ #
115
+ # @param dataset_name [String] the name to give to the new dataset or existing dataset to which this data will be upserted
116
+ # @param url [String] the url indicating where to find the file resource to import
117
+ # @param column_metadata [Array of NexosisApi::Column] description of each column in target dataset. Optional.
118
+ # @param options [Hash] may provide basic auth credentials or a 'content-type' value to identify csv or json content.
119
+ # @return [NexosisApi::ImportsResponse]
120
+ # @note imports depend on file extensions, so use a content type indicator if json or csv cannot be inferred.
121
+ # @note Urls protected by basic auth can be accessed if given a userid and password in options
122
+ # @since 2.0.0
123
+ def import_from_url(dataset_name, url, column_metadata = [], options = {})
124
+ raise ArgumentError, 'dataset_name was not provided and is not optional ' unless dataset_name.empty? == false
125
+ raise ArgumentError, 'url was not provided and is not optional ' unless url.empty? == false
126
+ endpoint_url = '/imports/url'
127
+ column_json = Column.to_json(column_metadata)
128
+ body = {
129
+ 'dataSetName' => dataset_name,
130
+ 'url' => url,
131
+ 'columns' => column_json
132
+ }
133
+ response = self.class.post(endpoint_url, headers: @headers, body: body.to_json)
134
+ raise HttpException.new("There was a problem importing from url: #{response.code}.",
135
+ "uploading dataset from #{url}",
136
+ response) unless response.success?
137
+ NexosisApi::ImportsResponse.new(response.parsed_response)
138
+ end
139
+ end
140
+ end
141
+ end