apigw 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8493d32a5105e968cbe8a2386e0f6fc8f3b12914
4
+ data.tar.gz: f6fed6db1ba06db25395b3d7cb459348d6a6ef5d
5
+ SHA512:
6
+ metadata.gz: 1dd5ac799c8aa241091680fdf24b727c607d81978555ffb2495ec89a3185c8b668e91e04b389f49ee935d129a793679453fc42d7d22b0da8c6f7da5a910510ae
7
+ data.tar.gz: b07d677f426f6bdf24e35cce1c58155a32f8864aab60e861505040d65fc17f74397a079df1609d5de4a587ace7ce5cc917f91c613c8a604947a1f1149d4c0320
@@ -0,0 +1,130 @@
1
+ NTT Communications API SDK(ruby)
2
+ ===
3
+
4
+ このライブラリは、NTT Communications APIsと対話的にアクセスするための簡易的なラッパーを提供します。
5
+ 各APIの仕様については、[デベロッパーポータル](https://developer.ntt.com/ja)のドキュメントを参照ください。
6
+
7
+ セットアップ
8
+ ---
9
+
10
+ ### Bundler での使用
11
+
12
+ Gemfile に下記を追記し、`bundle install`
13
+
14
+ ```ruby
15
+ gem 'apigw'
16
+ ```
17
+
18
+ システム要件
19
+ ---
20
+
21
+ Ruby 2.0.0 以上
22
+
23
+ 使い方
24
+ ---
25
+
26
+ 2015/10現在、クライアントは
27
+
28
+ * OAuth API
29
+ * Business Process API
30
+ * APILog API
31
+ * IAM API
32
+
33
+ の4つのAPIへのアクセスを提供します。
34
+
35
+ ### クライアントの生成
36
+
37
+ クライアントの生成時には、エンドポイントの情報を記載したYAML形式の設定ファイルへのパスを指定します。
38
+
39
+ ```ruby
40
+ client = ApiGW::Client.new(config_path: 'path/to/config.yml', environment: 'development')
41
+ ```
42
+
43
+ 設定ファイル例:
44
+
45
+ ```yaml
46
+ development:
47
+ host: "ホスト名"(e.g. 'https://api.ntt.com/')
48
+ api_version: "APIバージョン"(e.g. 'v1')
49
+ timeout: タイムアウト(ms)
50
+ verify_ssl: SSL証明書の検証を行うか(true / false)
51
+ debuggable: デバッグ情報を表示するか(true / false)
52
+ ```
53
+
54
+ ※ host と api_version は必須となります。
55
+
56
+ ### アクセストークンの取得
57
+
58
+ [デベロッパーポータル](https://developer.ntt.com/ja) にて払い出されたコンシューマーキーとシークレットキーを指定します。
59
+
60
+ ```ruby
61
+ response = client.oauth('コンシューマーキー', 'シークレットキー').access_token
62
+ access_token = response.body['accessToken']
63
+ ```
64
+
65
+ 全てのAPIのレスポンスは [Faraday::Response](https://github.com/lostisland/faraday/blob/master/lib/faraday/response.rb) オブジェクトで返却されます。
66
+ レスポンスのJSONは `Faraday::Response#body` からHash形式にパースされた状態で取得できます。
67
+ 各APIのレスポンス仕様については、[デベロッパーポータル](https://developer.ntt.com/ja)のドキュメントを参照ください。
68
+
69
+ ### Business Process API
70
+
71
+ OAuth API にて取得したアクセストークンとAPIパス、サービス名称を指定します。
72
+
73
+ ```ruby
74
+ response = client.business_process('アクセストークン').get('contracts', 'サービス名称')
75
+ items = response.body['items']
76
+ ```
77
+
78
+ 情報の参照には `ApiGW::BusinessProcess#get(...)` を、各種オーダには `ApiGW::BusinessProcess#post(...)` を利用します。
79
+
80
+ ### APILog API
81
+
82
+ OAuth API にて取得したアクセストークンと利用履歴の対象日を指定します。
83
+
84
+ ```ruby
85
+ response = client.api_log('アクセストークン').get('日付(YYYYMMDD)')
86
+ records = response.body['Records']
87
+ ```
88
+
89
+ ### その他API
90
+
91
+ examples/を参考にしてください。
92
+
93
+ ApiGW::ApiBase の継承について
94
+ ---
95
+
96
+ 新しいAPIに対応したクラスを作成する場合は、`ApiGW::ApiBase`のサブクラスを実装します。
97
+ 以下のドキュメント及び `ApiGW::BusinessProcess` や `ApiGW::ApiLog` を参考に実装してください。
98
+
99
+ ### 実装が必要なクラスメソッド
100
+
101
+ #### `api_name()`
102
+
103
+ APIの名称を返すメソッドです。
104
+
105
+ `https://api.ntt.com/v1/xxxxxx/yyyyyy` の `xxxxxx` の部分を返します。
106
+ 例えば `ApiGW::BusinessProcess` では `business-process` 、 `ApiGW:ApiLog` では `apilog` を返すよう実装されています。
107
+
108
+ api_name をオーバーライドしないと NotImplementedError が発生します。
109
+
110
+ #### `require_authorization?()`
111
+
112
+ このAPIに認証(アクセストークン)が必要かを返します。デフォルトでは false です。
113
+
114
+ trueを返すようにすると、リクエストヘッダーに `Authorization` を自動で付加するようになります。
115
+
116
+ ### リクエスト時に使用するインスタンスメソッド
117
+
118
+ #### `XXX_request(path, opts = {})`
119
+
120
+ `get_request`, `post_request`, `put_request`, `delete_request`, `options_request` の5つが定義されています。
121
+ それぞれ GET, POST, PUT, DELETE, OPTIONS のリクエストを実行し、結果を返します。
122
+
123
+ `path` は `https://api.ntt.com/v1/xxxxxx/yyyyyy` の `yyyyyy` の部分を渡します。
124
+ `yyyyyy` に相当するパスがないAPIの場合、 `nil` を指定します。
125
+
126
+
127
+ ライセンス
128
+ ---
129
+ Copyright © 2015 NTT Communications
130
+ [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0)
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'apigw/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "apigw"
8
+ spec.version = ApiGW::VERSION
9
+ spec.authors = ["NTT Communications APIGateway Teams"]
10
+ spec.email = ["apigateway@ntt.com"]
11
+
12
+ spec.summary = %q{NTT Communications APIGateway SDK}
13
+ spec.description = %q{NTT Communications APIGateway SDK}
14
+ spec.homepage = "https://github.com/nttcom/apigw-ruby-sdk"
15
+
16
+ spec.files = Dir["README.md", "lib/**/*.rb", "*.gemspec" ]
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_development_dependency "bundler", "~> 1.10"
20
+ spec.add_development_dependency "rake", "~> 10.0"
21
+ spec.add_development_dependency "rspec"
22
+ spec.add_development_dependency "guard-rspec"
23
+ spec.add_development_dependency "pry"
24
+
25
+ spec.add_dependency "faraday"
26
+ spec.add_dependency "faraday_middleware"
27
+ end
@@ -0,0 +1,9 @@
1
+ module ApiGW
2
+ require "apigw/version"
3
+ require "apigw/client"
4
+ require "apigw/api_base"
5
+ require "apigw/oauth"
6
+ require "apigw/business_process"
7
+ require "apigw/api_log"
8
+ require "apigw/iam"
9
+ end
@@ -0,0 +1,53 @@
1
+ # coding: utf-8
2
+ module ApiGW
3
+
4
+ class ApiBase
5
+
6
+ class << self
7
+ def api_name
8
+ raise NotImplementedError
9
+ end
10
+
11
+ def require_authorization?
12
+ false
13
+ end
14
+ end
15
+
16
+ def initialize(client, opts = {})
17
+ @client = client
18
+ @consumer_key = opts[:consumer_key]
19
+ @secret_key = opts[:secret_key]
20
+ @access_token = opts[:access_token]
21
+ end
22
+
23
+ # メソッドをまとめて定義
24
+ [:get, :post, :put, :delete, :options].each do |verb|
25
+ define_method "#{verb}_request" do |path, opts = {}|
26
+ request verb, path, opts
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def request(method, path, opts = {})
33
+ clz = self.class
34
+
35
+ url = path.nil? ? clz.api_name : "#{clz.api_name}/#{path}"
36
+
37
+ opts_copy = Marshal.load Marshal.dump(opts)
38
+ if clz.require_authorization?
39
+ apply_authorization_header_to_opts! opts_copy
40
+ end
41
+
42
+ @client.send_request method, url, opts_copy
43
+ end
44
+
45
+ def apply_authorization_header_to_opts!(opts)
46
+ headers = opts[:headers] || {}
47
+ headers['Authorization'] = "Bearer #{@access_token}"
48
+ opts[:headers] = headers
49
+ end
50
+
51
+ end
52
+
53
+ end
@@ -0,0 +1,24 @@
1
+ module ApiGW
2
+
3
+ class ApiLog < ApiBase
4
+
5
+ class << self
6
+ def api_name
7
+ 'apilog'
8
+ end
9
+
10
+ def require_authorization?
11
+ true
12
+ end
13
+ end
14
+
15
+ def get(target_date)
16
+ querys = {
17
+ targetDate: target_date
18
+ }
19
+ get_request nil, querys: querys
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -0,0 +1,37 @@
1
+ module ApiGW
2
+
3
+ class BusinessProcess < ApiBase
4
+
5
+ class << self
6
+ def api_name
7
+ 'business-process'
8
+ end
9
+
10
+ def require_authorization?
11
+ true
12
+ end
13
+ end
14
+
15
+ def get(path, service_name, opts = {})
16
+ get_request path, apply_service_name_to_opts(opts, service_name)
17
+ end
18
+
19
+ def post(path, service_name, data, opts = {})
20
+ opts_copy = Marshal.load Marshal.dump(opts)
21
+ opts_data = opts_copy[:data] || {}
22
+ opts_copy[:data] = opts_data.merge data
23
+ post_request path, apply_service_name_to_opts(opts_copy, service_name)
24
+ end
25
+
26
+ private
27
+
28
+ def apply_service_name_to_opts(opts, service_name)
29
+ opts_copy = Marshal.load Marshal.dump(opts)
30
+ querys = opts_copy[:querys] || {}
31
+ querys['serviceName'] = service_name
32
+ opts_copy[:querys] = querys
33
+ return opts_copy
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,113 @@
1
+ # coding: utf-8
2
+
3
+ module ApiGW
4
+
5
+ require 'apigw/logger'
6
+ require 'yaml'
7
+ require 'faraday'
8
+ require 'faraday_middleware'
9
+
10
+ class Client
11
+
12
+ DEFAULT_CONFIG = {
13
+ timeout: 30000,
14
+ verify_ssl: false,
15
+ debuggable: false
16
+ }
17
+
18
+ def initialize(opts = {})
19
+ if opts.include?(:config_path) && opts.include?(:environment)
20
+ # YAMLから取得したHashはキーがシンボルではなく文字列なので、キーをシンボルに直す必要あり
21
+ cfg_all = YAML.load_file opts[:config_path]
22
+ cfg = cfg_all[opts[:environment].to_s]
23
+ cfg.keys.each do |key|
24
+ cfg[key.to_sym] = cfg.delete key
25
+ end
26
+ config = cfg
27
+ elsif opts.include?(:config)
28
+ # こっちはキーがシンボルである想定
29
+ config = opts[:config]
30
+ else
31
+ config = {}
32
+ end
33
+
34
+ @host = config[:host]
35
+ if @host.nil?
36
+ raise "Configuration 'host' is not found."
37
+ end
38
+
39
+ @api_version = config[:api_version]
40
+ if @api_version.nil?
41
+ raise "Configuration 'api_version' is not found."
42
+ end
43
+
44
+ @verify_ssl = config[:verify_ssl] || DEFAULT_CONFIG[:verify_ssl]
45
+ @timeout = config[:timeout] || DEFAULT_CONFIG[:timeout]
46
+ @debuggable = config[:debuggable] || DEFAULT_CONFIG[:debuggable]
47
+ end
48
+
49
+ def oauth(consumer_key, secret_key)
50
+ ApiGW::OAuth.new self, consumer_key: consumer_key, secret_key: secret_key
51
+ end
52
+
53
+ def business_process(access_token)
54
+ ApiGW::BusinessProcess.new self, access_token: access_token
55
+ end
56
+
57
+ def api_log(access_token)
58
+ ApiGW::ApiLog.new self, access_token: access_token
59
+ end
60
+
61
+ def iam(access_token)
62
+ ApiGW::Iam.new self, access_token: access_token
63
+ end
64
+
65
+ def send_request(method, path, opts = {})
66
+ opts_copy = Marshal.load Marshal.dump(opts)
67
+ querys = opts_copy[:querys]
68
+ data = opts_copy[:data]
69
+ headers = opts_copy[:headers] || {}
70
+
71
+ headers[:accept] ||= "application/json"
72
+ headers[:accept_encoding] ||= "gzip, deflate"
73
+ headers[:connection] ||= "Keep-Alive"
74
+ headers[:host] ||= @host.split("://")[1]
75
+
76
+ unless method == :get
77
+ if data
78
+ headers[:content_length] ||= JSON.generate(data).length.to_s
79
+ else
80
+ headers[:content_length] ||= "0"
81
+ end
82
+ end
83
+
84
+ headers[:user_agent] ||= "apigw/#{VERSION}"
85
+
86
+ connection = Faraday.new("#{@host}/#{@api_version}") do |c|
87
+ # 順番を変えると正しく動かないので順番を変えないこと
88
+ c.request :json
89
+ c.response :json
90
+ if @debuggable
91
+ # デバッグモード時はカスタムロガーとカスタムアダプターを使用
92
+ c.use ApiGW::Logger
93
+ c.use ApiGW::NetHttp
94
+ else
95
+ c.adapter :net_http
96
+ end
97
+ c.ssl.verify = @verify_ssl
98
+ end
99
+
100
+ # Faradayのタイムアウトは秒単位
101
+ timeout_sec = @timeout / 1000
102
+ response = connection.run_request method, path, data, headers do |r|
103
+ r.params = querys unless querys.nil?
104
+ r.options.timeout = timeout_sec
105
+ r.options.open_timeout = timeout_sec
106
+ end
107
+
108
+ return response
109
+ end
110
+
111
+ end
112
+
113
+ end
@@ -0,0 +1,44 @@
1
+ module ApiGW
2
+
3
+ class Iam < ApiBase
4
+
5
+ class << self
6
+ def api_name
7
+ 'iam'
8
+ end
9
+
10
+ def require_authorization?
11
+ true
12
+ end
13
+ end
14
+
15
+ def get(path, opts = {})
16
+ get_request path, opts
17
+ end
18
+
19
+ def post(path, data)
20
+ opts_copy = {}
21
+ opts_data = opts_copy[:data] || {}
22
+ opts_copy[:data] = data
23
+ post_request path, opts_copy
24
+ end
25
+
26
+ def put(path)
27
+ put_request path
28
+ end
29
+
30
+ def delete(path)
31
+ delete_request path
32
+ end
33
+
34
+ private
35
+
36
+ def apply_service_name_to_opts(opts)
37
+ opts_copy = Marshal.load Marshal.dump(opts)
38
+ querys = opts_copy[:querys] || {}
39
+ opts_copy[:querys] = querys
40
+ return opts_copy
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,61 @@
1
+ # coding: utf-8
2
+ module ApiGW
3
+
4
+ require 'faraday'
5
+ require 'json'
6
+
7
+ class NetHttp < Faraday::Adapter::NetHttp
8
+ # オーバーライドして :http_version と :message をenvに入れておく
9
+ def perform_request(http, env)
10
+ http_response = super
11
+ env[:http_version] = http_response.http_version
12
+ env[:message] = http_response.message
13
+ http_response
14
+ end
15
+ end
16
+
17
+ class Logger < Faraday::Response::Middleware
18
+
19
+ def call(env)
20
+ lines = [ "" ]
21
+ if env.url.query.nil?
22
+ path = env.url.path
23
+ else
24
+ path = "#{env.url.path}?#{env.url.query}"
25
+ end
26
+ lines << "#{env.method.upcase} #{path} HTTP/1.1"
27
+ env.request_headers.keys.sort.each do |key|
28
+ lines << "#{key}: #{env.request_headers[key]}"
29
+ end
30
+ lines << pretty_json(env.body)
31
+ lines << ""
32
+
33
+ @app.call(env).on_complete do
34
+ lines << "HTTP/#{env[:http_version]} #{env.status} #{env[:message]}"
35
+ env.response_headers.keys.sort.each do |key|
36
+ lines << "#{key}: #{env.response_headers[key]}"
37
+ end
38
+ lines << pretty_json(env.body)
39
+ puts lines.join "\n"
40
+ end
41
+
42
+ end
43
+
44
+ private
45
+
46
+ def pretty_json(input)
47
+ if input.nil?
48
+ return ''
49
+ end
50
+ begin
51
+ # TODO inputがunicodeエスケープされててもパースされてしまう
52
+ return JSON.pretty_generate(JSON.parse(input))
53
+ rescue
54
+ # pass
55
+ end
56
+ return input
57
+ end
58
+
59
+ end
60
+
61
+ end
@@ -0,0 +1,22 @@
1
+ module ApiGW
2
+
3
+ class OAuth < ApiBase
4
+
5
+ class << self
6
+ def api_name
7
+ 'oauth'
8
+ end
9
+ end
10
+
11
+ def access_token
12
+ data = {
13
+ clientId: @consumer_key,
14
+ clientSecret: @secret_key,
15
+ grantType: 'client_credentials'
16
+ }
17
+ post_request 'accesstokens', data: data
18
+ end
19
+
20
+ end
21
+
22
+ end
@@ -0,0 +1,3 @@
1
+ module ApiGW
2
+ VERSION = "1.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,152 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: apigw
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - NTT Communications APIGateway Teams
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: guard-rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: faraday
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: faraday_middleware
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: NTT Communications APIGateway SDK
112
+ email:
113
+ - apigateway@ntt.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - README.md
119
+ - apigw.gemspec
120
+ - lib/apigw.rb
121
+ - lib/apigw/api_base.rb
122
+ - lib/apigw/api_log.rb
123
+ - lib/apigw/business_process.rb
124
+ - lib/apigw/client.rb
125
+ - lib/apigw/iam.rb
126
+ - lib/apigw/logger.rb
127
+ - lib/apigw/oauth.rb
128
+ - lib/apigw/version.rb
129
+ homepage: https://github.com/nttcom/apigw-ruby-sdk
130
+ licenses: []
131
+ metadata: {}
132
+ post_install_message:
133
+ rdoc_options: []
134
+ require_paths:
135
+ - lib
136
+ required_ruby_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ required_rubygems_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ requirements: []
147
+ rubyforge_project:
148
+ rubygems_version: 2.2.2
149
+ signing_key:
150
+ specification_version: 4
151
+ summary: NTT Communications APIGateway SDK
152
+ test_files: []