gds-api-adapters 63.0.0 → 63.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- require "gds_api/publishing_api_v2"
1
+ require "gds_api/publishing_api"
2
2
  require "time"
3
3
 
4
4
  module GdsApi
@@ -6,7 +6,7 @@ module GdsApi
6
6
  class SpecialRoutePublisher
7
7
  def initialize(options = {})
8
8
  @logger = options[:logger] || GdsApi::Base.logger
9
- @publishing_api = options[:publishing_api] || GdsApi::PublishingApiV2.new(Plek.find("publishing-api"))
9
+ @publishing_api = options[:publishing_api] || GdsApi::PublishingApi.new(Plek.find("publishing-api"))
10
10
  end
11
11
 
12
12
  def publish(options)
@@ -1,4 +1,4 @@
1
- require_relative "base"
1
+ require_relative "publishing_api"
2
2
 
3
3
  # Adapter for the Publishing API.
4
4
  #
@@ -6,492 +6,9 @@ require_relative "base"
6
6
  # @see https://github.com/alphagov/publishing-api/blob/master/doc/publishing-application-examples.md
7
7
  # @see https://github.com/alphagov/publishing-api/blob/master/doc/model.md
8
8
  # @api documented
9
- class GdsApi::PublishingApiV2 < GdsApi::Base
10
- # Put a content item
11
- #
12
- # @param content_id [UUID]
13
- # @param payload [Hash] A valid content item
14
- #
15
- # @see https://github.com/alphagov/publishing-api/blob/master/doc/api.md#put-v2contentcontent_id
16
- def put_content(content_id, payload)
17
- put_json(content_url(content_id), payload)
18
- end
19
-
20
- # Return a content item
21
- #
22
- # Raises exception if the item doesn't exist.
23
- #
24
- # @param content_id [UUID]
25
- # @param params [Hash]
26
- # @option params [String] locale The language, defaults to 'en' in publishing-api.
27
- #
28
- # @return [GdsApi::Response] a content item
29
- #
30
- # @raise [HTTPNotFound] when the content item is not found
31
- # @see https://github.com/alphagov/publishing-api/blob/master/doc/api.md#get-v2contentcontent_id
32
- def get_content(content_id, params = {})
33
- get_json(content_url(content_id, params))
34
- end
35
-
36
- # Find the content_ids for a list of base_paths.
37
- #
38
- # @param base_paths [Array]
39
- # @param exclude_document_types [Array] (optional)
40
- # @param exclude_unpublishing_types [Array] (optional)
41
- # @param with_drafts [Boolean] (optional)
42
- # @return [Hash] a hash, keyed by `base_path` with `content_id` as value
43
- # @example
44
- #
45
- # publishing_api.lookup_content_ids(base_paths: ['/foo', '/bar'])
46
- # # => { "/foo" => "51ac4247-fd92-470a-a207-6b852a97f2db", "/bar" => "261bd281-f16c-48d5-82d2-9544019ad9ca" }
47
- #
48
- # @see https://github.com/alphagov/publishing-api/blob/master/doc/api.md#post-lookup-by-base-path
49
- def lookup_content_ids(base_paths:, exclude_document_types: nil, exclude_unpublishing_types: nil, with_drafts: false)
50
- options = { base_paths: base_paths }
51
- options[:exclude_document_types] = exclude_document_types if exclude_document_types
52
- options[:exclude_unpublishing_types] = exclude_unpublishing_types if exclude_unpublishing_types
53
- options[:with_drafts] = with_drafts if with_drafts
54
- response = post_json("#{endpoint}/lookup-by-base-path", options)
55
- response.to_hash
56
- end
57
-
58
- # Find the content_id for a base_path.
59
- #
60
- # Convenience method if you only need to look up one content_id for a
61
- # base_path. For multiple base_paths, use {GdsApi::PublishingApiV2#lookup_content_ids}.
62
- #
63
- # @param base_path [String]
64
- # @param exclude_document_types [Array] (optional)
65
- # @param exclude_unpublishing_types [Array] (optional)
66
- # @param with_drafts [Boolean] (optional)
67
- #
68
- # @return [UUID] the `content_id` for the `base_path`
69
- #
70
- # @example
71
- #
72
- # publishing_api.lookup_content_id(base_path: '/foo')
73
- # # => "51ac4247-fd92-470a-a207-6b852a97f2db"
74
- #
75
- # @see https://github.com/alphagov/publishing-api/blob/master/doc/api.md#post-lookup-by-base-path
76
- def lookup_content_id(base_path:, exclude_document_types: nil, exclude_unpublishing_types: nil, with_drafts: false)
77
- lookups = lookup_content_ids(
78
- base_paths: [base_path],
79
- exclude_document_types: exclude_document_types,
80
- exclude_unpublishing_types: exclude_unpublishing_types,
81
- with_drafts: with_drafts,
82
- )
83
- lookups[base_path]
84
- end
85
-
86
- # Publish a content item
87
- #
88
- # The publishing-api will "publish" a draft item, so that it will be visible
89
- # on the public site.
90
- #
91
- # @param content_id [UUID]
92
- # @param update_type [String] Either 'major', 'minor' or 'republish'
93
- # @param options [Hash]
94
- # @option options [String] locale The language, defaults to 'en' in publishing-api.
95
- #
96
- # @see https://github.com/alphagov/publishing-api/blob/master/doc/api.md#post-v2contentcontent_idpublish
97
- def publish(content_id, update_type = nil, options = {})
98
- params = {
99
- update_type: update_type,
100
- }
101
-
102
- optional_keys = %i[locale previous_version]
103
-
104
- params = merge_optional_keys(params, options, optional_keys)
105
-
106
- post_json(publish_url(content_id), params)
107
- end
108
-
109
- # Republish a content item
110
- #
111
- # The publishing-api will "republish" a live edition. This can be used to remove an unpublishing or to
112
- # re-send a published edition downstream
113
- #
114
- # @param content_id [UUID]
115
- # @param options [Hash]
116
- # @option options [String] locale The language, defaults to 'en' in publishing-api.
117
- #
118
- # @see ...
119
- def republish(content_id, options = {})
120
- optional_keys = %i[locale previous_version]
121
-
122
- params = merge_optional_keys({}, options, optional_keys)
123
-
124
- post_json(republish_url(content_id), params)
125
- end
126
-
127
- # Import content into the publishing API
128
- #
129
- # The publishing-api will delete any content which has the content
130
- # id provided, and then import the data given.
131
- #
132
- # @param content_id [UUID]
133
- # @param content_items [Array]
134
- #
135
- # @see https://github.com/alphagov/publishing-api/blob/master/doc/api.md#post-v2contentcontent_idimport
136
- def import(content_id, locale, content_items)
137
- params = {
138
- history: content_items,
139
- }
140
-
141
- post_json("#{endpoint}/v2/content/#{content_id}/import?locale=#{locale}", params)
142
- end
143
-
144
- # Unpublish a content item
145
- #
146
- # The publishing API will "unpublish" a live item, to remove it from the public
147
- # site, or update an existing unpublishing.
148
- #
149
- # @param content_id [UUID]
150
- # @param type [String] Either 'withdrawal', 'gone' or 'redirect'.
151
- # @param explanation [String] (optional) Text to show on the page.
152
- # @param alternative_path [String] (optional) Alternative path to show on the page or redirect to.
153
- # @param discard_drafts [Boolean] (optional) Whether to discard drafts on that item. Defaults to false.
154
- # @param previous_version [Integer] (optional) A lock version number for optimistic locking.
155
- # @param locale [String] (optional) The content item locale.
156
- # @param unpublished_at [Time] (optional) The time the content was withdrawn. Ignored for types other than withdrawn
157
- # @param redirects [Array] (optional) Required if no alternative_path is given. An array of redirect values, ie: { path:, type:, destination: }
158
- #
159
- # @see https://github.com/alphagov/publishing-api/blob/master/doc/api.md#post-v2contentcontent_idunpublish
160
- def unpublish(content_id, type:, explanation: nil, alternative_path: nil, discard_drafts: false, allow_draft: false, previous_version: nil, locale: nil, unpublished_at: nil, redirects: nil)
161
- params = {
162
- type: type,
163
- }
164
-
165
- params[:explanation] = explanation if explanation
166
- params[:alternative_path] = alternative_path if alternative_path
167
- params[:previous_version] = previous_version if previous_version
168
- params[:discard_drafts] = discard_drafts if discard_drafts
169
- params[:allow_draft] = allow_draft if allow_draft
170
- params[:locale] = locale if locale
171
- params[:unpublished_at] = unpublished_at.utc.iso8601 if unpublished_at
172
- params[:redirects] = redirects if redirects
173
-
174
- post_json(unpublish_url(content_id), params)
175
- end
176
-
177
- # Discard a draft
178
- #
179
- # Deletes the draft content item.
180
- #
181
- # @param options [Hash]
182
- # @option options [String] locale The language, defaults to 'en' in publishing-api.
183
- # @option options [Integer] previous_version used to ensure the request is discarding the latest lock version of the draft
184
- #
185
- # @see https://github.com/alphagov/publishing-api/blob/master/doc/api.md#post-v2contentcontent_iddiscard-draft
186
- def discard_draft(content_id, options = {})
187
- optional_keys = %i[locale previous_version]
188
-
189
- params = merge_optional_keys({}, options, optional_keys)
190
-
191
- post_json(discard_url(content_id), params)
192
- end
193
-
194
- # Get the link set for the given content_id.
195
- #
196
- # Given a Content ID, it fetchs the existing link set and their version.
197
- #
198
- # @param content_id [String]
199
- #
200
- # @return [GdsApi::Response] A response containing `links` and `version`.
201
- #
202
- # @example
203
- #
204
- # publishing_api.get_links("a-content-id")
205
- # # => {
206
- # "content_id" => "a-content-id",
207
- # "links" => [
208
- # "organisation" => "organisation-content-id",
209
- # "document_collection" => "document-collection-content-id"
210
- # ],
211
- # "version" => 17
212
- # }
213
- #
214
- # @see https://github.com/alphagov/publishing-api/blob/master/doc/api.md#get-v2linkscontent_id
215
- def get_links(content_id)
216
- get_json(links_url(content_id))
217
- end
218
-
219
- # Returns an array of changes to links.
220
- #
221
- # The link changes can be filtered by link_type, source content_id,
222
- # target content_id and user. A maximum of 250 changes will be
223
- # returned.
224
- #
225
- # @param link_types [Array] Array of link_types to filter by.
226
- # @param source_content_ids [Array] Array of source content ids to filter by.
227
- # @param target_content_ids [Array] Array of target content ids to filter by.
228
- # @param users [Array] User UIDs to filter by.
229
- # @example
230
- #
231
- # publishing_api.get_links_changes(
232
- # link_types: ['taxons'],
233
- # target_content_ids: ['a544d48b-1e9e-47fb-b427-7a987c658c14']
234
- # )
235
- #
236
- def get_links_changes(params)
237
- get_json(links_changes_url(params))
238
- end
239
-
240
- # Get expanded links
241
- #
242
- # Return the expanded links of the item.
243
- #
244
- # @param content_id [UUID]
245
- # @param locale [String] Locale with which to generate the expanded links. Unless this is specified, the default locale (`en`) in the Publishing API will be used.
246
- # @param with_drafts [Bool] Whether links to draft-only editions are returned, defaulting to `true`.
247
- # @param generate [Bool] Whether to require publishing-api to generate the expanded links, which may be slow. Defaults to `false`.
248
- #
249
- # @example
250
- #
251
- # publishing_api.get_expanded_links("8157589b-65e2-4df6-92ba-2c91d80006c0", with_drafts: false).to_h
252
- #
253
- # #=> {
254
- # "generated" => "2017-08-01T10:42:49Z",
255
- # "expanded_links" => {
256
- # "organisations" => [
257
- # {
258
- # "content_id" => "21aa83a2-a47f-4189-a252-b02f8c322012",
259
- # ... (and more attributes)
260
- # }
261
- # ]
262
- # }
263
- # }
264
- #
265
- # @see https://github.com/alphagov/publishing-api/blob/master/doc/api.md#get-v2expanded-linkscontent_id
266
- def get_expanded_links(content_id, locale: nil, with_drafts: true, generate: false)
267
- params = {}
268
- params[:with_drafts] = "false" unless with_drafts
269
- params[:generate] = "true" if generate
270
- params[:locale] = locale if locale
271
- query = query_string(params)
272
- validate_content_id(content_id)
273
- get_json("#{endpoint}/v2/expanded-links/#{content_id}#{query}")
274
- end
275
-
276
- # Patch the links of a content item
277
- #
278
- # @param content_id [UUID]
279
- # @param params [Hash]
280
- # @option params [Hash] links A "links hash"
281
- # @option params [Integer] previous_version The previous version (returned by `get_links`). If this version is not the current version, the publishing-api will reject the change and return 409 Conflict. (optional)
282
- # @option params [Boolean] bulk_publishing Set to true to indicate that this is part of a mass-republish. Allows the publishing-api to prioritise human-initiated publishing (optional, default false)
283
- # @example
284
- #
285
- # publishing_api.patch_links(
286
- # '86963c13-1f57-4005-b119-e7cf3cb92ecf',
287
- # links: {
288
- # topics: ['d6e1527d-d0c0-40d5-9603-b9f3e6866b8a'],
289
- # mainstream_browse_pages: ['d6e1527d-d0c0-40d5-9603-b9f3e6866b8a'],
290
- # },
291
- # previous_version: 10,
292
- # bulk_publishing: true
293
- # )
294
- #
295
- # @see https://github.com/alphagov/publishing-api/blob/master/doc/api.md#patch-v2linkscontent_id
296
- def patch_links(content_id, params)
297
- payload = {
298
- links: params.fetch(:links),
299
- }
300
-
301
- payload = merge_optional_keys(payload, params, %i[previous_version bulk_publishing])
302
-
303
- patch_json(links_url(content_id), payload)
304
- end
305
-
306
- # Get a list of content items from the Publishing API.
307
- #
308
- # The only required key in the params hash is `document_type`. These will be used to filter down the content items being returned by the API. Other allowed options can be seen from the link below.
309
- #
310
- # @param params [Hash] At minimum, this hash has to include the `document_type` of the content items we wish to see. All other optional keys are documented above.
311
- #
312
- # @example
313
- #
314
- # publishing_api.get_content_items(
315
- # document_type: 'taxon',
316
- # q: 'Driving',
317
- # page: 1,
318
- # per_page: 50,
319
- # publishing_app: 'content-tagger',
320
- # fields: ['title', 'description', 'public_updated_at'],
321
- # locale: 'en',
322
- # order: '-public_updated_at'
323
- # )
324
- #
325
- # @see https://github.com/alphagov/publishing-api/blob/master/doc/api.md#get-v2content
326
- def get_content_items(params)
327
- query = query_string(params)
328
- get_json("#{endpoint}/v2/content#{query}")
329
- end
330
-
331
- # Returns an Enumerator of content items for the provided
332
- # query string parameters.
333
- #
334
- # @param params [Hash]
335
- #
336
- # @return [Enumerator] an enumerator of content items
337
- #
338
- # @see https://github.com/alphagov/publishing-api/blob/master/doc/api.md#get-v2content
339
- def get_content_items_enum(params)
340
- Enumerator.new do |yielder|
341
- (1..Float::INFINITY).each do |index|
342
- merged_params = params.merge(page: index)
343
- page = get_content_items(merged_params).to_h
344
- results = page.fetch("results", [])
345
- results.each do |result|
346
- yielder << result
347
- end
348
- break if page.fetch("pages") <= index
349
- end
350
- end
351
- end
352
-
353
- # FIXME: Add documentation
354
- #
355
- # @see https://github.com/alphagov/publishing-api/blob/master/doc/api.md#get-v2linkables
356
- def get_linkables(document_type: nil)
357
- if document_type.nil?
358
- raise ArgumentError.new("Please provide a `document_type`")
359
- end
360
-
361
- get_json("#{endpoint}/v2/linkables?document_type=#{document_type}")
362
- end
363
-
364
- # FIXME: Add documentation
365
- #
366
- # @see https://github.com/alphagov/publishing-api/blob/master/doc/api.md#get-v2linkedcontent_id
367
- def get_linked_items(content_id, params = {})
368
- query = query_string(params)
369
- validate_content_id(content_id)
370
- get_json("#{endpoint}/v2/linked/#{content_id}#{query}")
371
- end
372
-
373
- # Returns a paginated list of editions for the provided query string
374
- # parameters.
375
- #
376
- # @param params [Hash]
377
- #
378
- # @return [GdsApi::Response] a paginated list of editions
379
- #
380
- # @see https://github.com/alphagov/publishing-api/blob/master/doc/api.md#get-v2editions
381
- def get_editions(params = {})
382
- get_json(get_editions_url(params))
383
- end
384
-
385
- # Returns an Enumerator of Response objects for each page of results of
386
- # editions for the provided query string parameters.
387
- #
388
- # @param params [Hash]
389
- #
390
- # @return [Enumerator] an enumerator of editions responses
391
- #
392
- # @see https://github.com/alphagov/publishing-api/blob/master/doc/api.md#get-v2editions
393
- def get_paged_editions(params = {})
394
- Enumerator.new do |yielder|
395
- next_link = get_editions_url(params)
396
- while next_link
397
- yielder.yield begin
398
- response = get_json(next_link)
399
- end
400
- next_link_info = response["links"].select { |link| link["rel"] == "next" }.first
401
- next_link = next_link_info && next_link_info["href"]
402
- end
403
- end
404
- end
405
-
406
- # Returns a mapping of content_ids => links hashes
407
- #
408
- # @param content_ids [Array]
409
- #
410
- # @return [Hash] a mapping of content_id => links
411
- #
412
- # @example
413
- #
414
- # publishing_api.get_links_for_content_ids([
415
- # "e1067450-7d13-45ff-ada4-5e3dd4025fb7",
416
- # "72ed754c-4c82-415f-914a-ab6760454cb4"
417
- # ])
418
- #
419
- # #=> {
420
- # "e1067450-7d13-45ff-ada4-5e3dd4025fb7" => {
421
- # links: {
422
- # taxons: ["13bba81c-b2b1-4b13-a3de-b24748977198"]},
423
- # ... (and more attributes)
424
- # version: 10},
425
- # "72ed754c-4c82-415f-914a-ab6760454cb4" => { ..etc }
426
- # }
427
- #
428
- def get_links_for_content_ids(content_ids)
429
- post_json("#{endpoint}/v2/links/by-content-id", content_ids: content_ids).to_hash
430
- end
431
-
432
- # Reserves a path for a publishing application
433
- #
434
- # Returns success or failure only.
435
- #
436
- # @param payload [Hash]
437
- # @option payload [Hash] publishing_app The publishing application, like `content-tagger`
438
- #
439
- # @see https://docs.publishing.service.gov.uk/apis/publishing-api/api.html#put-pathsbase_path
440
- def put_path(base_path, payload)
441
- url = "#{endpoint}/paths#{base_path}"
442
- put_json(url, payload)
443
- end
444
-
445
- private
446
-
447
- def content_url(content_id, params = {})
448
- validate_content_id(content_id)
449
- query = query_string(params)
450
- "#{endpoint}/v2/content/#{content_id}#{query}"
451
- end
452
-
453
- def links_url(content_id)
454
- validate_content_id(content_id)
455
- "#{endpoint}/v2/links/#{content_id}"
456
- end
457
-
458
- def links_changes_url(params = {})
459
- query = query_string(params)
460
- "#{endpoint}/v2/links/changes#{query}"
461
- end
462
-
463
- def publish_url(content_id)
464
- validate_content_id(content_id)
465
- "#{endpoint}/v2/content/#{content_id}/publish"
466
- end
467
-
468
- def republish_url(content_id)
469
- validate_content_id(content_id)
470
- "#{endpoint}/v2/content/#{content_id}/republish"
471
- end
472
-
473
- def unpublish_url(content_id)
474
- validate_content_id(content_id)
475
- "#{endpoint}/v2/content/#{content_id}/unpublish"
476
- end
477
-
478
- def discard_url(content_id)
479
- validate_content_id(content_id)
480
- "#{endpoint}/v2/content/#{content_id}/discard-draft"
481
- end
482
-
483
- def get_editions_url(params)
484
- query = query_string(params)
485
- "#{endpoint}/v2/editions#{query}"
486
- end
487
-
488
- def merge_optional_keys(params, options, optional_keys)
489
- optional_keys.each_with_object(params) do |optional_key, hash|
490
- hash.merge!(optional_key => options[optional_key]) if options[optional_key]
491
- end
492
- end
493
-
494
- def validate_content_id(content_id)
495
- raise ArgumentError, "content_id cannot be nil" unless content_id
9
+ class GdsApi::PublishingApiV2 < GdsApi::PublishingApi
10
+ def initialize(*args)
11
+ warn "GdsApi::PublishingApiV2 is deprecated. Use GdsApi::PublishingApi instead."
12
+ super
496
13
  end
497
14
  end