@@ -2,8 +2,7 @@ module CKAN
2
2
module V26
3
3
class Depaginator
4
4
include CKAN ::Modules ::URLBuilder
5
- # temp increase to 10000, should normally be 100
6
- MAX_DELETIONS = 10_000
5
+ MAX_DELETIONS = 100
7
6
8
7
def self . depaginate ( *args , **kwargs )
9
8
new ( *args , **kwargs ) . depaginate
@@ -18,60 +17,63 @@ def initialize(base_url, existing_total:)
18
17
def depaginate
19
18
loop do
20
19
url = append_url ( base_url , params : { "start" => results . count } )
20
+ begin
21
+ raw_response = url . read
22
+ response = JSON . parse ( raw_response )
23
+ result = response . fetch ( "result" )
24
+ page = result . fetch ( "results" )
25
+ total_expected = result . fetch ( "count" )
26
+ total_expected_from_first_response ||= total_expected
27
+
28
+ if total_expected != total_expected_from_first_response
29
+ raise ExpectedTotalChangedError , <<~MESSAGE
30
+ New expected count `#{ total_expected } ` from CKAN does not match
31
+ the original expected count of `#{ total_expected_from_first_response } `.
21
32
22
- raw_response = url . read
23
- response = JSON . parse ( raw_response )
24
- result = response . fetch ( "result" )
25
- page = result . fetch ( "results" )
26
- total_expected = result . fetch ( "count" )
27
- total_expected_from_first_response ||= total_expected
28
-
29
- if total_expected != total_expected_from_first_response
30
- raise ExpectedTotalChangedError , <<~MESSAGE
31
- New expected count `#{ total_expected } ` from CKAN does not match
32
- the original expected count of `#{ total_expected_from_first_response } `.
33
-
34
- CKAN response:
35
- #{ raw_response }
36
- MESSAGE
37
- end
33
+ CKAN response:
34
+ #{ raw_response }
35
+ MESSAGE
36
+ end
38
37
39
- results . concat ( page )
38
+ results . concat ( page )
40
39
41
- if results . count > total_expected
42
- raise MoreResultsThanExpectedError , <<~MESSAGE
43
- We have received more results (#{ results . count } ) than expected (#{ total_expected } ).
40
+ if results . count > total_expected
41
+ raise MoreResultsThanExpectedError , <<~MESSAGE
42
+ We have received more results (#{ results . count } ) than expected (#{ total_expected } ).
44
43
45
- CKAN response:
46
- #{ raw_response }
47
- MESSAGE
48
- end
44
+ CKAN response:
45
+ #{ raw_response }
46
+ MESSAGE
47
+ end
49
48
50
- if page . empty?
51
- if results . count == total_expected
52
- number_being_deleted = existing_total - results . count
49
+ if page . empty?
50
+ if results . count == total_expected
51
+ number_being_deleted = existing_total - results . count
53
52
54
- if number_being_deleted <= MAX_DELETIONS
55
- break
56
- else
57
- raise DeletionTooLargeError , <<~MESSAGE
58
- Attempting to delete `#{ number_being_deleted } ` datasets.
53
+ if number_being_deleted <= MAX_DELETIONS
54
+ break
55
+ else
56
+ raise DeletionTooLargeError , <<~MESSAGE
57
+ Attempting to delete `#{ number_being_deleted } ` datasets.
59
58
60
- No more than #{ MAX_DELETIONS } datasets can be deleted at once.
59
+ No more than #{ MAX_DELETIONS } datasets can be deleted at once.
60
+
61
+ CKAN response:
62
+ #{ raw_response }
63
+ MESSAGE
64
+ end
65
+ else
66
+ raise EarlyEmptyPageError , <<~MESSAGE
67
+ We have received an empty page but have only received `#{ results . count } `
68
+ results rather than the expected `#{ total_expected } ` results.
61
69
62
70
CKAN response:
63
71
#{ raw_response }
64
72
MESSAGE
65
73
end
66
- else
67
- raise EarlyEmptyPageError , <<~MESSAGE
68
- We have received an empty page but have only received `#{ results . count } `
69
- results rather than the expected `#{ total_expected } ` results.
70
-
71
- CKAN response:
72
- #{ raw_response }
73
- MESSAGE
74
74
end
75
+ rescue OpenURI ::HTTPError
76
+ logger . error "Problem getting response from #{ url } "
75
77
end
76
78
end
77
79
0 commit comments