Class: CardDB::Resources::Imports

Inherits:
Base
  • Object
show all
Defined in:
lib/carddb/resources/imports.rb

Overview

Publisher import job resource for single-dataset and game-level imports.

Constant Summary collapse

EMPTY_CONNECTION =
{
  'totalCount' => 0,
  'pageInfo' => {
    'hasNextPage' => false,
    'hasPreviousPage' => false,
    'startCursor' => nil,
    'endCursor' => nil
  },
  'edges' => []
}.freeze

Instance Attribute Summary

Attributes inherited from Base

#client, #config, #connection

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from CardDB::Resources::Base

Instance Method Details

#cancel_game_job(id:) ⇒ Object

Cancel a pending or processing advanced game-level import job.



175
176
177
178
179
180
# File 'lib/carddb/resources/imports.rb', line 175

def cancel_game_job(id:)
  config.require_secret_credential!('imports.cancel_game_job')

  data = connection.execute(QueryBuilder.game_import_job_cancel, { id: id })
  GameImportJob.new(data['gameImportJobCancel'], client: client)
end

#cancel_job(id:) ⇒ Object

Cancel a pending or processing single-dataset import job.



86
87
88
89
90
91
# File 'lib/carddb/resources/imports.rb', line 86

def cancel_job(id:)
  config.require_secret_credential!('imports.cancel_job')

  data = connection.execute(QueryBuilder.import_job_cancel, { id: id })
  ImportJob.new(data['importJobCancel'], client: client)
end

#get_game_job(id, cache: nil) ⇒ Object

Fetch one advanced game-level import job.



148
149
150
151
152
153
154
# File 'lib/carddb/resources/imports.rb', line 148

def get_game_job(id, cache: nil)
  key = cache_key('imports', 'get_game_job', id: id)
  with_cache(key, resource: :imports, cache: cache) do
    data = connection.execute(QueryBuilder.game_import_job, { id: id })
    data['gameImportJob'] ? GameImportJob.new(data['gameImportJob'], client: client) : nil
  end
end

#get_job(id, cache: nil) ⇒ Object

Fetch one single-dataset import job.



40
41
42
43
44
45
46
# File 'lib/carddb/resources/imports.rb', line 40

def get_job(id, cache: nil)
  key = cache_key('imports', 'get_job', id: id)
  with_cache(key, resource: :imports, cache: cache) do
    data = connection.execute(QueryBuilder.import_job, { id: id })
    data['importJob'] ? ImportJob.new(data['importJob'], client: client) : nil
  end
end

#list_game_jobs(game_id:, status: nil, first: nil, after: nil, cache: nil) ⇒ Object

List advanced game-level import jobs.



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/carddb/resources/imports.rb', line 157

def list_game_jobs(game_id:, status: nil, first: nil, after: nil, cache: nil)
  query = QueryBuilder.game_import_jobs(game_id: game_id, status: status, first: first, after: after)
  variables = build_variables(gameId: game_id, status: status, first: first, after: after)
  key = cache_key('imports', 'list_game_jobs', **variables)

  data = with_cache(key, resource: :imports, cache: cache) { connection.execute(query, variables) }

  Collection.new(
    data['gameImportJobs'],
    item_class: GameImportJob,
    next_page_loader: lambda { |cursor|
      list_game_jobs(game_id: game_id, status: status, first: first, after: cursor, cache: cache)
    },
    client: client
  )
end

#list_job_logs(import_job_id:, level: nil, dataset_key: nil, first: nil, after: nil, cache: nil) ⇒ Object

List structured logs for one single-dataset import job.



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
120
121
122
123
# File 'lib/carddb/resources/imports.rb', line 94

def list_job_logs(import_job_id:, level: nil, dataset_key: nil, first: nil, after: nil, cache: nil)
  query = QueryBuilder.import_job_logs(level: level, dataset_key: dataset_key, first: first, after: after)
  variables = build_variables(
    importJobId: import_job_id,
    level: level,
    datasetKey: dataset_key,
    first: first,
    after: after
  )
  key = cache_key('imports', 'list_job_logs', **variables)

  data = with_cache(key, resource: :imports, cache: cache) { connection.execute(query, variables) }
  logs = data.dig('importJob', 'logs') || EMPTY_CONNECTION

  Collection.new(
    logs,
    item_class: ImportJobLog,
    next_page_loader: lambda { |cursor|
      list_job_logs(
        import_job_id: import_job_id,
        level: level,
        dataset_key: dataset_key,
        first: first,
        after: cursor,
        cache: cache
      )
    },
    client: client
  )
