m1_api 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/m1_api.rb +33 -115
- data/lib/m1_api/version.rb +3 -3
- data/lib/m1_api/yaml_helpers.rb +70 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 880305cc13e0cde33cdabd9c30e1d4b8dff25a337cb6363bbb574deff09fd656
|
4
|
+
data.tar.gz: 03c84cca83c77623a41fe1715bcad2b5396fc440a3e0fccb0d2494017e81fa7f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 518a8c44b1dedba1c6790d4383bf22102588c1c45208c733c38c5520cb5da97b7e89a715df7346ee1c1a5b115da48a5f2ac241b22c7e73936de703479eaad47e
|
7
|
+
data.tar.gz: 61ad53f7ddac12bf53d60fe9eb4c6f78158f366ea45fd9ba8525746106bdb7f1f1e45ac81d32b00b27bf438f80329065818f5bcede0608cdf22a9507cb0b9fb7
|
data/lib/m1_api.rb
CHANGED
@@ -1,127 +1,45 @@
|
|
1
|
-
|
2
|
-
require 'json'
|
3
|
-
require 'yaml'
|
4
|
-
require 'erb'
|
1
|
+
require_relative './m1_api/yaml_helpers.rb'
|
5
2
|
|
6
|
-
|
7
|
-
### change it to just authenticate once and use the same token
|
8
|
-
|
9
|
-
|
10
|
-
module M1API
|
3
|
+
class M1API
|
11
4
|
# autoload :CLI, 'm1_api/cli'
|
12
5
|
autoload :VERSION, 'm1_api/version'
|
13
6
|
|
14
|
-
|
15
|
-
|
16
|
-
@@api_config_file = "#{__dir__}/m1_api/api_configs.yml"
|
7
|
+
attr_accessor :api_config_file, :token
|
17
8
|
|
18
|
-
|
19
|
-
@@api_config_file = path
|
20
|
-
end
|
21
|
-
|
22
|
-
def read_credentials(credentials_file=nil)
|
23
|
-
if credentials_file
|
24
|
-
credentials = load_yaml(credentials_file)
|
25
|
-
{ username: credentials[:M1_USERNAME], password: credentials[:M1_PASSWORD] }
|
26
|
-
else
|
27
|
-
{username: ENV['M1_USERNAME'], password: ENV['M1_PASSWORD']}
|
28
|
-
end
|
29
|
-
end
|
9
|
+
api_config_file = "#{__dir__}/m1_api/api_configs.yml"
|
30
10
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
raise 'nyi'
|
39
|
-
api_config = load_yaml(api_configs_file)[api]
|
40
|
-
raise "No api '#{api}' defined in '#{api_configs_file}'" unless api_config
|
41
|
-
params = parse_api_config(api_config)
|
42
|
-
JSON.parse(RestClient.send(params['method'], params['url']), params['body'], params['headers'])
|
43
|
-
end
|
44
|
-
|
45
|
-
# might have to convert everything to sym first
|
46
|
-
def replace_dynamic_string(string, context)
|
47
|
-
raise 'input is not a string' unless string.is_a?(String)
|
48
|
-
replace_targets = string.split('>>>').map { |target| target.match(/<<<.*/).to_s }
|
49
|
-
replace_targets.each do |target|
|
50
|
-
key = target.match(/<<<(.*)/)
|
51
|
-
if key
|
52
|
-
temp_value = context
|
53
|
-
key[1].split(' ').each do |current_key|
|
54
|
-
raise "no value '#{current_key}' defined in context" unless temp_value.key?(current_key.to_sym)
|
55
|
-
temp_value = temp_value[current_key.to_sym]
|
56
|
-
end
|
57
|
-
string = string.gsub("#{target}>>>", temp_value)
|
58
|
-
end
|
59
|
-
end
|
60
|
-
string
|
61
|
-
end
|
62
|
-
|
63
|
-
# need something to deal with uri encode
|
64
|
-
def replace_dynamic_array(array, context)
|
65
|
-
raise 'input is not a array' unless array.is_a?(Array)
|
66
|
-
dup = array.clone
|
67
|
-
dup.each_with_index do |value, index|
|
68
|
-
dup[index] = replace_dynamic_string(value, context)
|
69
|
-
end
|
70
|
-
dup.join
|
71
|
-
end
|
72
|
-
|
73
|
-
def replace_dynamic_hash(hash, context = hash)
|
74
|
-
raise 'input is not a hash' unless hash.is_a?(Hash)
|
75
|
-
hash.each do |key, value|
|
76
|
-
if value.is_a?(String)
|
77
|
-
hash[key] = replace_dynamic_string(value, context)
|
78
|
-
elsif value.is_a?(Array)
|
79
|
-
hash[key] = replace_dynamic_array(value, context)
|
80
|
-
elsif value.is_a?(Hash)
|
81
|
-
hash[key] = replace_dynamic_hash(value, context)
|
82
|
-
end
|
83
|
-
end
|
84
|
-
hash
|
85
|
-
end
|
11
|
+
def initialize(username, password, api_config_file = nil)
|
12
|
+
@api_config_file = api_config_file || "#{__dir__}/m1_api/api_configs.yml"
|
13
|
+
credentials = { username: username, password: password }
|
14
|
+
res = YamlHelpers.call_api_from_yml(@api_config_file, 'authenticate', credentials)
|
15
|
+
raise "failed to authenticate:\n\t#{res}" unless res[:code] == 200 && res[:body]['data']['authenticate']['result']['didSucceed']
|
16
|
+
@token = res[:body]['data']['authenticate']['accessToken']
|
17
|
+
end
|
86
18
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
params.delete(nil)
|
94
|
-
res = RestClient.send(*params)
|
95
|
-
{ code: res.code, body: JSON.parse(res.body) }
|
96
|
-
rescue Exception => e
|
97
|
-
return { code: res.code, body: res.body } if res
|
98
|
-
puts "failed to call api for api #{api}: #{e}"
|
99
|
-
end
|
100
|
-
|
101
|
-
def authenticate(credentials_file = nil)
|
102
|
-
credentials = read_credentials(credentials_file)
|
103
|
-
res = call_api_from_yml(@@api_config_file, 'authenticate', credentials)
|
104
|
-
raise "failed to authenticate:\n\t#{res}" unless res[:code] == 200 && res[:body]['data']['authenticate']['result']['didSucceed']
|
105
|
-
res[:body]['data']['authenticate']['accessToken']
|
19
|
+
def self.read_credentials(credentials_file=nil)
|
20
|
+
if credentials_file
|
21
|
+
credentials = YamlHelpers.load_yaml(credentials_file)
|
22
|
+
{ username: credentials[:M1_USERNAME], password: credentials[:M1_PASSWORD] }
|
23
|
+
else
|
24
|
+
{username: ENV['M1_USERNAME'], password: ENV['M1_PASSWORD']}
|
106
25
|
end
|
26
|
+
end
|
107
27
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
end
|
28
|
+
def check_status
|
29
|
+
res = call_api_from_yml(@@api_config_file, 'check_status', token)
|
30
|
+
puts res.inspect
|
31
|
+
end
|
113
32
|
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
end
|
33
|
+
def query_accounts
|
34
|
+
accounts = {}
|
35
|
+
data = { token: @token }
|
36
|
+
id_res = YamlHelpers.call_api_from_yml(@api_config_file, 'list_account_ids', data)
|
37
|
+
ids = id_res[:body]['data']['viewer']['_accounts1NFCow']['edges'].map { |account| account['node']['id'] }
|
38
|
+
ids.each do |id|
|
39
|
+
data[:account_id] = id
|
40
|
+
account_res = YamlHelpers.call_api_from_yml(@api_config_file, 'query_account', data)
|
41
|
+
accounts[id] = account_res[:body]['data']['node']
|
42
|
+
end
|
43
|
+
accounts
|
126
44
|
end
|
127
45
|
end
|
data/lib/m1_api/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
class M1API
|
2
|
+
VERSION = Version = '0.0.4'
|
3
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'erb'
|
3
|
+
require 'rest-client'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
module YamlHelpers
|
7
|
+
module_function
|
8
|
+
|
9
|
+
def load_yaml(file_path)
|
10
|
+
YAML.load(ERB.new(File.read(file_path)).result) || {}
|
11
|
+
rescue SystemCallError
|
12
|
+
raise "Could not load file: '#{file_path}"
|
13
|
+
end
|
14
|
+
|
15
|
+
# might have to convert everything to sym first
|
16
|
+
def replace_dynamic_string(string, context)
|
17
|
+
raise 'input is not a string' unless string.is_a?(String)
|
18
|
+
replace_targets = string.split('>>>').map { |target| target.match(/<<<.*/).to_s }
|
19
|
+
replace_targets.each do |target|
|
20
|
+
key = target.match(/<<<(.*)/)
|
21
|
+
if key
|
22
|
+
temp_value = context
|
23
|
+
key[1].split(' ').each do |current_key|
|
24
|
+
raise "no value '#{current_key}' defined in context" unless temp_value.key?(current_key.to_sym)
|
25
|
+
temp_value = temp_value[current_key.to_sym]
|
26
|
+
end
|
27
|
+
string = string.gsub("#{target}>>>", temp_value)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
string
|
31
|
+
end
|
32
|
+
|
33
|
+
# need something to deal with uri encode
|
34
|
+
def replace_dynamic_array(array, context)
|
35
|
+
raise 'input is not a array' unless array.is_a?(Array)
|
36
|
+
dup = array.clone
|
37
|
+
dup.each_with_index do |value, index|
|
38
|
+
dup[index] = replace_dynamic_string(value, context)
|
39
|
+
end
|
40
|
+
dup.join
|
41
|
+
end
|
42
|
+
|
43
|
+
def replace_dynamic_hash(hash, context = hash)
|
44
|
+
raise 'input is not a hash' unless hash.is_a?(Hash)
|
45
|
+
hash.each do |key, value|
|
46
|
+
if value.is_a?(String)
|
47
|
+
hash[key] = replace_dynamic_string(value, context)
|
48
|
+
elsif value.is_a?(Array)
|
49
|
+
hash[key] = replace_dynamic_array(value, context)
|
50
|
+
elsif value.is_a?(Hash)
|
51
|
+
hash[key] = replace_dynamic_hash(value, context)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
hash
|
55
|
+
end
|
56
|
+
|
57
|
+
def call_api_from_yml(config_file, api, data = {})
|
58
|
+
config = load_yaml(config_file)[api.to_sym]
|
59
|
+
raise "no api defined for #{api}" unless config
|
60
|
+
context = config.merge data
|
61
|
+
parsed_config = replace_dynamic_hash(context)
|
62
|
+
params = [parsed_config[:method], parsed_config[:url], parsed_config[:body], parsed_config[:headers]]
|
63
|
+
params.delete(nil)
|
64
|
+
res = RestClient.send(*params)
|
65
|
+
{ code: res.code, body: JSON.parse(res.body) }
|
66
|
+
rescue Exception => e
|
67
|
+
return { code: res.code, body: res.body } if res
|
68
|
+
puts "failed to call api for api #{api}: #{e}"
|
69
|
+
end
|
70
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: m1_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yuan Feng
|
@@ -48,6 +48,7 @@ files:
|
|
48
48
|
- lib/m1_api.rb
|
49
49
|
- lib/m1_api/api_configs.yml
|
50
50
|
- lib/m1_api/version.rb
|
51
|
+
- lib/m1_api/yaml_helpers.rb
|
51
52
|
homepage: http://github.com/ynotgnef/m1_api
|
52
53
|
licenses:
|
53
54
|
- MIT
|