Skip to content

Commit

Permalink
feat/#64 network구문 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
HEHEEUN committed Nov 28, 2024
1 parent 558dbb6 commit 0dd0ae7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ struct HomeDataObject: Codable {
// MARK: - BannerList

struct BannerList: Codable {
let bannerID: Int?
let bannerURL: String?
let bannerId: Int?
let bannerUrl: String?
}
42 changes: 42 additions & 0 deletions saftyReport/saftyReport/Network/NetworkManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,48 @@ import Alamofire

class NetworkManager {

func homeAPI(compleation: @escaping (Result<HomeDataObject, NetworkError>) -> Void) {
let url = "\(Environment.baseURL)/api/v1/home"

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:
if let homeDataObject = self.decodeHome(data: data) {
compleation(.success(homeDataObject))
} else {
compleation(.failure(.decodingError))
}
case .failure(let error):
print("[Error] Request Failed: \(error.localizedDescription)")
let error = self.handleStatusCode(statusCode, data: data)
compleation(.failure(error))
}
}
}

func decodeHome(data: Data) -> HomeDataObject? {
do {
let response = try JSONDecoder().decode(HomeResponse.self, from: data)
return response.data
} catch {
print("[Decode Error] Failed to Decode HomeResponse: \(error)")
return nil
}
}

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")
Expand Down

This file was deleted.

0 comments on commit 0dd0ae7

Please sign in to comment.