Class: Copyleaks::API

Inherits:
Object
  • Object
show all
Defined in:
lib/copyleaks/api.rb

Instance Method Summary collapse

Constructor Details

#initializeAPI

Returns a new instance of API.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/copyleaks/api.rb', line 35

def initialize
  # copyleaks identity http client
  _identity_server_uri = URI.parse(Config.identity_server_uri)
  @id_client = Net::HTTP.new(_identity_server_uri.host, _identity_server_uri.port)
  @id_client.use_ssl = true
  # copyleaks api http client
  _api_server_uri = URI.parse(Config.api_server_uri)
  @api_client = Net::HTTP.new(_api_server_uri.host, _api_server_uri.port)
  @api_client.use_ssl = true

  # Initialize clients
  @ai_detection_client = AIDetectionClient.new(@api_client)
  @writing_assistant_client = WritingAssistantClient.new(@api_client)
  @text_moderation_client = TextModerationClient.new(@api_client)
  @ai_image_detection_client = AIImageDetectionClient.new(@api_client)
end

Instance Method Details

#ai_detection_clientObject



456
457
458
# File 'lib/copyleaks/api.rb', line 456

def ai_detection_client
  @ai_detection_client
end

#ai_image_detection_clientObject



467
468
469
# File 'lib/copyleaks/api.rb', line 467

def ai_image_detection_client
  @ai_image_detection_client
end

#delete(authToken, data) ⇒ Object

Delete the specific process from the server. For more info: api.copyleaks.com/documentation/v3/scans/delete

  • Exceptions:

    • CommandExceptions: Server reject the request. See response status code, headers and content for more info.

Parameters:



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/copyleaks/api.rb', line 280

def delete(authToken, data)
  if data.nil? || !data.instance_of?(CopyleaksDeleteRequestModel)
    raise 'data is Invalid, must be instance of CopyleaksDeleteRequestModel'
  end

  verify_auth_token(authToken)

  path = "/v3.1/scans/delete"

  headers = {
    'Content-Type' => 'application/json',
    'User-Agent' => Config.user_agent,
    'Authorization' => "Bearer #{authToken.accessToken}"
  }

  request = Net::HTTP::Patch.new(path, headers)
  request.body = data.to_json

  handle_response(@api_client.request(request), 'delete')
end

#export(authToken, scanId, exportId, model) ⇒ Object

Exporting scans artifact into your server. For more info: api.copyleaks.com/documentation/v3/downloads/export

  • Exceptions:

    • CommandExceptions: Server reject the request. See response status code, headers and content for more info.

Parameters:

  • authToken (CopyleaksAuthToken)

    Your login token to Copyleaks server

  • scanId (String)

    The scan ID of the specific scan to export.

  • exportId (String)

    A new Id for the export process.

  • model (CopyleaksExportModel)

    Request of which artifact should be exported.



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/copyleaks/api.rb', line 214

def export(authToken, scanId, exportId, model)
  raise 'scanId is Invalid, must be instance of String' if scanId.nil? || !scanId.instance_of?(String)
  raise 'exportId is Invalid, must be instance of String' if exportId.nil? || !exportId.instance_of?(String)
  if model.nil? || !model.instance_of?(CopyleaksExportModel)
    raise 'model is Invalid, must be instance of type CopyleaksExportModel'
  end

  verify_auth_token(authToken)
  path = "/v3/downloads/#{scanId}/export/#{exportId}"

  headers = {
    'Content-Type' => 'application/json',
    'User-Agent' => Config.user_agent,
    'Authorization' => "Bearer #{authToken.accessToken}"
  }

  request = Net::HTTP::Post.new(path, headers)
  request.body = model.to_json

  handle_response(@api_client.request(request), 'export')
end

#get_credits_balance(authToken) ⇒ Object

Get current credits balance for the Copyleaks account. For more info: api.copyleaks.com/documentation/v3/scans/credits

  • Exceptions:

    • CommandExceptions: Server reject the request. See response status code, headers and content for more info.

    • RateLimitException: Too many requests. Please wait before calling again.

