Class: Konfidant::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/konfidant/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, base_url: nil, http_timeout: DEFAULT_TIMEOUT) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
# File 'lib/konfidant/client.rb', line 10

def initialize(api_key:, base_url: nil, http_timeout: DEFAULT_TIMEOUT)
  raise ArgumentError, 'api_key is required' if api_key.nil? || api_key.empty?

  @api_key      = api_key
  @base_url     = (base_url || DEFAULT_BASE_URL).sub(%r{/+\z}, '')
  @http_timeout = http_timeout
end

Instance Method Details

#get_file_status(file_key) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/konfidant/client.rb', line 43

def get_file_status(file_key)
  encoded = encode_path_segment(file_key)
  body = request(:get, "/api/v1/files/#{encoded}/status")
  FileStatusResponse.new(
    status:        body['status'],
    message:       body['message'],
    file_id:       body['file_id'],
    file_name:     body['file_name'],
    share_url:     body['share_url'],
    expires_at:    body['expires_at'],
    verified_burn: body['verified_burn']
  )
end

#list_shares(type: nil, status: nil, limit: nil, offset: nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/konfidant/client.rb', line 57

def list_shares(type: nil, status: nil, limit: nil, offset: nil)
  params = {}
  params[:type]   = type   if type
  params[:status] = status if status
  params[:limit]  = limit  if limit
  params[:offset] = offset if offset

  path = '/api/v1/shares'
  path += "?#{URI.encode_www_form(params)}" unless params.empty?

  body = request(:get, path)
  ListSharesResponse.new(
    shares:     body['shares'].map { |s| parse_share(s) },
    pagination: parse_pagination(body['pagination'])
  )
end

#share_and_upload_file(io:, size:, filename:, content_type:, ttl_hours:, poll_interval: 2, timeout: 60) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/konfidant/client.rb', line 92

def share_and_upload_file(io:, size:, filename:, content_type:, ttl_hours:,
                          poll_interval: 2, timeout: 60)
  presigned = share_file(filename: filename, file_size: size, ttl_hours: ttl_hours)
  upload_file(io: io, size: size, content_type: content_type, presigned: presigned)

  deadline = Time.now + timeout
  while Time.now < deadline
    status = get_file_status(presigned.file_key)
    if status.status == 'complete'
      return ShareResult.new(
        share_url:    status.share_url,
        file_id:      status.file_id,
        expires_at:   status.expires_at,
        verified_burn: status.verified_burn
      )
    end
    sleep(poll_interval)
  end

  raise "konfidant: encryption timed out after #{timeout}s"
end

#share_file(filename:, file_size:, ttl_hours:) ⇒ Object



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

def share_file(filename:, file_size:, ttl_hours:)
  body = request(:post, '/api/v1/files', { filename: filename, file_size: file_size, ttl_hours: ttl_hours })
  h = body['metadata_headers']
  ShareFileResponse.new(
    upload_url:       body['upload_url'],
    file_key:         body['file_key'],
    metadata_headers: FileMetadataHeaders.new(
      user_id:         h['x-amz-meta-user-id'],
      ttl_hours:       h['x-amz-meta-ttl-hours'],
      organization_id: h['x-amz-meta-organization-id']
    ),
    poll_url: body['poll_url']
  )
end

#share_text(text:, ttl_hours:) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/konfidant/client.rb', line 18

def share_text(text:, ttl_hours:)
  body = request(:post, '/api/v1/texts', { text: text, ttl_hours: ttl_hours })
  ShareTextResponse.new(
    text_id:      body['text_id'],
    share_url:    body['share_url'],
    expires_at:   body['expires_at'],
    verified_burn: body['verified_burn']
  )
end

#upload_file(io:, size:, content_type:, presigned:) ⇒ Object

Raises:



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/konfidant/client.rb', line 74

def upload_file(io:, size:, content_type:, presigned:)
  uri  = URI.parse(presigned.upload_url)
  http = build_http(uri)

  req = Net::HTTP::Put.new(uri.request_uri)
  req['Content-Type']               = content_type
  req['Content-Length']             = size.to_s
  req['x-amz-meta-organization-id'] = presigned..organization_id
  req['x-amz-meta-ttl-hours']       = presigned..ttl_hours
  req['x-amz-meta-user-id']         = presigned..user_id
  req.body_stream = io

  resp = http.request(req)
  return if resp.code.to_i.between?(200, 299)

  raise ApiError.new("file upload failed: HTTP #{resp.code}", resp.code.to_i, resp.body)
end