Skip to content

Commit

Permalink
[Add] API Request 코드추가 #13
Browse files Browse the repository at this point in the history
  • Loading branch information
alpha-kwhn committed May 11, 2023
1 parent b7e6f9a commit 40bb477
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 28 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,49 @@ final class MovieService {
private init() {}

//75df104409445c36a314b12eb0aa5fdb
//ko

func getInfo(key: String, completion: @escaping (NetworkState<Any>) -> Void) {

let header: HTTPHeader = [
func getInfo(key: String, language: String, page: Int, completion: @escaping (NetworkState<Any>) -> Void) {

let header: HTTPHeaders = [
"Content-Type": "application/json",
"Authorization": "Bearer 75df104409445c36a314b12eb0aa5fdb"
]

let url = MovieConfig.movieURL + "?language=\(language)&page=\(page)"

let dataRequest = AF.request(url, method: .get, headers: header)

dataRequest.responseData { response in
switch response.result {
case .success:
guard let statusCode = response.response?.statusCode else { return }
guard let value = response.value else { return }

let networkResult = self.judgeStatus(by: statusCode, value)
completion(networkResult)
case .failure:
completion(.networkErr)
}
}
}

private func judgeStatus(by statusCode: Int, _ data: Data) -> NetworkState<Any> {
switch statusCode {
case 200: return isValidData(data: data)
case 201: return isValidData(data: data)
case 400, 409: return isValidData(data: data)
case 500: return .serverErr
default: return .networkErr
}
}


private func isValidData(data: Data) -> NetworkState<Any> {
let decoder = JSONDecoder()

guard let decodedData = try? decoder.decode(Weathers.self, from: data)
else { return .pathErr }

return .success(decodedData as Any)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,6 @@
// Created by 김응관 on 2023/05/06.
//

//import Foundation
//let headers = [
// "accept": "application/json",
// "Authorization": "Bearer eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiI3NWRmMTA0NDA5NDQ1YzM2YTMxNGIxMmViMGFhNWZkYiIsInN1YiI6IjY0NWQxNDg2M2ZlMTYwMDE3MjI2N2FhMSIsInNjb3BlcyI6WyJhcGlfcmVhZCJdLCJ2ZXJzaW9uIjoxfQ.VAMyXI3CfXx3hI3LNDWVJDxMt7a-uy1Pq2ay6NTJ18M"
//]
//
//let request = NSMutableURLRequest(url: NSURL(string: "https://api.themoviedb.org/3/discover/movie?include_adult=false&include_video=false&language=en-US&page=1&sort_by=popularity.desc")! as URL,
// cachePolicy: .useProtocolCachePolicy,
// timeoutInterval: 10.0)
//request.httpMethod = "GET"
//request.allHTTPHeaderFields = headers
//
//let session = URLSession.shared
//let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
// if (error != nil) {
// print(error as Any)
// } else {
// let httpResponse = response as? HTTPURLResponse
// print(httpResponse)
// }
//})
//
//dataTask.resume()

import Foundation
import Alamofire

Expand Down

0 comments on commit 40bb477

Please sign in to comment.