diff --git a/Sources/XuqmSDK/Core/CommonApiClient.swift b/Sources/XuqmSDK/Core/CommonApiClient.swift new file mode 100644 index 0000000..f86b753 --- /dev/null +++ b/Sources/XuqmSDK/Core/CommonApiClient.swift @@ -0,0 +1,32 @@ +import Foundation + +public enum CommonApiClient { + + public static func postJSON(urlString: String, body: [String: Any]) async throws -> [String: Any] { + guard let url = URL(string: urlString) else { + throw URLError(.badURL) + } + + var request = URLRequest(url: url) + request.httpMethod = "POST" + request.setValue("application/json", forHTTPHeaderField: "Content-Type") + request.setValue("application/json", forHTTPHeaderField: "Accept") + request.httpBody = try JSONSerialization.data(withJSONObject: body) + + let (data, response) = try await URLSession.shared.data(for: request) + guard let http = response as? HTTPURLResponse else { + throw URLError(.badServerResponse) + } + + let json = try JSONSerialization.jsonObject(with: data) as? [String: Any] ?? [:] + guard (200..<300).contains(http.statusCode) else { + throw NSError( + domain: "CommonApiClient", + code: http.statusCode, + userInfo: [NSLocalizedDescriptionKey: json["message"] as? String ?? "HTTP \(http.statusCode)"] + ) + } + + return json + } +} diff --git a/XuqmSDK.podspec b/XuqmSDK.podspec index 7d58277..8cff31f 100644 --- a/XuqmSDK.podspec +++ b/XuqmSDK.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'XuqmSDK' - s.version = '0.1.0' + s.version = '0.1.1' s.summary = 'XuqmGroup iOS SDK — IM, Push Notifications, Version Management' s.homepage = 'https://xuqinmin.com/xuqinmin12/XuqmGroup-iOSSDK' s.license = { :type => 'MIT' } diff --git a/XuqmWebViewSDK.podspec b/XuqmWebViewSDK.podspec index 5a9a72f..b2f47dc 100644 --- a/XuqmWebViewSDK.podspec +++ b/XuqmWebViewSDK.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'XuqmWebViewSDK' - s.version = '0.1.0' + s.version = '0.1.1' s.summary = 'XuqmGroup iOS SDK — WebView module' s.homepage = 'https://xuqinmin.com/xuqinmin12/XuqmGroup-iOSSDK' s.license = { :type => 'MIT' }