|
1 | 1 | import { |
2 | 2 | all, call, put, select, takeEvery, |
3 | 3 | } from 'redux-saga/effects'; |
4 | | -import { Utils } from 'manifesto.js'; |
5 | 4 | import normalizeUrl from 'normalize-url'; |
6 | 5 | import ActionTypes from '../actions/action-types'; |
7 | 6 | import { |
8 | 7 | receiveManifest, receiveManifestFailure, receiveInfoResponse, |
9 | 8 | receiveInfoResponseFailure, receiveDegradedInfoResponse, |
10 | 9 | receiveSearch, receiveSearchFailure, |
11 | 10 | receiveAnnotation, receiveAnnotationFailure, |
| 11 | + receiveProbeResponse, receiveProbeResponseFailure, |
| 12 | + receiveDegradedProbeResponse, |
12 | 13 | } from '../actions'; |
13 | | -import { getTokenService } from '../../lib/getServices'; |
| 14 | +import { anyAuthServices, getTokenService } from '../../lib/getServices'; |
14 | 15 | import { |
15 | 16 | getManifests, |
16 | 17 | getRequestsConfig, |
17 | 18 | getAccessTokens, |
18 | 19 | selectInfoResponse, |
| 20 | + selectProbeResponse, |
19 | 21 | } from '../selectors'; |
20 | 22 |
|
21 | 23 | /** */ |
@@ -80,10 +82,13 @@ function* fetchIiifResourceWithAuth(url, iiifResource, options, { degraded, fail |
80 | 82 |
|
81 | 83 | const id = json['@id'] || json.id; |
82 | 84 | if (response.ok) { |
83 | | - if (normalizeUrl(id, { stripAuthentication: false }) |
| 85 | + if (id && normalizeUrl(id, { stripAuthentication: false }) |
84 | 86 | === normalizeUrl(url.replace(/info\.json$/, ''), { stripAuthentication: false })) { |
85 | | - yield put(success({ json, response, tokenServiceId })); |
86 | | - return; |
| 87 | + if (!json.substitute) { |
| 88 | + // substitute indicates the Auth2 equivalent of a degraded response, should fall through |
| 89 | + yield put(success({ json, response, tokenServiceId })); |
| 90 | + return; |
| 91 | + } |
87 | 92 | } |
88 | 93 | } else if (response.status !== 401) { |
89 | 94 | yield put(failure({ |
@@ -121,7 +126,7 @@ function* getAccessTokenService(resource) { |
121 | 126 | const manifestoCompatibleResource = resource && resource.__jsonld |
122 | 127 | ? resource |
123 | 128 | : { ...resource, options: {} }; |
124 | | - const services = Utils.getServices(manifestoCompatibleResource).filter(s => s.getProfile().match(/http:\/\/iiif.io\/api\/auth\//)); |
| 129 | + const services = anyAuthServices(manifestoCompatibleResource); |
125 | 130 | if (services.length === 0) return undefined; |
126 | 131 |
|
127 | 132 | const accessTokens = yield select(getAccessTokens); |
@@ -161,6 +166,30 @@ export function* fetchInfoResponse({ imageResource, infoId, windowId }) { |
161 | 166 | yield call(fetchIiifResourceWithAuth, `${infoId.replace(/\/$/, '')}/info.json`, iiifResource, {}, callbacks); |
162 | 167 | } |
163 | 168 |
|
| 169 | +/** @private */ |
| 170 | +export function* fetchProbeResponse({ resource, probeId, windowId }) { |
| 171 | + let iiifResource = resource; |
| 172 | + if (!iiifResource) { |
| 173 | + iiifResource = yield select(selectProbeResponse, { probeId }); |
| 174 | + } |
| 175 | + |
| 176 | + const callbacks = { |
| 177 | + degraded: ({ |
| 178 | + json, response, tokenServiceId, |
| 179 | + }) => receiveDegradedProbeResponse(probeId, json, response.ok, tokenServiceId, windowId), |
| 180 | + failure: ({ |
| 181 | + error, json, response, tokenServiceId, |
| 182 | + }) => ( |
| 183 | + receiveProbeResponseFailure(probeId, error, tokenServiceId) |
| 184 | + ), |
| 185 | + success: ({ |
| 186 | + json, response, tokenServiceId, |
| 187 | + }) => receiveProbeResponse(probeId, json, response.ok, tokenServiceId), |
| 188 | + }; |
| 189 | + |
| 190 | + yield call(fetchIiifResourceWithAuth, probeId, iiifResource, {}, callbacks); |
| 191 | +} |
| 192 | + |
164 | 193 | /** @private */ |
165 | 194 | export function* fetchSearchResponse({ |
166 | 195 | windowId, companionWindowId, query, searchId, |
@@ -215,6 +244,7 @@ export default function* iiifSaga() { |
215 | 244 | yield all([ |
216 | 245 | takeEvery(ActionTypes.REQUEST_MANIFEST, fetchManifest), |
217 | 246 | takeEvery(ActionTypes.REQUEST_INFO_RESPONSE, fetchInfoResponse), |
| 247 | + takeEvery(ActionTypes.REQUEST_PROBE_RESPONSE, fetchProbeResponse), |
218 | 248 | takeEvery(ActionTypes.REQUEST_SEARCH, fetchSearchResponse), |
219 | 249 | takeEvery(ActionTypes.REQUEST_ANNOTATION, fetchAnnotation), |
220 | 250 | takeEvery(ActionTypes.ADD_RESOURCE, fetchResourceManifest), |
|
0 commit comments