apptrail-application-events-sdk 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0a56d565f6da67bd2bc4bbfb36c20f599e0ef18bd7ec0f5e969caae6c592409c
4
- data.tar.gz: b3c67d43208b9a8eb134bb529d023c33c24bae80b4485ed41dcc6ca224865131
3
+ metadata.gz: 69363a69d481f39af9b1a6e6be6559e29e5e3468cb866c452bc05d8b3665e783
4
+ data.tar.gz: 7bb490fac18cc57737981319c378bfcbf1520ed3f0867a37991d66fdaf74fbed
5
5
  SHA512:
6
- metadata.gz: 9955cd66982d262361d7cbfdd6672ce4f34ff03e4caf65fc883ddcede9ba0fc2d3386b7fe93e32e2f1cbeed1fc0ee3f1d4450c87131eb7e94491ce6f182ed5ce
7
- data.tar.gz: 76618a0b1bd9f2800a37f167b2781210664da5bb4458a4be29c50d842d14d0e8c2f46bdafd4c3d353e1652625c08bd3062c007233a18b763c5f373a01010a0ce
6
+ metadata.gz: 149fbe6a85a1070cd6e546953241b4871eb8bc76ab41e82c734ad031db291ac273140ec0a2a1a9caed6e4864df787530dbaddbdc74669640f73c1eb6ea8f0b10
7
+ data.tar.gz: 3245bf906b316403f4bc635059c1a4950cf6448e25be554226ea5e8bcb0efc17e34eac2cda2a0d623e61ab379377930b7ff271a15e484b2aaf4473b628eb0069
@@ -1,13 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'logger'
2
4
  require 'stringio'
3
- require "base64"
4
- require "json"
5
+ require 'base64'
6
+ require 'json'
5
7
  require 'securerandom'
6
- require "http"
7
- require "json-schema"
8
- require "retriable"
9
-
10
- #HTTP.default_options = HTTP::Options.new(features: {logging: { logger: Logger.new(STDOUT)}})
8
+ require 'http'
9
+ require 'json-schema'
10
+ require 'retriable'
11
11
 
12
12
  module Apptrail
13
13
  class << self
@@ -15,15 +15,13 @@ module Apptrail
15
15
  # For example, log to STDOUT by setting this to Logger.new(STDOUT).
16
16
  #
17
17
  # @return [Logger]
18
- attr_accessor :logger
18
+ attr_writer :logger
19
19
 
20
20
  def logger
21
21
  @logger ||= Logger.new($stdout).tap do |log|
22
- log.progname = self.name
22
+ log.progname = name
23
23
  end
24
24
  end
25
-
26
-
27
25
  end
28
26
 
29
27
  class ApptrailError < StandardError; end
@@ -34,10 +32,10 @@ module Apptrail
34
32
  tries: 3,
35
33
  base_interval: 0.04,
36
34
  on: Apptrail::ApptrailRetryableError,
37
- on_retry: Proc.new { puts 'Retrying...' },
35
+ on_retry: proc { Apptrail.logger.debug('Retrying upload.') },
38
36
  max_interval: 0.12,
39
37
  max_elapsed_time: 4,
40
- rand_factor: 0.2,
38
+ rand_factor: 0.2
41
39
  }
42
40
  end
43
41
 
@@ -50,8 +48,8 @@ module Apptrail
50
48
  @_upload_url = nil
51
49
 
52
50
  SCHEMA = JSON.parse(File.read(
53
- File.expand_path('../raw-event-schema.json', __FILE__)
54
- )).freeze
51
+ File.expand_path('raw-event-schema.json', __dir__)
52
+ )).freeze
55
53
  private_constant :SCHEMA
56
54
 
57
55
  def inspect
@@ -59,17 +57,17 @@ module Apptrail
59
57
  end
60
58
 
61
59
  def to_s
62
- "ApptrailEventsClient{}"
60
+ 'ApptrailEventsClient{}'
63
61
  end
64
62
 
65
63
  def initialize(region:, api_key:)
66
64
  @_region = region
67
65
  begin
68
66
  parsed_key = Base64.urlsafe_decode64(api_key)
69
- application_id, *_ = parsed_key.split(',', 3)
67
+ application_id, = parsed_key.split(',', 3)
70
68
  @_application_id = application_id
71
- rescue
72
- raise ArgumentError, "Invalid API Key."
69
+ rescue StandardError
70
+ raise ArgumentError, 'Invalid API Key.'
73
71
  end
74
72
 
75
73
  @_base_api_url = "https://events.#{region}.apptrail.com/applications/session"
@@ -83,88 +81,74 @@ module Apptrail
83
81
  put_events([event])
84
82
  end
85
83
 
86
-
87
84
  # Send a list of up to 1000 audit events to log to Apptrail.
88
85
  #
89
86
  # @param events [Array<ApptrailEvent>]] An array of events to log. See [Apptrail Event Format](https://apptrail.com/docs/applications/guide/event-format) for a full description of the event format.