Parameters:



340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/copyleaks/api.rb', line 340

def get_credits_balance(authToken)
  verify_auth_token(authToken)

  path = "/v3/scans/credits"

  headers = {
    'Content-Type' => 'application/json',
    'User-Agent' => Config.user_agent,
    'Authorization' => "Bearer #{authToken.accessToken}"
  }

  request = Net::HTTP::Get.new(path, headers)
  handle_response(@api_client.request(request), 'get_credits_balance')
end

#get_ocr_supported_languagesObject

Get a list of the supported languages for OCR (this is not a list of supported languages for the api, but only for the OCR files scan). For more info: api.copyleaks.com/documentation/v3/specifications/ocr-languages/list

  • Exceptions:

    • CommandExceptions: Server reject the request. See response status code, headers and content for more info.

    • RateLimitException: Too many requests. Please wait before calling again.

Returns:

  • array List of supported OCR languages.



397
398
399
400
401
402
403
404
405
406
# File 'lib/copyleaks/api.rb', line 397

def get_ocr_supported_languages
  path = '/v3/miscellaneous/ocr-languages-list'

  headers = {
    'Content-Type' => 'application/json',
    'User-Agent' => Config.user_agent
  }
  request = Net::HTTP::Get.new(path, headers)
  handle_response(@api_client.request(request), 'get_ocr_supported_languages')
end

#get_release_notesObject

Get updates about copyleaks api release notes. For more info: api.copyleaks.com/documentation/v3/release-notes

  • Exceptions:

    • CommandExceptions: Server reject the request. See response status code, headers and content for more info.

    • RateLimitException: Too many requests. Please wait before calling again.

Returns:

  • mixed List of release notes.



440
441
442
443
444
445
446
447
448
449
# File 'lib/copyleaks/api.rb', line 440

def get_release_notes
  path = '/v3/release-logs.json'

  header = {
    'Content-Type' => 'application/json',
    'User-Agent' => Config.user_agent
  }
  request = Net::HTTP::Get.new(path, header)
  handle_response(@api_client.request(request), 'get_release_notes')
end

#get_supported_file_typesObject

Get a list of the supported file types. For more info: api.copyleaks.com/documentation/v3/specifications/supported-file-types

  • Exceptions:

    • CommandExceptions: Server reject the request. See response status code, headers and content for more info.

    • RateLimitException: Too many requests. Please wait before calling again.

Returns:

  • mixed List of supported file types.



418
419
420
421
422
423
424
425
426
427
428
# File 'lib/copyleaks/api.rb', line 418

def get_supported_file_types
  path =  '/v3/miscellaneous/supported-file-types'

  headers = {
    'Content-Type' => 'application/json',
    'User-Agent' => Config.user_agent
  }

  request = Net::HTTP::Get.new(path, headers)
  handle_response(@api_client.request(request), 'get_supported_file_types')
end

#get_usages_history_csv(authToken, startDate, endDate) ⇒ Object

This endpoint allows you to export your usage history between two dates. The output results will be exported to a csv file and it will be attached to the response. For more info: api.copyleaks.com/documentation/v3/scans/usages/history

  • Exceptions:

    • CommandExceptions: Server reject the request. See response status code, headers and content for more info.

    • RateLimitException: Too many requests. Please wait before calling again.

Parameters:

  • authToken (CopyleaksAuthToken)

    Copyleaks authentication token.

  • startDate (String)

    The start date to collect usage history from. Date Format: ‘dd-MM-yyyy`.

  • endDate (String)

    The end date to collect usage history from. Date Format: ‘dd-MM-yyyy`.



369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/copyleaks/api.rb', line 369

