Change so that we only send 300 markers per request. Fixes #3711

This commit is contained in:
Maurice Parker 2022-10-28 15:53:26 -05:00
parent 6baee15007
commit 948b02d0ac
1 changed files with 40 additions and 24 deletions

View File

@ -7,6 +7,7 @@
// //
import Foundation import Foundation
import RSCore
import RSWeb import RSWeb
import Secrets import Secrets
@ -820,33 +821,48 @@ extension FeedlyAPICaller: FeedlyMarkArticlesService {
fatalError("\(components) does not produce a valid URL.") fatalError("\(components) does not produce a valid URL.")
} }
var request = URLRequest(url: url) let articleIdChunks = Array(articleIds).chunked(into: 300)
request.httpMethod = "POST" let dispatchGroup = DispatchGroup()
request.addValue("application/json", forHTTPHeaderField: HTTPRequestHeader.contentType) var groupError: Error? = nil
request.addValue("application/json", forHTTPHeaderField: "Accept-Type")
request.addValue("OAuth \(accessToken)", forHTTPHeaderField: HTTPRequestHeader.authorization) for articleIdChunk in articleIdChunks {
do { var request = URLRequest(url: url)
let body = MarkerEntriesBody(action: action.actionValue, entryIds: Array(articleIds)) request.httpMethod = "POST"
let encoder = JSONEncoder() request.addValue("application/json", forHTTPHeaderField: HTTPRequestHeader.contentType)
let data = try encoder.encode(body) request.addValue("application/json", forHTTPHeaderField: "Accept-Type")
request.httpBody = data request.addValue("OAuth \(accessToken)", forHTTPHeaderField: HTTPRequestHeader.authorization)
} catch {
return DispatchQueue.main.async { do {
completion(.failure(error)) let body = MarkerEntriesBody(action: action.actionValue, entryIds: Array(articleIdChunk))
let encoder = JSONEncoder()
let data = try encoder.encode(body)
request.httpBody = data
} catch {
return DispatchQueue.main.async {
completion(.failure(error))
}
}
dispatchGroup.enter()
send(request: request, resultType: String.self, dateDecoding: .millisecondsSince1970, keyDecoding: .convertFromSnakeCase) { result in
switch result {
case .success(let (httpResponse, _)):
if httpResponse.statusCode != 200 {
groupError = URLError(.cannotDecodeContentData)
}
case .failure(let error):
groupError = error
}
dispatchGroup.leave()
} }
} }
send(request: request, resultType: String.self, dateDecoding: .millisecondsSince1970, keyDecoding: .convertFromSnakeCase) { result in dispatchGroup.notify(queue: .main) {
switch result { if let groupError = groupError {
case .success(let (httpResponse, _)): completion(.failure(groupError))
if httpResponse.statusCode == 200 { } else {
completion(.success(())) completion(.success(()))
} else {
completion(.failure(URLError(.cannotDecodeContentData)))
}
case .failure(let error):
completion(.failure(error))
} }
} }
} }