90
87
  def put_events(events)
91
- unless events.is_a?(Array)
92
- raise TypeError, "Must pass in array of events."
93
- end
94
- unless events.length <= 1000
95
- raise ArgumentError, "Can not send more than 1000 events in a single PutEvents call."
96
- end
88
+ raise TypeError, 'Must pass in array of events.' unless events.is_a?(Array)
89
+ raise ArgumentError, 'Can not send more than 1000 events in a single PutEvents call.' unless events.length <= 1000
97
90
 
98
91
  begin
99
- JSON::Validator.validate!(SCHEMA, events, :list => true, :parse_data => false)
92
+ JSON::Validator.validate!(SCHEMA, events, list: true, parse_data: false)
100
93
  rescue JSON::Schema::ValidationError => e
101
- raise Apptrail::ApptrailError.new(e.message)
94
+ raise Apptrail::ApptrailError, e.message
102
95
  end
103
96
 
104
- if @_upload_url.nil? || @_form_data.nil?
105
- _refresh_post_policy()
106
- end
97
+ _refresh_post_policy if @_upload_url.nil? || @_form_data.nil?
107
98
 
108
- content = ""
99
+ content = ''
109
100
  events.each do |evt|
110
101
  content << JSON.generate(evt)
111
102
  content << "\n"
112
103
  end
113
104
 
114
- filename = SecureRandom.uuid + ".jsonl"
105
+ filename = "#{SecureRandom.uuid}.jsonl"
115
106
  s3_key = File.join(@_application_id, filename)
116
107
 
117
- form_file = HTTP::FormData::File.new(StringIO.new(content), :filename => filename)
108
+ form_file = HTTP::FormData::File.new(StringIO.new(content), filename: filename)
118
109
 
119
110
  new_form_opts = {
120
111
  **@_form_data,
121
- :key => s3_key,
122
- :file => form_file
112
+ key: s3_key,
113
+ file: form_file
123
114
  }
124
115
 
125
116
  begin
126
117
  Retriable.with_context(:s3) do
127
- resp = HTTP.post(@_upload_url, :form => new_form_opts)
128
- if !resp.status.success?
129
- if (resp.status.server_error?)
130
- raise ApptrailRetryableError.new("Server Error while putting events.")
131
- elsif (resp.status.client_error?)
132
- if (resp.status.code === 403 && resp.body.to_s.include?("Policy expired"))
133
- _refresh_post_policy()
134
- raise ApptrailRetryableError.new("Session expired.")
118
+ resp = HTTP.post(@_upload_url, form: new_form_opts)
119
+ unless resp.status.success?
120
+ if resp.status.server_error?
121
+ raise ApptrailRetryableError, 'Server Error while putting events.'
122
+ elsif resp.status.client_error?
123
+ if resp.status.code == 403 && resp.body.to_s.include?('Policy expired')
124
+ _refresh_post_policy
125
+ raise ApptrailRetryableError, 'Session expired.'
135
126
  else
136
- puts resp.body.to_s
137
- raise ApptrailError.new("Error while putting events.")
127
+ raise ApptrailError, 'Error while putting events.'
138
128
  end
139
129
  else
140
- raise ApptrailError.new("Error while putting events.")
130
+ raise ApptrailError, 'Error while putting events.'
141
131
  end
142
132
  end
143
133
  end
144
- rescue
145
- raise ApptrailError.new("Failed to put #{events.length} events. Encountered error.")
134
+ rescue StandardError
135
+ raise ApptrailError, "Failed to put #{events.length} events. Encountered error."
146
136
  else
147
- Apptrail::logger.info("Successfully wrote #{events.length} events.")
137
+ Apptrail.logger.info("Successfully wrote #{events.length} events.")
148
138
  end
149
-
150
139
  end
151
140
 
152
141
  private
153
142
 
154
143
  def _refresh_post_policy
155
- puts @_base_api_url
156
144
  resp = HTTP
157
- .auth("Bearer #{@_api_key}")
158
- .get(@_base_api_url)
159
- if !resp.status.success?
160
- raise ApptrailError.new("Error refreshing credentials, status code: #{resp.code}")
161
- end
145
+ .auth("Bearer #{@_api_key}")
146
+ .get(@_base_api_url)
147
+ raise ApptrailError, "Error refreshing credentials, status code: #{resp.code}" unless resp.status.success?
148
+
162
149
  result = JSON.parse(resp.body.to_s)
163
- @_upload_url = result["uploadUrl"]
164
- @_form_data = result["form"]
150
+ @_upload_url = result['uploadUrl']
151
+ @_form_data = result['form']
165
152
  end
166
-
167
-
168
153
  end
169
-
170
154
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: apptrail-application-events-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Apptrail Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-01-27 00:00:00.000000000 Z
11
+ date: 2022-01-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: http