forked from yagom-academy/ios-diary
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
66 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// | ||
// NetworkManager.swift | ||
// Diary | ||
// | ||
// Created by Max, Hemg on 2023/09/13. | ||
// | ||
|
||
import CoreLocation | ||
|
||
final class NetworkManager { | ||
private var dataTask: URLSessionDataTask? | ||
|
||
func fetchData(url: String, completionHandler: @escaping(Result<Data, APIError>) -> Void) { | ||
guard let url = URL(string: url) else { | ||
completionHandler(.failure(.invalidURL)) | ||
return | ||
} | ||
|
||
dataTask = URLSession.shared.dataTask(with: url) { data, response, error in | ||
if error != nil { | ||
completionHandler(.failure(.requestFail)) | ||
} | ||
|
||
guard let httpResponse = response as? HTTPURLResponse, (200...299) ~= httpResponse.statusCode else { | ||
completionHandler(.failure(.invalidData)) | ||
return | ||
} | ||
|
||
guard let data else { | ||
completionHandler(.failure(.invalidData)) | ||
return | ||
} | ||
|
||
completionHandler(.success(data)) | ||
} | ||
self.dataTask?.resume() | ||
} | ||
|
||
func fetchLocation(location: CLLocation, _ completionHandler: @escaping(Result<WeatherResult, APIError>) -> Void) { | ||
let urlStr = "https://api.openweathermap.org/data/2.5/weather?lat=\(location.coordinate.latitude)&lon=\(location.coordinate.longitude)&appid=aaa5700cbd82d1a09e738731002f97be" | ||
|
||
fetchData(url: urlStr) { result in | ||
switch result { | ||
case .success(let data): | ||
do { | ||
let decodingData: WeatherResult = try DecodingManager.decodeData(from: data) | ||
completionHandler(.success(decodingData)) | ||
} catch { | ||
completionHandler(.failure(APIError.decodingFail)) | ||
} | ||
case .failure(let error): | ||
completionHandler(.failure(error)) | ||
} | ||
} | ||
} | ||
} |