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.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/copyleaks/api.rb', line 32

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)
end

Instance Method Details

#ai_detection_clientObject



469
470
471
# File 'lib/copyleaks/api.rb', line 469

def ai_detection_client
  @ai_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:



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/copyleaks/api.rb', line 275

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.



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/copyleaks/api.rb', line 209

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:



335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/copyleaks/api.rb', line 335

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.



392
393
394
395
396
397
398
399
400
401
# File 'lib/copyleaks/api.rb', line 392

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.



435
436
437
438
439
440
441
442
443
444
# File 'lib/copyleaks/api.rb', line 435

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.



413
414
415
416
417
418
419
420
421
422
423
# File 'lib/copyleaks/api.rb', line 413

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`.



364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/copyleaks/api.rb', line 364

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.



447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
# File 'lib/copyleaks/api.rb', line 447

def handle_response(response, used_by)
  if Utils.is_success_status_code(response.code)
    if response.body.nil? || response.body == ''
      nil
    else
      JSON.parse(response.body)
    end
  elsif Utils.is_under_maintenance_response(response.code)
    raise UnderMaintenanceException.new.reason
  elsif Utils.is_rate_limit_response(response.code)
    raise RateLimitException.new.reason
  else
    _err_message = '---------Copyleaks SDK Error (' + used_by + ')---------' + "\n\n"
    _err_message += 'status code: ' + response.code + "\n\n"

    _err_message += 'response body:' + "\n" + response.body.to_json + "\n\n" unless response.body.nil?

    _err_message += '-------------------------------------'
    raise CommandException.new(_err_message).reason + "\n"
  end
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.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/copyleaks/api.rb', line 55

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



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/copyleaks/api.rb', line 307

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:



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/copyleaks/api.rb', line 242

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:



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/copyleaks/api.rb', line 106

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:



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/copyleaks/api.rb', line 140

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:



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/copyleaks/api.rb', line 174

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

#verify_auth_token(authToken) ⇒ Object

Verify that Copyleaks authentication token is exists and not exipired.

  • Exceptions:

    • AuthExipredException: authentication expired. Need to login again.

Parameters:



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/copyleaks/api.rb', line 80

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 AuthExipredException.new.reason # expired
  end
end

#writing_assistant_clientObject



473
474
475
# File 'lib/copyleaks/api.rb', line 473

def writing_assistant_client
  @writing_assistant_client
end