def get_usages_history_csv(authToken, startDate, endDate)
  raise 'startDate is Invalid, must be instance of String' if startDate.nil? || !startDate.instance_of?(String)
  raise 'endDate is Invalid, must be instance of String' if endDate.nil? || !endDate.instance_of?(String)

  verify_auth_token(authToken)

  path =  "/v3/scans/usages/history?start=#{startDate}&end=#{endDate}"

  headers = {
    'Content-Type' => 'application/json',
    'User-Agent' => Config.user_agent,
    'Authorization' => "Bearer #{authToken.accessToken}"
  }

  request = Net::HTTP::Get.new(path, headers)
  handle_response(@api_client.request(request), 'get_usages_history_csv')
end

#handle_response(response, used_by) ⇒ Object

this methods is a helper for hanlding reponse data and exceptions.



452
453
454
# File 'lib/copyleaks/api.rb', line 452

def handle_response(response, used_by)
  Copyleaks::ClientUtils.handle_response(response, used_by)
end

#login(email, key) ⇒ Object

Login to Copyleaks authentication server. For more info: api.copyleaks.com/documentation/v3/account/login.

  • Exceptions:

    • CommandExceptions: Server reject the request. See response status code, headers and content for more info.

Parameters:

  • email (String)

    Copyleaks account email address.

  • key (String)

    Copyleaks account secret key.

Returns:

  • A authentication token that being expired after certain amount of time.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/copyleaks/api.rb', line 60

def (email, key)
  raise 'email is Invalid, must be instance of String' if email.nil? || !email.instance_of?(String)
  raise 'key is Invalid, must be instance of String' if key.nil? || !email.instance_of?(String)

  path = '/v3/account/login/api'

  headers = {
    'Content-Type' => 'application/json',
    'User-Agent' => Config.user_agent
  }

  payload = { email: email, key: key }

  request = Net::HTTP::Post.new(path, headers)
  request.body = payload.to_json

  res_data = handle_response(@id_client.request(request), 'login')

  CopyleaksAuthToken.new(res_data['.expires'], res_data['access_token'], res_data['.issued'])
end

#resend_webhook(authToken, scanId) ⇒ Object

Resend status webhooks for existing scans. For more info: api.copyleaks.com/documentation/v3/scans/webhook-resend

  • Exceptions:

    • CommandExceptions: Server reject the request. See response status code, headers and content for more info.

Parameters:

  • authToken (CopyleaksAuthToken)

    Copyleaks authentication token

  • scanId (String)

    Copyleaks scan Id



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/copyleaks/api.rb', line 312

def resend_webhook(authToken, scanId)
  raise 'scanId is Invalid, must be instance of String' if scanId.nil? || !scanId.instance_of?(String)

  verify_auth_token(authToken)

  path =  "/v3/scans/#{scanId}/webhooks/resend"

  headers = {
    'Content-Type' => 'application/json',
    'User-Agent' => Config.user_agent,
    'Authorization' => "Bearer #{authToken.accessToken}"
  }

  request = Net::HTTP::Post.new(path, headers)
  handle_response(@api_client.request(request), 'resend_webhook')
end

#start(authToken, data) ⇒ Object

Start scanning all the files you submitted for a price-check. For more info: api.copyleaks.com/documentation/v3/scans/start

  • Exceptions:

    • CommandExceptions: Server reject the request. See response status code, headers and content for more info.

Parameters:



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/copyleaks/api.rb', line 247

def start(authToken, data)
  if data.nil? || !data.instance_of?(CopyleaksStartRequestModel)
    raise 'data is Invalid, must be instance of type CopyleaksStartRequestModel'
  end

  verify_auth_token(authToken)

  path =  "/v3/scans/start"

  headers = {
    'Content-Type' => 'application/json',
    'User-Agent' => Config.user_agent,
    'Authorization' => "Bearer #{authToken.accessToken}"
  }

  request = Net::HTTP::Patch.new(path, headers)
  request.body = data.to_json

  handle_response(@api_client.request(request), 'start')
end

#submit_file(authToken, scanId, submission) ⇒ Object

Starting a new process by providing a file to scan. For more info: api.copyleaks.com/documentation/v3/scans/submit/file

  • Exceptions:

    • CommandExceptions: Server reject the request. See response status code, headers and content for more info.

