Skip to content

Commit

Permalink
Remove usages of GetData (#747)
Browse files Browse the repository at this point in the history
  • Loading branch information
guarani authored Mar 8, 2024
2 parents 61d077c + 745c359 commit b6d4b2e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 39 deletions.
23 changes: 5 additions & 18 deletions WordPressKit/AnnouncementServiceRemote.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,11 @@ public class AnnouncementServiceRemote: ServiceRemoteWordPressComREST {
}

let path = self.path(forEndpoint: endPoint, withVersion: ._2_0)

wordPressComRestApi.GETData(path, parameters: nil) { result in
switch result {
case .success((let data, _)):
do {
let announcements = try self.decodeAnnouncements(from: data)
completion(.success(announcements))
} catch {
completion(.failure(error))
}
case .failure(let error):
completion(.failure(error))
}
Task { @MainActor [wordPressComRestApi] in
await wordPressComRestApi.perform(.get, URLString: path, type: AnnouncementsContainer.self)
.map { $0.body.announcements }
.eraseToError()
.execute(completion)
}
}
}
Expand All @@ -45,11 +37,6 @@ private extension AnnouncementServiceRemote {
path?.queryItems = makeQueryItems(appId: appId, appVersion: appVersion, locale: locale)
return path?.string
}

func decodeAnnouncements(from data: Data) throws -> [Announcement] {
let container = try JSONDecoder().decode(AnnouncementsContainer.self, from: data)
return container.announcements
}
}

// MARK: - Constants
Expand Down
32 changes: 11 additions & 21 deletions WordPressKit/Plans/PlanServiceRemote.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,28 +195,18 @@ public class PlanServiceRemote: ServiceRemoteWordPressComREST {
public func getZendeskMetadata(siteID: Int, completion: @escaping (Result<ZendeskMetadata, Error>) -> Void) {
let endpoint = "me/sites"
let path = self.path(forEndpoint: endpoint, withVersion: ._1_1)
let parameters: [String: String] = ["fields": "ID, zendesk_site_meta"]

wordPressComRestApi.GETData(path, parameters: parameters as [String: AnyObject]) { result in
switch result {
case .success((let data, _)):
do {
let metadata = try self.decodeZendeskMetadata(from: data, siteID: siteID)
completion(.success(metadata))
} catch {
completion(.failure(error))
let parameters = ["fields": "ID, zendesk_site_meta"] as [String: AnyObject]

Task { @MainActor [wordPressComRestApi] in
await wordPressComRestApi.perform(.get, URLString: path, parameters: parameters, type: ZendeskSiteContainer.self)
.eraseToError()
.flatMap { container in
guard let metadata = container.body.sites.filter({ $0.ID == siteID }).first?.zendeskMetadata else {
return .failure(PlanServiceRemoteError.noMetadata)
}
return .success(metadata)
}
case .failure(let error):
completion(.failure(error))
}
}
}

private func decodeZendeskMetadata(from data: Data, siteID: Int) throws -> ZendeskMetadata {
let container = try JSONDecoder().decode(ZendeskSiteContainer.self, from: data)
guard let metadata = container.sites.filter({ $0.ID == siteID }).first?.zendeskMetadata else {
throw PlanServiceRemoteError.noMetadata
.execute(completion)
}
return metadata
}
}

0 comments on commit b6d4b2e

Please sign in to comment.