Skip to content
This repository was archived by the owner on Jul 5, 2023. It is now read-only.

Commit 499f86b

Browse files
authored
Merge pull request #2 from hoyelam/fix/macos-correct-support
Fix: correct OS versions support
2 parents 141313b + 81420ed commit 499f86b

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import PackageDescription
66
let package = Package(
77
name: "DeepLAPI",
88
platforms: [
9-
.macOS("10.16"), .iOS("15"), .tvOS("14"), .watchOS("7")
9+
.macOS("10.16"), .iOS("14"), .tvOS("14"), .watchOS("7")
1010
],
1111
products: [
1212
// Products define the executables and libraries a package produces, and make them visible to other packages.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
DeepAPI is a Swift open-source library that implements Swift-based clients to interact with the DeepL API using an Authentication Key. You can get an Authentication Key on their website. As of right now, this library supports only the free version of the API
55

66
### Installation
7-
The library requires macOS 10.16+ / iOS 15.0+ / tvOS 14.0+ / watchOS 7.0+
7+
The library requires macOS 10.16+ / iOS 14.0+ / tvOS 14.0+ / watchOS 7.0+
88

99
You can add DeepLAPI-Swift by adding it as a Swift Package Manager dependency:
1010
1. From the *File* menu, select *Add packages*
@@ -23,7 +23,7 @@ dependencies: [
2323
To translate a text
2424
``` swift
2525
let authToken = "12345QWERTASDFG" // Provided by DeepL
26-
let service = DeepLAPI(accessToken: testToken)
26+
let service = DeepLAPI(authToken: testToken)
2727
let request = TranslationRequest(
2828
text: "Hello World!",
2929
sourceLanguage: .english,

Sources/DeepLAPI/Base/HTTPClient.swift

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ extension HTTPClient {
1919
guard !DeepLAPI.authToken.isEmpty else {
2020
throw RequestError.noAuthenticationToken
2121
}
22-
22+
2323
var urlComponents = URLComponents()
2424
urlComponents.scheme = endpoint.scheme
2525
urlComponents.host = endpoint.host
@@ -41,7 +41,34 @@ extension HTTPClient {
4141
request.httpBody = try? JSONSerialization.data(withJSONObject: body, options: [])
4242
}
4343

44-
let (data, response) = try await URLSession.shared.data(for: request, delegate: nil)
44+
if #available(macOS 12.0, iOS 15.0, *) {
45+
let (data, response) = try await URLSession.shared.data(for: request, delegate: nil)
46+
return try self.handleResponse(data, response, responseModel)
47+
} else {
48+
return try await withCheckedThrowingContinuation { continuation in
49+
let task = URLSession.shared.dataTask(with: request) { data, response, error in
50+
guard let data = data else {
51+
if let error = error {
52+
continuation.resume(throwing: error)
53+
}
54+
55+
return
56+
}
57+
58+
do {
59+
let result = try self.handleResponse(data, response, responseModel)
60+
continuation.resume(returning: result)
61+
} catch let error {
62+
continuation.resume(throwing: error)
63+
}
64+
}
65+
66+
task.resume()
67+
}
68+
}
69+
}
70+
71+
private func handleResponse<T: Decodable>(_ data: Data, _ response: URLResponse?, _ responseModel: T.Type) throws -> T {
4572
guard let response = response as? HTTPURLResponse else {
4673
throw RequestError.noResponse
4774
}

0 commit comments

Comments
 (0)