Skip to content

Commit

Permalink
Merge pull request #25 from AppcentMobile/feature/network-error-statu…
Browse files Browse the repository at this point in the history
…scode

🎷 [UPDATE] HTTP status code added for network error.
  • Loading branch information
burakcolakappcent authored Feb 1, 2024
2 parents d260435 + aa086c6 commit ff96ce2
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public enum ACMGenericCallbacks {
/// Void callback for generic closures
public typealias VoidCallback = (() -> Void)?
/// Error callback for generic closures
public typealias ErrorCallback = ((Error?) -> Void)?
public typealias ErrorCallback = ((ACMBaseNetworkError?) -> Void)?
/// Info callback with success check and error for generic closures
public typealias InfoCallback = ((Bool?, Error?) -> Void)?
/// Success callback with generic response for closures
Expand All @@ -21,6 +21,5 @@ public enum ACMGenericCallbacks {
/// Progress callback with generic response for closures
public typealias ProgressCallback = ((ACMProgressModel) -> Void)?
/// Success callback with generic response for closures
public typealias StreamCallback = ((Data) -> Void)

public typealias StreamCallback = (Data) -> Void
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import Foundation

struct ACMPListContants {
enum ACMPListContants {
static let fileName = "ACMConfig"
static let fileExtension = "plist"
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ extension ACMNetworking {

extension ACMNetworking: URLSessionTaskDelegate, URLSessionDelegate, URLSessionDataDelegate {
/// URL Session data task for stream requests
public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
public func urlSession(_: URLSession, dataTask _: URLSessionDataTask, didReceive data: Data) {
let dataString = String(data: data, encoding: .utf8) ?? ""
if !dataString.contains("[DONE]") {
let response = dataString.components(separatedBy: "\n")
.filter{ !$0.replacingOccurrences(of: " ", with: "").isEmpty }
.filter { !$0.replacingOccurrences(of: " ", with: "").isEmpty }
.map { $0.replacingOccurrences(of: "data:", with: "")
.replacingOccurrences(of: " ", with: "")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ extension ACMNetworking {
ACMNetworkConstants.responseNullMessage,
])
endpoint.logger?.error(message)
onError?(ACMBaseNetworkError(message: ACMNetworkConstants.errorMessage, log: ACMNetworkConstants.responseNullMessage, endpoint: endpoint))
onError?(ACMBaseNetworkError(message: ACMNetworkConstants.errorMessage, log: ACMNetworkConstants.responseNullMessage, endpoint: endpoint, statusCode: 200))
return
}
}
Expand Down Expand Up @@ -141,7 +141,7 @@ extension ACMNetworking {

extension ACMNetworking {
/// Handle server response
func handleResult<T: Decodable>(with endpoint: ACMBaseEndpoint, data: Data, onSuccess: ACMGenericCallbacks.ResponseCallback<T>, onError: ACMGenericCallbacks.ErrorCallback) {
func handleResult<T: Decodable>(with endpoint: ACMBaseEndpoint, httpResponse: HTTPURLResponse, data: Data, onSuccess: ACMGenericCallbacks.ResponseCallback<T>, onError: ACMGenericCallbacks.ErrorCallback) {
do {
let dataString = String(data: data, encoding: .utf8) ?? ""
let info = endpoint.stringUtils?.merge(list: [
Expand Down Expand Up @@ -195,7 +195,7 @@ extension ACMNetworking {
} catch let e {
let errorMessage = String(format: ACMNetworkConstants.dataParseErrorMessage, e.localizedDescription)
endpoint.logger?.warning(errorMessage)
onError?(ACMBaseNetworkError(message: ACMNetworkConstants.errorMessage, log: errorMessage, endpoint: endpoint))
onError?(ACMBaseNetworkError(message: ACMNetworkConstants.errorMessage, log: errorMessage, endpoint: endpoint, statusCode: httpResponse.statusCode))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import Foundation

public extension ACMNetworking {
func stream(to endpoint: ACMBaseEndpoint,
currentRetryCount: Int? = 0,
currentRetryCount _: Int? = 0,
onPartial: @escaping ACMGenericCallbacks.StreamCallback,
onProgress: ACMGenericCallbacks.ProgressCallback = nil,
onError: ACMGenericCallbacks.ErrorCallback = nil) {
onError _: ACMGenericCallbacks.ErrorCallback = nil)
{
self.onPartial = onPartial

session = endpoint.session(delegate: self)
Expand Down Expand Up @@ -40,9 +41,10 @@ public extension ACMNetworking {
func request<T: Decodable>(to endpoint: ACMBaseEndpoint,
currentRetryCount: Int? = 0,
onSuccess: ACMGenericCallbacks.ResponseCallback<T>,
onPartial: ACMGenericCallbacks.ResponseCallback<T> = nil,
onPartial _: ACMGenericCallbacks.ResponseCallback<T> = nil,
onProgress: ACMGenericCallbacks.ProgressCallback = nil,
onError: ACMGenericCallbacks.ErrorCallback = nil) {
onError: ACMGenericCallbacks.ErrorCallback = nil)
{
guard let urlRequest = generateURLRequest(endpoint: endpoint) else { return }

session = endpoint.session(delegate: self)
Expand All @@ -65,7 +67,7 @@ public extension ACMNetworking {

self.cancel()

self.handleResult(with: endpoint, data: data, onSuccess: onSuccess, onError: onError)
self.handleResult(with: endpoint, httpResponse: httpResponse, data: data, onSuccess: onSuccess, onError: onError)
}

taskProgress = requestTask?.progress.observe(\.fractionCompleted, changeHandler: { progress, _ in
Expand Down
4 changes: 2 additions & 2 deletions Sources/ACMNetworking/Library/Manager/ACMBaseEndpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,11 @@ public struct ACMBaseEndpoint {
}

init(config: ACMPlistModel? = nil, configOverride: Bool? = nil, host: String? = nil, scheme: ACMBaseScheme? = nil, path: String = "", queryItems: [URLQueryItem]? = nil, params: [String: Any?]? = nil, headers: NSMutableDictionary? = nil, method: ACMBaseMethod? = nil, authHeader: ACMAuthModel? = nil, mediaData: NSMutableData? = nil, retryCount: Int? = nil, isStream: Bool = false, downloadURL: String? = nil) {
self.plistUtils = ACMPlistUtils()
plistUtils = ACMPlistUtils()
if let config = config {
self.config = config
} else {
self.config = self.plistUtils?.config()
self.config = plistUtils?.config()
}
logger = ACMBaseLogger(config: self.config)
stringUtils = ACMStringUtils()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
// ACMBaseNetworkError.swift
//

struct ACMBaseNetworkError: Error {
public struct ACMBaseNetworkError: Error {
/// Message holds the error description
var message: String?
/// Log holds the localization description
var log: String?
/// Endpoint holds the current endpoint that calls
var endpoint: ACMBaseEndpoint?
/// Status code
var statusCode: Int?
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Foundation
/// ACMBodyEncoder
///
/// Encoder for body payload
final class ACMBodyEncoder {
enum ACMBodyEncoder {
/// Encode function
/// For creating body payload
/// - Parameters:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Foundation
/// ACMHeadersEncoder
///
/// Encoder for headers
final class ACMHeadersEncoder {
enum ACMHeadersEncoder {
/// Encode function
/// For creating headers
/// - Parameters:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Foundation
/// ACMQueryParamEncoder
///
/// Encoder for query parameters
final class ACMQueryParamEncoder {
enum ACMQueryParamEncoder {
/// Static function for encode
/// Creates queryitem list with given query model list
/// - Parameters:
Expand Down

0 comments on commit ff96ce2

Please sign in to comment.