Skip to content

Commit

Permalink
Move decoding to client.
Browse files Browse the repository at this point in the history
  • Loading branch information
borut-t committed Jun 5, 2024
1 parent dd8a2bc commit a3d9531
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
39 changes: 20 additions & 19 deletions Sources/LinkedIn/API/LinkedInAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,39 +37,41 @@ public extension LinkedInAPI {
throw Error.invalidUrl
}

let response = try await client.request(
method: "POST",
url: url,
headers: ["Content-Type": "application/x-www-form-urlencoded"]
)

let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
decoder.dateDecodingStrategy = .custom { decoder in
let container = try decoder.singleValueContainer()
let secondsRemaining = try container.decode(Int.self)
return Date().addingTimeInterval(TimeInterval(secondsRemaining))
}
let decoded = try decoder.decode(LinkedInAuthResponse.self, from: response)

return decoded
let response = try await client.request(
method: "POST",
url: url,
headers: ["Content-Type": "application/x-www-form-urlencoded"],
decodeTo: LinkedInAuthResponse.self,
with: decoder
)

return response
}

func loadProfile(with request: LinkedInProfileRequest) async throws -> LinkedInProfileResponse {
guard let url = URL(string: Endpoints.profile.url) else { throw Error.invalidUrl }

let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
decoder.dateDecodingStrategy = .iso8601

let response = try await client.request(
method: "GET",
url: url,
headers: ["Authorization": "Bearer \(request.token)"]
headers: ["Authorization": "Bearer \(request.token)"],
decodeTo: LinkedInProfileResponse.self,
with: decoder
)

let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
decoder.dateDecodingStrategy = .iso8601
let decoded = try decoder.decode(LinkedInProfileResponse.self, from: response)

return decoded
return response
}

func loadEmail(with request: LinkedInProfileRequest) async throws -> LinkedInEmailValueResponse {
Expand All @@ -78,12 +80,11 @@ public extension LinkedInAPI {
let response = try await client.request(
method: "GET",
url: url,
headers: ["Authorization": "Bearer \(request.token)"]
headers: ["Authorization": "Bearer \(request.token)"],
decodeTo: LinkedInEmailResponse.self
)

let decoded = try JSONDecoder().decode(LinkedInEmailResponse.self, from: response)

guard let emailObject = decoded.elements.first?.handle else {
guard let emailObject = response.elements.first?.handle else {
throw Error.invalidResponse
}

Expand Down
10 changes: 8 additions & 2 deletions Sources/LinkedIn/Core/HttpClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@
import Foundation

struct HttpClient {
func request(method: String, url: URL, headers: [String: String]?) async throws -> Data {
func request<D: Decodable>(
method: String,
url: URL,
headers: [String: String]?,
decodeTo decode: D.Type,
with decoder: JSONDecoder = .init()
) async throws -> D {
var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = method
urlRequest.allHTTPHeaderFields = headers
Expand All @@ -21,6 +27,6 @@ struct HttpClient {
throw NSError(domain: "HTTP Error", code: httpResponse.statusCode, userInfo: nil)
}

return data
return try decoder.decode(decode, from: data)
}
}

0 comments on commit a3d9531

Please sign in to comment.