Module: Minisky::Requests
- Included in:
- Minisky
- Defined in:
- lib/minisky/requests.rb
Instance Attribute Summary collapse
- #auto_manage_tokens ⇒ Object
-
#default_progress ⇒ Object
(also: #progress)
Returns the value of attribute default_progress.
- #send_auth_headers ⇒ Object
Instance Method Summary collapse
- #base_url ⇒ Object
- #check_access ⇒ Object
- #fetch_all(method, params = nil, field:, auth: default_auth_mode, break_when: nil, max_pages: nil, headers: nil, progress: @default_progress) ⇒ Object
- #get_request(method, params = nil, auth: default_auth_mode, headers: nil, **kwargs) ⇒ Object
- #log_in ⇒ Object
- #perform_token_refresh ⇒ Object
- #post_request(method, params = nil, auth: default_auth_mode, headers: nil, **kwargs) ⇒ Object
- #reset_tokens ⇒ Object
- #user ⇒ Object
Instance Attribute Details
#auto_manage_tokens ⇒ Object
38 39 40 |
# File 'lib/minisky/requests.rb', line 38 def auto_manage_tokens instance_variable_defined?('@auto_manage_tokens') ? @auto_manage_tokens : true end |
#default_progress ⇒ Object Also known as: progress
Returns the value of attribute default_progress.
30 31 32 |
# File 'lib/minisky/requests.rb', line 30 def default_progress @default_progress end |
#send_auth_headers ⇒ Object
34 35 36 |
# File 'lib/minisky/requests.rb', line 34 def send_auth_headers instance_variable_defined?('@send_auth_headers') ? @send_auth_headers : true end |
Instance Method Details
#base_url ⇒ Object
45 46 47 |
# File 'lib/minisky/requests.rb', line 45 def base_url @base_url ||= "https://#{host}/xrpc" end |
#check_access ⇒ Object
110 111 112 113 114 115 116 |
# File 'lib/minisky/requests.rb', line 110 def check_access if !user.logged_in? log_in elsif token_expiration_date(user.access_token) < Time.now + 60 perform_token_refresh end end |
#fetch_all(method, params = nil, field:, auth: default_auth_mode, break_when: nil, max_pages: nil, headers: nil, progress: @default_progress) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/minisky/requests.rb', line 85 def fetch_all(method, params = nil, field:, auth: default_auth_mode, break_when: nil, max_pages: nil, headers: nil, progress: @default_progress) data = [] params = {} if params.nil? pages = 0 loop do print(progress) if progress response = get_request(method, params, auth: auth, headers: headers) records = response[field] cursor = response['cursor'] data.concat(records) params[:cursor] = cursor pages += 1 break if !cursor || records.empty? || pages == max_pages break if break_when && records.any? { |x| break_when.call(x) } end data.delete_if { |x| break_when.call(x) } if break_when data end |
#get_request(method, params = nil, auth: default_auth_mode, headers: nil, **kwargs) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/minisky/requests.rb', line 53 def get_request(method, params = nil, auth: default_auth_mode, headers: nil) check_access if auto_manage_tokens && auth == true headers = authentication_header(auth).merge(headers || {}) url = URI("#{base_url}/#{method}") if params && !params.empty? url.query = URI.encode_www_form(params) end request = Net::HTTP::Get.new(url, headers) response = make_request(request) handle_response(response) end |
#log_in ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/minisky/requests.rb', line 118 def log_in if user.id.nil? || user.pass.nil? raise AuthError, "To log in, please provide a user id and password" end data = { identifier: user.id, password: user.pass } json = post_request('com.atproto.server.createSession', data, auth: false) user.did = json['did'] user.access_token = json['accessJwt'] user.refresh_token = json['refreshJwt'] save_config json end |
#perform_token_refresh ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/minisky/requests.rb', line 138 def perform_token_refresh if user.refresh_token.nil? raise AuthError, "Can't refresh access token - refresh token is missing" end json = post_request('com.atproto.server.refreshSession', auth: user.refresh_token) user.access_token = json['accessJwt'] user.refresh_token = json['refreshJwt'] save_config json end |
#post_request(method, params = nil, auth: default_auth_mode, headers: nil, **kwargs) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/minisky/requests.rb', line 69 def post_request(method, params = nil, auth: default_auth_mode, headers: nil) check_access if auto_manage_tokens && auth == true headers = authentication_header(auth).merge(headers || {}) headers["Content-Type"] = "application/json" unless headers.keys.any? { |k| k.to_s.downcase == 'content-type' } body = if params.is_a?(String) || params.nil? params.to_s else params.to_json end response = Net::HTTP.post(URI("#{base_url}/#{method}"), body, headers) handle_response(response) end |
#reset_tokens ⇒ Object
152 153 154 155 156 157 |
# File 'lib/minisky/requests.rb', line 152 def reset_tokens user.access_token = nil user.refresh_token = nil save_config nil end |