Class: Todoist::Util::NetworkHelper
- Inherits:
-
Object
- Object
- Todoist::Util::NetworkHelper
- Defined in:
- lib/todoist/util/network_helper.rb
Instance Method Summary collapse
- #configure_http(command) ⇒ Object
- #configure_request(command, params) ⇒ Object
-
#get_multipart_response(command, params = {}) ⇒ Object
Files need to be of class UploadIO.
- #get_response(command, params = {}, token = true) ⇒ Object
- #get_sync_response(params) ⇒ Object
-
#initialize(client) ⇒ NetworkHelper
constructor
A new instance of NetworkHelper.
-
#multipart_file(file) ⇒ Object
Prepares a file for multipart upload.
- #queue(command, callback = nil) ⇒ Object
- #sync ⇒ Object
- #throttle_request(http, request) ⇒ Object
Constructor Details
#initialize(client) ⇒ NetworkHelper
Returns a new instance of NetworkHelper.
14 15 16 17 18 19 20 21 |
# File 'lib/todoist/util/network_helper.rb', line 14 def initialize(client) @client = client @command_cache = Concurrent::Array.new([]) @command_mutex = Mutex.new @temp_id_callback_cache = Concurrent::Array.new([]) @last_request_time = 0.0 end |
Instance Method Details
#configure_http(command) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/todoist/util/network_helper.rb', line 23 def configure_http(command) http = Net::HTTP.new(Config.getURI()[command].host, Config.getURI()[command].port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE return http end |
#configure_request(command, params) ⇒ Object
30 31 32 33 34 35 |
# File 'lib/todoist/util/network_helper.rb', line 30 def configure_request(command, params) request = Net::HTTP::Post.new(Config.getURI()[command].request_uri) request['Authorization'] = 'Bearer ' + params[:token] request.set_form_data(params) return request end |
#get_multipart_response(command, params = {}) ⇒ Object
Files need to be of class UploadIO
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/todoist/util/network_helper.rb', line 39 def get_multipart_response(command, params={}) token = {token: @client.token} http = configure_http(command) url = Config.getURI()[command] http.start do req = Net::HTTP::Post::Multipart.new(url, token.merge(params)) response = http.request(req) begin return JSON.parse(response.body) rescue JSON::ParserError return response.body end end end |
#get_response(command, params = {}, token = true) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/todoist/util/network_helper.rb', line 54 def get_response(command, params ={}, token = true) token = token ? {token: @client.token} : {} http = configure_http(command) request = configure_request(command, token.merge(params)) retry_after_secs = Todoist::Config.retry_time # Hack to fix encoding issues with Net:HTTP for login case request.body = request.body.gsub '%40', '@' unless token while true response = throttle_request(http, request) case response.code.to_i when 200 begin return JSON.parse(response.body) rescue JSON::ParserError return response.body end when 400 raise StandardError, "HTTP 400 Error - The request was incorrect." when 401 raise StandardError, "HTTP 401 Error - Authentication is required, and has failed, or has not yet been provided." when 403 raise StandardError, "HTTP 403 Error - The request was valid, but for something that is forbidden." when 404 raise StandardError, "HTTP 404 Error - The requested resource could not be found." when 429 puts("Encountered 429 - retry after #{retry_after_secs}") sleep(retry_after_secs) retry_after_secs *= Todoist::Config.retry_time when 500 raise StandardError, "HTTP 500 Error - The request failed due to a server error." when 503 raise StandardError, "HTTP 503 Error - The server is currently unable to handle the request." end end end |
#get_sync_response(params) ⇒ Object
131 132 133 |
# File 'lib/todoist/util/network_helper.rb', line 131 def get_sync_response(params) get_response(Config::TODOIST_SYNC_COMMAND, params) end |
#multipart_file(file) ⇒ Object
Prepares a file for multipart upload
104 105 106 107 108 |
# File 'lib/todoist/util/network_helper.rb', line 104 def multipart_file(file) filename = File.basename(file) mime_type = MimeMagic.by_path(filename).type return UploadIO.new(file, mime_type, filename) end |
#queue(command, callback = nil) ⇒ Object
110 111 112 113 114 115 116 |
# File 'lib/todoist/util/network_helper.rb', line 110 def queue(command, callback = nil) @command_mutex.synchronize do @command_cache.push(command) @temp_id_callback_cache.push(callback) if callback end end |
#sync ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/todoist/util/network_helper.rb', line 118 def sync @command_mutex.synchronize do response = get_sync_response({commands: @command_cache.to_json}) @command_cache.clear # Process callbacks here temp_id_mappings = response["temp_id_mapping"] @temp_id_callback_cache.each do |callback| callback.(temp_id_mappings) end @temp_id_callback_cache.clear end end |
#throttle_request(http, request) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/todoist/util/network_helper.rb', line 91 def throttle_request(http, request) time_since_last_request = Time.now.to_f - @last_request_time if (time_since_last_request < Todoist::Config.delay_between_requests) wait = Todoist::Config.delay_between_requests - time_since_last_request puts("Throttling request by: #{wait}") sleep(wait) end @last_request_time = Time.now.to_f http.request(request) end |