Parameters:



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/copyleaks/api.rb', line 111

def submit_file(authToken, scanId, submission)
  raise 'scanId is Invalid, must be instance of String' if scanId.nil? || !scanId.instance_of?(String)
  if submission.nil? || !submission.instance_of?(CopyleaksFileSubmissionModel)
    raise 'submission is Invalid, must be instance of type CopyleaksFileSubmissionModel'
  end

  verify_auth_token(authToken)

  path = "/v3/scans/submit/file/#{scanId}"

  headers = {
    'Content-Type' => 'application/json',
    'User-Agent' => Config.user_agent,
    'Authorization' => "Bearer #{authToken.accessToken}"
  }

  request = Net::HTTP::Put.new(path, headers)
  request.body = submission.to_json

  handle_response(@api_client.request(request), 'submit_file')
end

#submit_file_ocr(authToken, scanId, submission) ⇒ Object

Starting a new process by providing a OCR image file to scan. For more info: api.copyleaks.com/documentation/v3/scans/submit/ocr

  • Exceptions:

    • CommandExceptions: Server reject the request. See response status code, headers and content for more info.

Parameters:



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/copyleaks/api.rb', line 145

def submit_file_ocr(authToken, scanId, submission)
  raise 'scanId is Invalid, must be instance of String' if scanId.nil? || !scanId.instance_of?(String)
  if submission.nil? || !submission.instance_of?(CopyleaksFileOcrSubmissionModel)
    raise 'submission is Invalid, must be instance of type CopyleaksFileOcrSubmissionModel'
  end

  verify_auth_token(authToken)

  path = "/v3/scans/submit/ocr/#{scanId}"

  headers = {
    'Content-Type' => 'application/json',
    'User-Agent' => Config.user_agent,
    'Authorization' => "Bearer #{authToken.accessToken}"
  }

  request = Net::HTTP::Put.new(path, headers)
  request.body = submission.to_json

  handle_response(@api_client.request(request), 'submit_file_ocr')
end

#submit_url(authToken, scanId, submission) ⇒ Object

Starting a new process by providing a URL to scan. For more info: api.copyleaks.com/documentation/v3/scans/submit/url

  • Exceptions:

    • CommandExceptions: Server reject the request. See response status code, headers and content for more info.

Parameters:



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/copyleaks/api.rb', line 179

def submit_url(authToken, scanId, submission)
  raise 'scanId is Invalid, must be instance of String' if scanId.nil? || !scanId.instance_of?(String)
  if submission.nil? || !submission.instance_of?(CopyleaksURLSubmissionModel)
    raise 'submission is Invalid, must be instance of CopyleaksURLSubmissionModel'
  end

  verify_auth_token(authToken)

  path = "/v3/scans/submit/url/#{scanId}"

  headers = {
    'Content-Type' => 'application/json',
    'User-Agent' => Config.user_agent,
    'Authorization' => "Bearer #{authToken.accessToken}"
  }

  request = Net::HTTP::Put.new(path, headers)
  request.body = submission.to_json

  handle_response(@api_client.request(request), 'submit_url')
end

#text_moderation_clientObject



463
464
465
# File 'lib/copyleaks/api.rb', line 463

def text_moderation_client
  @text_moderation_client
end

#verify_auth_token(authToken) ⇒ Object

Verify that Copyleaks authentication token is exists and not exipired.

  • Exceptions:

    • AuthExpiredException: authentication expired. Need to login again.

Parameters:



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/copyleaks/api.rb', line 85

def verify_auth_token(authToken)
  if authToken.nil? || !authToken.instance_of?(CopyleaksAuthToken)
    raise 'authToken is Invalid, must be instance of CopyleaksAuthToken'
  end

  _time = DateTime.now
  _expiresTime = DateTime.parse(authToken.expires)

  if _expiresTime <= _time
    raise AuthExpiredException # expired
  end
end

#writing_assistant_clientObject



460
461
462
# File 'lib/copyleaks/api.rb', line 460

def writing_assistant_client
  @writing_assistant_client
end