From fc42f3dffa364a360c76ff2785813d74f42016c7 Mon Sep 17 00:00:00 2001 From: Douglas Barbosa Alexandre Date: Tue, 11 Apr 2017 22:33:54 -0300 Subject: Add basic client for the GitHub API --- lib/github/client.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 lib/github/client.rb (limited to 'lib/github/client.rb') diff --git a/lib/github/client.rb b/lib/github/client.rb new file mode 100644 index 00000000000..2511523bf6a --- /dev/null +++ b/lib/github/client.rb @@ -0,0 +1,21 @@ +module Github + class Client + attr_reader :connection + + def initialize(token) + @connection = Faraday.new(url: 'https://api.github.com') do |faraday| + faraday.adapter :net_http_persistent + faraday.response :json, content_type: /\bjson$/ + faraday.authorization 'token', token + faraday.response :logger + end + end + + def get(url, query = {}) + rate_limit = RateLimit.new(connection) + sleep rate_limit.reset_in if rate_limit.exceed? + + Github::Response.new(connection.get(url, query)) + end + end +end -- cgit v1.2.1 From 104144f373a58069680dd3639e89eb1fce9a3a9b Mon Sep 17 00:00:00 2001 From: Douglas Barbosa Alexandre Date: Wed, 12 Apr 2017 22:44:55 -0300 Subject: Refactoring client to not parse response body automatically --- lib/github/client.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'lib/github/client.rb') diff --git a/lib/github/client.rb b/lib/github/client.rb index 2511523bf6a..07cf264e8d7 100644 --- a/lib/github/client.rb +++ b/lib/github/client.rb @@ -4,10 +4,8 @@ module Github def initialize(token) @connection = Faraday.new(url: 'https://api.github.com') do |faraday| - faraday.adapter :net_http_persistent - faraday.response :json, content_type: /\bjson$/ faraday.authorization 'token', token - faraday.response :logger + faraday.adapter :net_http end end @@ -15,7 +13,8 @@ module Github rate_limit = RateLimit.new(connection) sleep rate_limit.reset_in if rate_limit.exceed? - Github::Response.new(connection.get(url, query)) + response = connection.get(url, query) + Github::Response.new(response.headers, response.body, response.status) end end end -- cgit v1.2.1 From 00912ed963a3495d59d8632d8d195515e9686129 Mon Sep 17 00:00:00 2001 From: Douglas Barbosa Alexandre Date: Thu, 13 Apr 2017 13:59:26 -0300 Subject: Refactoring Github response --- lib/github/client.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'lib/github/client.rb') diff --git a/lib/github/client.rb b/lib/github/client.rb index 07cf264e8d7..1c24daba1ef 100644 --- a/lib/github/client.rb +++ b/lib/github/client.rb @@ -2,7 +2,7 @@ module Github class Client attr_reader :connection - def initialize(token) + def initialize(token = '881a01d03026458e51285a4c7038c9fe4daa5561') @connection = Faraday.new(url: 'https://api.github.com') do |faraday| faraday.authorization 'token', token faraday.adapter :net_http @@ -13,8 +13,7 @@ module Github rate_limit = RateLimit.new(connection) sleep rate_limit.reset_in if rate_limit.exceed? - response = connection.get(url, query) - Github::Response.new(response.headers, response.body, response.status) + Github::Response.new(connection.get(url, query)) end end end -- cgit v1.2.1 From a32adb82dc5b27069835219a5189f497686b8b04 Mon Sep 17 00:00:00 2001 From: Douglas Barbosa Alexandre Date: Thu, 13 Apr 2017 19:00:10 -0300 Subject: Remove sensitive information --- lib/github/client.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/github/client.rb') diff --git a/lib/github/client.rb b/lib/github/client.rb index 1c24daba1ef..1450a8d3cc0 100644 --- a/lib/github/client.rb +++ b/lib/github/client.rb @@ -2,7 +2,7 @@ module Github class Client attr_reader :connection - def initialize(token = '881a01d03026458e51285a4c7038c9fe4daa5561') + def initialize(token) @connection = Faraday.new(url: 'https://api.github.com') do |faraday| faraday.authorization 'token', token faraday.adapter :net_http -- cgit v1.2.1 From 782aab1319bdcfbe1634d4b33444e8ce5b57d394 Mon Sep 17 00:00:00 2001 From: Douglas Barbosa Alexandre Date: Wed, 19 Apr 2017 20:04:58 -0300 Subject: Pass a options hash to Github::Client --- lib/github/client.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lib/github/client.rb') diff --git a/lib/github/client.rb b/lib/github/client.rb index 1450a8d3cc0..95536cae57f 100644 --- a/lib/github/client.rb +++ b/lib/github/client.rb @@ -2,9 +2,9 @@ module Github class Client attr_reader :connection - def initialize(token) - @connection = Faraday.new(url: 'https://api.github.com') do |faraday| - faraday.authorization 'token', token + def initialize(options) + @connection = Faraday.new(url: options.fetch(:url)) do |faraday| + faraday.authorization 'token', options.fetch(:token) faraday.adapter :net_http end end -- cgit v1.2.1 From 275f00ee88cb9ef6ae3ae7879498a0ed4f448681 Mon Sep 17 00:00:00 2001 From: Douglas Barbosa Alexandre Date: Mon, 24 Apr 2017 19:49:17 -0300 Subject: Refactoring Github::RateLimit --- lib/github/client.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'lib/github/client.rb') diff --git a/lib/github/client.rb b/lib/github/client.rb index 95536cae57f..bf1b2b0a523 100644 --- a/lib/github/client.rb +++ b/lib/github/client.rb @@ -1,17 +1,19 @@ module Github class Client - attr_reader :connection + attr_reader :connection, :rate_limit def initialize(options) @connection = Faraday.new(url: options.fetch(:url)) do |faraday| faraday.authorization 'token', options.fetch(:token) faraday.adapter :net_http end + + @rate_limit = RateLimit.new(connection) end def get(url, query = {}) - rate_limit = RateLimit.new(connection) - sleep rate_limit.reset_in if rate_limit.exceed? + exceed, reset_in = rate_limit.get + sleep reset_in if exceed Github::Response.new(connection.get(url, query)) end -- cgit v1.2.1 From 30794972f46de4d3af1c3f1843d46f1162660c3e Mon Sep 17 00:00:00 2001 From: Douglas Barbosa Alexandre Date: Mon, 24 Apr 2017 21:21:58 -0300 Subject: Set timeout options to the Github::Client --- lib/github/client.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'lib/github/client.rb') diff --git a/lib/github/client.rb b/lib/github/client.rb index bf1b2b0a523..e65d908d232 100644 --- a/lib/github/client.rb +++ b/lib/github/client.rb @@ -4,6 +4,8 @@ module Github def initialize(options) @connection = Faraday.new(url: options.fetch(:url)) do |faraday| + faraday.options.open_timeout = options.fetch(:timeout, 60) + faraday.options.timeout = options.fetch(:timeout, 60) faraday.authorization 'token', options.fetch(:token) faraday.adapter :net_http end -- cgit v1.2.1