-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Feat/#54 connect network
- Loading branch information
Showing
9 changed files
with
409 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,90 @@ | ||
// | ||
// NetworkManager.swift | ||
// saftyReport | ||
// | ||
// Created by 이지훈 on 11/18/24. | ||
// | ||
|
||
import Foundation | ||
import Alamofire | ||
|
||
class NetworkManager { | ||
|
||
func photoAPI(compleation: @escaping (Result<[GalleryPhotoList], NetworkError>) -> Void) { | ||
guard let baseURL = Bundle.main.object(forInfoDictionaryKey: "BASE_URL") as? String else { | ||
print("[Error] BASE_URL is missing in Info.plist") | ||
compleation(.failure(.unknownError)) | ||
return | ||
} | ||
|
||
let url = "\(baseURL)/api/v1/report/photo" | ||
|
||
let headers: HTTPHeaders = ["userId": "1"] | ||
|
||
AF.request(url, method: .get, headers: headers) | ||
.validate() | ||
.response { [weak self] response in | ||
guard let statusCode = response.response?.statusCode, | ||
let data = response.data, | ||
let self = self | ||
else { | ||
print("[Error] Response or Data is nil") | ||
compleation(.failure(.unknownError)) | ||
return | ||
} | ||
|
||
|
||
switch response.result { | ||
case .success: | ||
let photoList = self.decodePhoto(data: data) | ||
compleation(.success(photoList)) | ||
case .failure(let error): | ||
print("[Error] Request Failed: \(error.localizedDescription)") | ||
let error = self.handleStatusCode(statusCode, data: data) | ||
compleation(.failure(error)) | ||
} | ||
} | ||
} | ||
|
||
func decodePhoto(data: Data) -> [GalleryPhotoList] { | ||
do { | ||
let response = try JSONDecoder().decode(GalleryResponse.self, from: data) | ||
return response.data?.photoList ?? [] | ||
} catch { | ||
print("[Decode Error] Failed to Decode GalleryResponse: \(error)") | ||
return [] | ||
} | ||
} | ||
} | ||
|
||
extension NetworkManager { | ||
func decodeError(data: Data) -> String { | ||
do { | ||
let failResponse = try JSONDecoder().decode(FailResponse.self, from: data) | ||
print("[Decode Error Response] Status: \(failResponse.status?.description ?? "Unknown")") | ||
return failResponse.status?.description ?? "" | ||
} catch { | ||
print("[Decode Error] Failed to Decode Error Response: \(error)") | ||
return "" | ||
} | ||
} | ||
|
||
func handleStatusCode(_ statusCode: Int, data: Data) -> NetworkError { | ||
let errorCode = decodeError(data: data) | ||
print("[Handle Status Code] Status Code: \(statusCode), Error Code: \(errorCode)") | ||
|
||
switch (statusCode, errorCode) { | ||
case (400, "00"): | ||
print("[Error] Invalid Request (400, 00)") | ||
return .invalidRequest | ||
case (400, "01"): | ||
print("[Error] Expression Error (400, 01)") | ||
return .expressionError | ||
case (404, ""): | ||
print("[Error] Invalid URL (404)") | ||
return .invalidURL | ||
case (409, "00"): | ||
print("[Error] Duplicate Error (409, 00)") | ||
return .duplicateError | ||
case (500, ""): | ||
print("[Error] Server Error (500)") | ||
return .serverError | ||
default: | ||
print("[Error] Unknown Error") | ||
return .unknownError | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
saftyReport/saftyReport/Resource/Extension/UIImageView+.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// | ||
// UIImageView+.swift | ||
// saftyReport | ||
// | ||
// Created by OneTen on 11/28/24. | ||
// | ||
|
||
import UIKit | ||
|
||
extension UIImageView { | ||
func load(url: URL) { | ||
DispatchQueue.global().async { [weak self] in | ||
if let data = try? Data(contentsOf: url) { | ||
if let image = UIImage(data: data) { | ||
DispatchQueue.main.async { | ||
self?.image = image | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.