end

#list_jobs(publisher_id:, dataset_id: nil, status: nil, first: nil, after: nil, cache: nil) ⇒ Object

List single-dataset import jobs for a publisher.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/carddb/resources/imports.rb', line 49

def list_jobs(publisher_id:, dataset_id: nil, status: nil, first: nil, after: nil, cache: nil)
  query = QueryBuilder.import_jobs(
    publisher_id: publisher_id,
    dataset_id: dataset_id,
    status: status,
    first: first,
    after: after
  )
  variables = build_variables(
    publisherId: publisher_id,
    datasetId: dataset_id,
    status: status,
    first: first,
    after: after
  )
  key = cache_key('imports', 'list_jobs', **variables)

  data = with_cache(key, resource: :imports, cache: cache) { connection.execute(query, variables) }

  Collection.new(
    data['importJobs'],
    item_class: ImportJob,
    next_page_loader: lambda { |cursor|
      list_jobs(
        publisher_id: publisher_id,
        dataset_id: dataset_id,
        status: status,
        first: first,
        after: cursor,
        cache: cache
      )
    },
    client: client
  )
end

#preview(input:) ⇒ Object

Preview a single-dataset import source without writing records or schema.



19
20
21
22
# File 'lib/carddb/resources/imports.rb', line 19

def preview(input:)
  data = connection.execute(QueryBuilder.dataset_import_preview, { input: input })
  DatasetImportPreviewResult.new(data['datasetImportPreview'], client: client)
end

#preview_game(input:) ⇒ Object

Preview an advanced game-level import source without writing records or schema.



133
134
135
136
# File 'lib/carddb/resources/imports.rb', line 133

def preview_game(input:)
  data = connection.execute(QueryBuilder.game_import_preview, { input: input })
  GameImportPreview.new(data['gameImportPreview'], client: client)
end

#run(input:) ⇒ Object

Start an async single-dataset import from fileId, data, or sourceUrl.



31
32
33
34
35
36
37
# File 'lib/carddb/resources/imports.rb', line 31

def run(input:)
  config.require_secret_credential!('imports.run')

  query, response_key = import_query_for(input)
  data = connection.execute(query, { input: input })
  ImportJob.new(data[response_key], client: client)
end

#run_game(input:) ⇒ Object

Start an async advanced game-level import from fileId, data, or sourceUrl.



139
140
141
142
143
144
145
# File 'lib/carddb/resources/imports.rb', line 139

def run_game(input:)
  config.require_secret_credential!('imports.run_game')

  query, response_key = game_import_query_for(input)
  data = connection.execute(query, { input: input })
  GameImportJob.new(data[response_key], client: client)
end

#validate(input:) ⇒ Object

Validate a single-dataset import source with dry-run enforced.



25
26
27
28
# File 'lib/carddb/resources/imports.rb', line 25

def validate(input:)
  data = connection.execute(QueryBuilder.dataset_import_validate, { input: input })
  BulkImportResult.new(data['datasetImportValidate'], client: client)
end

#wait_for_game_job(id, interval: 1, timeout: 300) ⇒ Object

Poll an advanced game-level import job until completion or failure.



183
184
185
186
187
# File 'lib/carddb/resources/imports.rb', line 183

def wait_for_game_job(id, interval: 1, timeout: 300)
  wait_until_terminal(id, interval: interval, timeout: timeout) do
    get_game_job(id, cache: false)
  end
end

#wait_for_job(id, interval: 1, timeout: 300) ⇒ Object

Poll a single-dataset import job until completion or failure.



126
127
128
129
130
# File 'lib/carddb/resources/imports.rb', line 126

def wait_for_job(id, interval: 1, timeout: 300)
  wait_until_terminal(id, interval: interval, timeout: timeout) do
    get_job(id, cache: false)
  end
end