Class: ATProto::Session

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Includes:
RequestUtils
Defined in:
lib/at_protocol/session.rb

Direct Known Subclasses

TokenSession

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RequestUtils

at_uri, #create_session_uri, #default_authenticated_headers, #default_headers, #delete_record_uri, #delete_session_uri, #get_paginated_data, #get_post_thread_uri, #get_session_uri, #mute_actor_uri, #query_obj_to_query_params, #refresh_session_uri, #refresh_token_headers, #resolve_handle, #upload_blob_uri

Constructor Details

#initialize(credentials, should_open = true) ⇒ Session

Returns a new instance of Session.



23
24
25
26
27
# File 'lib/at_protocol/session.rb', line 23

def initialize(credentials, should_open = true)
  @credentials = credentials
  @pds = credentials.pds
  open! if should_open
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



19
20
21
# File 'lib/at_protocol/session.rb', line 19

def access_token
  @access_token
end

#didObject (readonly)

Returns the value of attribute did.



19
20
21
# File 'lib/at_protocol/session.rb', line 19

def did
  @did
end

#pdsObject (readonly)

Returns the value of attribute pds.



19
20
21
# File 'lib/at_protocol/session.rb', line 19

def pds
  @pds
end

#refresh_tokenObject (readonly)

Returns the value of attribute refresh_token.



19
20
21
# File 'lib/at_protocol/session.rb', line 19

def refresh_token
  @refresh_token
end

#xrpcObject (readonly)

Returns the value of attribute xrpc.



19
20
21
# File 'lib/at_protocol/session.rb', line 19

def xrpc
  @xrpc
end

Instance Method Details

#delete!Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/at_protocol/session.rb', line 58

def delete!
  response = HTTParty.post(
    URI(delete_session_uri(pds)),
    headers: refresh_token_headers(self),
  )
  if response.code == 200
    { success: true }
  else
    raise UnauthorizedError
  end
end

#get_sessionObject



54
55
56
# File 'lib/at_protocol/session.rb', line 54

def get_session
  @xrpc.get.com_atproto_server_getSession
end

#inspectObject



70
71
72
# File 'lib/at_protocol/session.rb', line 70

def inspect
  "#<ATProto::Session(did: #{did}, access_token: #{access_token})>"
end

#open!Object

Raises:



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/at_protocol/session.rb', line 29

def open!
  @xrpc = XRPC::Client.new(@pds)
  response = @xrpc.post.com_atproto_server_createSession(identifier: @credentials.username, password: @credentials.pw)

  raise UnauthorizedError if response["accessJwt"].nil?

  @access_token = response["accessJwt"]
  @refresh_token = response["refreshJwt"]
  @did = response["did"]

  @xrpc = XRPC::Client.new(@pds, @access_token)
  @refresher = XRPC::Client.new(@pds, @refresh_token)
end

#refresh!Object

Raises:



43
44
45
46
47
48
49
50
# File 'lib/at_protocol/session.rb', line 43

def refresh!
  response = @refresher.post.com_atproto_server_refreshSession
  raise UnauthorizedError if response["accessJwt"].nil?
  @access_token = response["accessJwt"]
  @refresh_token = response["refreshJwt"]
  @xrpc = XRPC::Client.new(@pds, @access_token)
  @refresher = XRPC::Client.new(@pds, @refresh_token)
end