Module: ATProto::RequestUtils

Extended by:
T::Sig
Includes:
Kernel
Included in:
AtUriParser, Record, ATProto::Record::StrongRef, Repo, ATProto::Repo::Collection, Session, Writes::Collector
Defined in:
lib/at_protocol/requests.rb,
lib/at_protocol/at_uri.rb,
lib/at_protocol/requests.rb

Overview

Goal is to replace with pure XRPC eventually

Defined Under Namespace

Classes: XRPCResponseCode

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.at_uri(url, atp_host = "https://bsky.social") ⇒ Object



10
11
12
# File 'lib/at_protocol/at_uri.rb', line 10

def at_uri(url, atp_host = "https://bsky.social")
  AtUriParser.parse(url, AtUriParser::RuleSets, pds: atp_host)
end

Instance Method Details

#create_session_uri(pds) ⇒ Object



28
29
30
# File 'lib/at_protocol/requests.rb', line 28

def create_session_uri(pds)
  "#{pds}/xrpc/com.atproto.server.createSession"
end

#default_authenticated_headers(session) ⇒ Object



60
61
62
63
64
# File 'lib/at_protocol/requests.rb', line 60

def default_authenticated_headers(session)
  default_headers.merge({
    Authorization: "Bearer #{session.access_token}",
  })
end

#default_headersObject



24
25
26
# File 'lib/at_protocol/requests.rb', line 24

def default_headers
  { "Content-Type" => "application/json" }
end

#delete_record_uri(pds) ⇒ Object



44
45
46
# File 'lib/at_protocol/requests.rb', line 44

def delete_record_uri(pds)
  "#{pds}/xrpc/com.atproto.repo.deleteRecord"
end

#delete_session_uri(pds) ⇒ Object



32
33
34
# File 'lib/at_protocol/requests.rb', line 32

def delete_session_uri(pds)
  "#{pds}/xrpc/com.atproto.server.deleteSession"
end

#get_paginated_data(session, method, key:, params:, cursor: nil, count: nil, &map_block) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/at_protocol/requests.rb', line 92

def get_paginated_data(session, method, key:, params:, cursor: nil, count: nil, &map_block)
  params.merge({ limit: 100, cursor: cursor }).then do |send_data|
    session.xrpc.get.public_send(method, **send_data).then do |response|
      response.dig(key).then do |data|
        if data.nil? || data.empty?
          return []
        end

        results = if block_given?
            data.map(&map_block)
          else
            data
          end

        response.dig("cursor").then do |next_cursor|
          if next_cursor.nil? || (count && results.length >= count)
            return results[0..count]
          else
            remaining_count = count ? count - results.length : nil
            get_paginated_data(session, method, key: key, params: params, cursor: next_cursor, count: remaining_count, &map_block).then do |next_results|
              return (results + next_results)
            end
          end
        end
      end
    end
  end
end

#get_post_thread_uri(pds, query) ⇒ Object



56
57
58
# File 'lib/at_protocol/requests.rb', line 56

def get_post_thread_uri(pds, query)
  "#{pds}/xrpc/app.bsky.feed.getPostThread#{query_obj_to_query_params(query)}"
end

#get_session_uri(pds) ⇒ Object



40
41
42
# File 'lib/at_protocol/requests.rb', line 40

def get_session_uri(pds)
  "#{pds}/xrpc/com.atproto.server.getSession"
end

#mute_actor_uri(pds) ⇒ Object



48
49
50
# File 'lib/at_protocol/requests.rb', line 48

def mute_actor_uri(pds)
  "#{pds}/xrpc/app.bsky.graph.muteActor"
end

#query_obj_to_query_params(q) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/at_protocol/requests.rb', line 16

def query_obj_to_query_params(q)
  out = "?"
  q.to_h.each do |key, value|
    out += "#{key}=#{value}&" unless value.nil? || (value.class.method_defined?(:empty?) && value.empty?)
  end
  out.slice(0...-1)
end

#refresh_session_uri(pds) ⇒ Object



36
37
38
# File 'lib/at_protocol/requests.rb', line 36

def refresh_session_uri(pds)
  "#{pds}/xrpc/com.atproto.server.refreshSession"
end

#refresh_token_headers(session) ⇒ Object



66
67
68
69
70
# File 'lib/at_protocol/requests.rb', line 66

def refresh_token_headers(session)
  default_headers.merge({
    Authorization: "Bearer #{session.refresh_token}",
  })
end

#resolve_handle(username, pds = "https://bsky.social") ⇒ Object



12
13
14
# File 'lib/at_protocol/requests.rb', line 12

def resolve_handle(username, pds = "https://bsky.social")
  (XRPC::Client.new(pds).get.com_atproto_identity_resolveHandle(handle: username))["did"]
end

#upload_blob_uri(pds) ⇒ Object



52
53
54
# File 'lib/at_protocol/requests.rb', line 52

def upload_blob_uri(pds)
  "#{pds}/xrpc/com.atproto.repo.uploadBlob"
end