ä»æ¥ã¯Faradayã®è©±ãããã
yao ã®éçºã®åºæ¬æ¹éã¯ããå¿ è¦ã®ãªãä¾åãããªããã ããã©ãFaradayã¯æ°å°ãªãä¾ågemã«å«ã¾ãã¦ããã
çç±ã¯ã
- Faradayèªä½ã®ä¾ågemãå°ãªã
- å©ç¨ãããã¨ã§ã®ãã³ã¼ããã¹ãããªãããã¡ãªãããé常ã«å¤§ãã
ã¨èããããã
å ·ä½çã«ã¯ãFaradayã¯RackããããMiddlewareã§æ©è½è¿½å ããããã¿ã¼ã³ãæ¡ç¨ãã¦ãããããã¡ãã£ã¨ããä»äºããããããã®ã³ã¼ãããçãããããããããã®ã«ã§ããã以ä¸ã¯å ·ä½ä¾ã
ãã°ã¤ã³ãã¼ã¯ã³ã®ä¿æã¨åçºè¡
OpenStackã®APIå©ç¨ã®æµãã¯ã
- usernameããã¹ã¯ã¼ããããã³ãåï¼AWSã§ããVPCã²ã¨ã¤ã²ã¨ã¤ã¿ãããªæãï¼ãæå®ãããã°ã¤ã³ç¨ã¨ã³ããã¤ã³ããå©ã
- ãã®ã¨ã³ããã¤ã³ãããããã°ã¤ã³ç¨ã®ãã¼ã¯ã³ã¨ãåãµã¼ãã¹ï¼computeãnetworkãªã©ï¼ããããã®ã¨ã³ããã¤ã³ãURLä¸è¦§ããããã
- ãã¼ã¯ã³ã¨ã¨ã³ããã¤ã³ãæ å ±ãå©ç¨ãã¦ãã®å¾ã®APIãå©ã
ã¨ããæãã«ãªã£ã¦ããã OpenStack API Quick Start ã®éãã§ããã
ãã¼ã¯ã³ã¯ã "ac4e1c05084ba4365c1c38bfb1350000"
ã¿ãããªMD5ããã·ã¥é¢¨ã®å¤ã§ãããããããã« X-Auth-Token: "ac4e1c05084ba4365c1c38bfb1350000"
ã¨å«ãã°OKããªãããã®èªè¨¼æ¹å¼ãå¾®å¦ã«OAuth2ã¨éãã®ã§æ¢åã®Middlewareã使ãã¾ãããªã...
ä»åã¯ä»¥ä¸ã®ãããªMiddlewareãä½ã£ãã
class Faraday::Request::OSToken def initialize(app, token) @app = app @token = token end def call(env) if @token.expired? @token.reflesh(Yao.default_client.default) end env[:request_headers]['X-Auth-Token'] = @token.to_s @app.call(env) end end Faraday::Request.register_middleware os_token: -> { Faraday::Request::OSToken }
ã覧ã®éããããã¯ãªãã¨ããRack Middleware...ã¨ããæãã§ããã
ãã¤ã³ãã¯ã @token
èªä½ã¯æ®éã«Tokenãªãã¸ã§ã¯ããªã®ã§ãexpireãããã©ããã®æ
å ±ãææãã¦ããããªã®ã§ããTokenãexpireãã¦ãããããã³ãã©ã¼ã®ä¸ã§åæã«åçºè¡ãããã¨ãã£ãå®è£
ãå¯è½ã«ãªã£ã¦ããã¨ããã
ã¬ã¹ãã³ã¹ JSON ããã®ã¾ã¾ãã³ããã
OpenStackã®ããã¥ã¡ã³ããªã©ã«æ³å®ãããã¬ã¹ãã³ã¹ã¯ãããã ãã©ãå®éã®ãã®ãè¦ãæ¹ãç解ãæ©ãå ´åãå¤ãã
ããããã¬ã¹ãã³ã¹ãä¿åãããã¤ã¨ãã¦ã¯ vcr/vcr · GitHub ãæåã ãã©ããªã¯ã¨ã¹ããä¿åããããªã©ééç´ãªã®ã§ãããããã®ã³ã¼ãã§å¯¾å¿ããã
class Faraday::Response::OSResponseRecorder < Faraday::Response::Middleware def on_complete(env) require 'pathname' root = Pathname.new(File.expand_path('../../../tmp', __FILE__)) path = [env.method.to_s.upcase, env.url.path.gsub('/', '-')].join("-") + ".json" puts root.join(path) File.open(root.join(path), 'w') do |f| f.write env.body end end end Faraday::Response.register_middleware os_response_recorder: -> { Faraday::Response::OSResponseRecorder }
ã¬ã¹ãã³ã¹ã®Middlewareã®å ´åãRackã£ã½ããªãã¦ã on_complete(env)
ãé 次å¼ã°ãç¶ããã¨ããã¤ã¡ã¼ã¸ããã®ä¸ã§envã«å¯ä½ç¨ãèµ·ããã°ããã
ãã®Middlewareãæå¹ã«ããã¨ãé常éãAPIã¢ã¯ã»ã¹ããã¤ã¤ã PROJECT_ROOT/tmp
以ä¸ã«ãã®ã¾ã¾ã¬ã¹ãã³ã¹ã®JSONãæ®ãã
$ jq . < tmp/POST--v2.0-tokens.json { "access": { "token": { "issued_at": "2015-08-31T03:58:36.073232", "expires": "2015-09-01T03:58:36Z", "id": "31a5166533fd49f3b11b1cdce2000000", "tenant": { "description": "development environment", "enabled": true, "id": "b598bf98671c47e1b955f8c9660e0000", "name": "dev" } }, "serviceCatalog": [ { "endpoints": [ ....
ã¨ã©ã¼å¦ç
OpenStack APIã®ã¨ã©ã¼ã®æ±ãã¯ãååã¨ã㦠200..299
ã®ç¯å²å¤ã®ãã®ã§ããã°ãã¹ãã¼ã¿ã¹ã³ã¼ãã«å¯¾å¿ããã¨ã©ã¼ã«ãªãï¼300ç³»ã¯ãªãããªãããï¼ï¼ã
ãªã®ã§ãã¢ããã¿ã¼å´ã§å ±éåã§ããã
class Faraday::Response::OSErrorDetector < Faraday::Response::Middleware # TODO: Better handling, respecting official doc def on_complete(env) # Faraday::Env ã®çµã¿è¾¼ã¿ã¡ã½ãã # æ¬æ¥ãAPIãã¨ã«æ£å¸¸ãªã¹ãã¼ã¿ã¹ã³ã¼ãã®æå®ãããã®ã ããå¾ã§ãã return if env.success? raise Yao::ServerError.detect(env) end end Faraday::Response.register_middleware os_error_detector: -> { Faraday::Response::OSErrorDetector }
module Yao class ServerError < ::StandardError def self.detect(env) case env.status when 400 if env.body && env.body["computeFault"] ComputeFault.new(extract_message(env.body), env) elsif env.body && env.body.to_a.join.include?('NetworkNotFound') NetworkNotFound.new(extract_message(env.body), env) else BadRequest.new(extract_message(env.body), env) end when 401 Unauthorized.new(extract_message(env.body), env) when 404 if env.body && env.body["itemNotFound"] ItemNotFound.new(extract_message(env.body), env) else NotFound.new("The resource could not be found.", env) end when 405 BadMethod.new(extract_message(env.body), env) # ... ãããªæãã§å¤å®ãé å¼µã end end end end
ã¨ã©ã¼ç¨®é¡ã®å¤å®ã¯ãã¾ãé å¼µã£ã¦ã¡ã¿ããã°ã©ãã³ã°ããªãã¦ãããã¨æã£ã¦ãããã¦ãããã®è¾ºå¤åæè¿Goè¨èªãæ¸ãã¦ããã¨ãå¼·ãå½±é¿ãã¦ãããã
ã¨ã©ã¼ã«ã¤ãã¦ã¯ããã注ææ·±ããã³ããªã³ã°ãããå ´åãããã ãããããã®å ´åã¯Request pathã¨ãï¼ã¨ãããenvèªä½ï¼ããããæ å ±ãå ¥ãã¦ãã®ã§ãrescueãã¦ããããã®ãè¦ãã¨ããè¨è¨æ¹éã§è¯ãããã
ãããªæãã§Middlewareã¯ç°¡åã«æ¸ãã¦å©ãããã¨ãã話ã§ããã
ãç¥ãã
ããªã§ 0.0.2 ããªãªã¼ã¹ãã¾ããã
yao | RubyGems.org | your community gem host
ConoHaã®API ã§ãåãããããªããã¨ããæ°ãããã試ãã¦ãªããã©ã