์นดํ…Œ๊ณ ๋ฆฌ ์—†์Œ

Alamofire

_cactus 2022. 1. 5. 16:37
๋ฐ˜์‘ํ˜•

Alamofire

: ๋น„๋™๊ธฐ๋กœ ์ˆ˜ํ–‰ํ•˜๋Š” swift๊ธฐ๋ฐ˜์˜ HTTP ๋„คํŠธ์›Œํ‚น ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ (ํ†ต์‹  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ)

๐Ÿ’ก
์™œ ํ•„์š”ํ•œ๊ฐ€ ? - ์„œ๋ฒ„์™€ ํ†ต์‹ ์„ ํ•˜๊ธฐ ์œ„ํ•ด ! ๋” ๊ตฌ์ฒด์ ์œผ๋ก , ์„œ๋ฒ„์—์„œ jsonํ˜•์‹์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜ค๋ ค๋ฉด HTTP ํ†ต์‹ ์„ ํ•ด์•ผํ•˜๋ฉฐ HTTP ํ†ต์‹  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์ตœ๊ฐ•์ž Alamofire๋ฅผ ์ด์šฉํ•ด๋ณด์ž !

  • Apple์˜ Foundation ํ”„๋ ˆ์ž„์›Œํฌ์—์„œ ์ œ๊ณตํ•˜๋Š” URL ๋กœ๋”ฉ ์‹œ์Šคํ…œ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ตฌ์ถ•๋˜์–ด์žˆ์Œ URLSession ๋ฐ URLSessionTask ๊ฐ™์€ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์‰ฝ๊ฒŒ ๊ตฌํ˜„๋˜์–ด์žˆ์Œ
  • Alamofire๋Š” CocoaPods๋ฅผ ์‚ฌ์šฉํ•ด ์‰ฝ๊ฒŒ ์„ค์น˜ ๊ฐ€๋Šฅ

์‚ฌ์šฉ๋ฒ•

Alamofire๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„  ์ œ์ผ ๋จผ์ € import Alamofire ํ•ด์คŒ

โœฑ ์ฐธ๊ณ  ) Alamofire ver5 ์ดํ›„๋ถ€ํ„ฐ๋Š” AF ๊ฐ€ Session.default ์— ๋Œ€ํ•œ ์ฐธ์กฐ..!!

1. Request

์ •์˜

// 1 open func request<Parameters: Encodable>(_ convertible: URLConvertible,                                          method: HTTPMethod = .get,                                          parameters: Parameters? = nil,                                          encoder: ParameterEncoder = URLEncodedFormParameterEncoder.default,                                          headers: HTTPHeaders? = nil,                                          interceptor: RequestInterceptor? = nil) -> DataRequest // 2 open func request(_ urlRequest: URLRequestConvertible,                    interceptor: RequestInterceptor? = nil) -> DataRequest

์‚ฌ์šฉ

// 1 AF.request(URL, method, parameters, encoder, headers, interceptor) // 2 AF.request(URL, interceptor)

1๋ฒˆ๊ณผ 2๋ฒˆ ๋ฐฉ์‹์œผ๋กœ request๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Œ 1๋ฒˆ : DataRequest๋ฅผ ์ƒ์„ฑํ•˜๋ฉด์„œ method๋‚˜ parameter๊ฐ™์€ ๊ฐœ๋ณ„ ๊ตฌ์„ฑ ์š”์ฒญ๋„ ํ—ˆ์šฉ ์š”์ฒญ๋งˆ๋‹ค Encodable์„ ๋งŒ์กฑํ•˜๋Š” parameter ํ—ˆ์šฉ 2๋ฒˆ : Alamofire์˜ ๋ชจ๋“  URLRequestConvertible ํ”„๋กœํ† ์ฝœ์„ ์ค€์ˆ˜ํ•˜๋Š” ๋ชจ๋“  ์œ ํ˜•์— ๋Œ€ํ•ด DataRequest๋ฅผ ์ƒ์„ฑ

HTTP Method

Alamofire์—์„œ๋Š” HTTP Method ๋˜ํ•œ ์ง€์› ๋Œ€ํ‘œ์ ์œผ๋กœ getpost,put,delete ๋“ฑ

AF.request(url, method: .post) AF.request(url, method: .get) ...

Parameters

Headers

2. Response

์•ž์„œ ๋ณด๋‚ธ Request ์— ๋”ฐ๋ฅธ response

์‚ฌ์šฉ๋ฒ•

Alamofire์—์„œ๋Š” requese ๋’ค์— response๋ฅผ ๋ถ™์—ฌ์ฃผ๋ฉด ๋จ

AF.request(url).response()

์˜ˆ์‹œ

์œ„ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฐ’์ด ์ถœ๋ ฅ๋จ

โ‡’  www.naver.com ์„œ๋ฒ„์— requestํ•˜๋ฉด ๊ทธ์— ๋Œ€ํ•œ response๋กœ success๊ฐ€ ๋„˜์–ด์˜จ๋‹ค

5๊ฐ€์ง€ response handler

(โœฑ์ฐธ๊ณ  : response handler๋Š” ์„œ๋ฒ„์—์„œ ๋ฐ˜ํ™˜๋˜๋Š” ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋ฅผ ํ•˜์ง€ ์•Š๋Š”๋‹ค ์ด๋Š” Response Validation์—์„œ ๋‹ค๋ฃฐ ์˜ˆ์ •. )

  1. response Handler URLSessionDelegate์—์„œ ์ง์ ‘ ๋ชจ๋“  ์ •๋ณด๋ฅผ ์ „๋‹ฌํ•˜๊ณ  ์‘๋‹ต ๋ฐ์ดํ„ฐ๋ฅผ ํ‰๊ฐ€ํ•˜์ง€ ์•Š์Œ
    URLSessionDelegate : URLSession ์ธ์Šคํ„ด์Šค๊ฐ€ ์„ธ์…˜ ์ˆ˜์ค€์˜ ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด Delegate์—์„œ ํ˜ธ์ถœํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•˜๋Š” ํ”„๋กœํ† ์ฝœ

    ์•„๋ž˜์™€ ๊ฐ™์ด ์‚ฌ์šฉ

     AF.request(url).response { response in     print(response)  }

  1. responseData Handler DataResponseSerializer๋ฅผ ์‚ฌ์šฉํ•ด ์„œ๋ฒ„์—์„œ ๋ฐ˜ํ™˜๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœํ•˜๊ณ  ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋ฅผ ์ง„ํ–‰
    - DataResponseSerializer : ์ตœ์†Œ ์‘๋‹ต ๊ฒ€์‚ฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ  ๋ชจ๋“  ์‘๋‹ต ๋ฐ์ดํ„ฐ๋ฅผ ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ResponseSerializer - ResponseSerializer : ์‘๋‹ต์„ ๋ชจ๋‘ ์ฒ˜๋ฆฌ ํ•  ์ˆ˜์žˆ๋Š” ์ง๋ ฌ ๋ณ€ํ™˜๊ธฐ
    AF.request(url).responseData { response in     print(response) }

  1. responseString Handler StringResponseSerializer๋ฅผ ์‚ฌ์šฉํ•ด ์„œ๋ฒ„์—์„œ ๋ฐ˜ํ™˜๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ง€์ •๋œ ์ธ์ฝ”๋”ฉ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ ๋งŒ์•ฝ, ์ธ์ฝ”๋”ฉ์ด ์ง€์ •๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ Alamofire๋Š” ์„œ๋ฒ„์˜ HTTPURLResponse์— ์ง€์ •๋œ ํ…์ŠคํŠธ ์ธ์ฝ”๋”ฉ์„ ์‚ฌ์šฉ
    StringResponseSerializer : ์‘๋‹ต ๋ฐ์ดํ„ฐ๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋””์ฝ”๋”ฉํ•˜๋Š” ResponseSerializer.
    AF.request(url).resonseString { response in 	print(response) }

  1. responseJSON Handler ์•„๋งˆ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ๋ฐฉ๋ฒ•..! JSONResponseSerializer๋ฅผ ์‚ฌ์šฉํ•ด ์„œ๋ฒ„์—์„œ ๋ฐ˜ํ™˜๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ง€์ •๋œ JSONSerialization.ReadingOptions๋ฅผ ์‚ฌ์šฉํ•˜๋Š” Anyํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜
    - JSONResponseSerializer : JSONSerialization์„ ์‚ฌ์šฉํ•˜์—ฌ ์‘๋‹ต ๋ฐ์ดํ„ฐ๋ฅผ ๋””์ฝ”๋”ฉํ•˜๋Š” ResponseSerializer - JSONSerialization.ReadingOptions : JSON ๋ฐ์ดํ„ฐ์—์„œ Foundation ๊ฐœ์ฒด๋ฅผ ๋งŒ๋“ค ๋•Œ ์‚ฌ์šฉ๋˜๋Š” ์˜ต์…˜

  1. responseDecodable Handler DecodableResponseSerializer๋ฅผ ์‚ฌ์šฉํ•ด ์„œ๋ฒ„์—์„œ ๋ฐ˜ํ™˜๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ง€์ •๋œ DataDecoder์„ ์‚ฌ์šฉํ•˜๋Š” Decodableํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜
    - DecodableResponseSerializer : DataDecoder๋ฅผ ๋”ฐ๋ฅด๋Š” ๋ชจ๋“  ํƒ€์ž…์„ ์‚ฌ์šฉํ•ด ์‘๋‹ต ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๋„ค๋ฆญ ๊ฐ’์œผ๋กœ ๋””์ฝ”๋”ฉํ•˜๋Š” ResponseSerializer - DataDecoder : ๋ฐ์ดํ„ฐ๋ฅผ Decodableํƒ€์ž…์œผ๋กœ ๋””์ฝ”๋”ฉ ํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ํƒ€์ž…

3. Validation

: ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ ์š”์ฒญ์— ๋Œ€ํ•œ response๋ฅผ ํ•˜๊ธฐ ์ „์— .validate() ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์œ ํšจํ•˜์ง€ ์•Š์€ ์ƒํƒœ ์ฝ”๋“œ๋‚˜ MIME ํƒ€์ž…์ด ์žˆ๋Š” ๊ฒฝ์šฐ responseํ•˜์ง€ ์•Š๋„๋ก ํ•จ

์‚ฌ์šฉ๋ฒ•

AF.request(url)   .validate()   .response { response in      print(response)

+ ์ƒํƒœ ์ฝ”๋“œ๋‚˜ MIME ํƒ€์ž… ์กฐ๊ฑด์„ ๋„ฃ๋Š” ๋ฐฉ๋ฒ• ๐Ÿ‘‡

AF.request(url)   .validate(200..<300) // 200~300 ์‚ฌ์ด ์ƒํƒœ์ฝ”๋“œ๋งŒ ํ—ˆ์šฉ   .validate(contentType:["application/json"]) // JSON ํฌ๋งท๋งŒ ํ—ˆ์šฉ   .response { response in       print(response) }

๐Ÿ”Ž MIME ํƒ€์ž… ์ „์ฒด๋ชฉ๋ก

728x90
๋ฐ˜์‘ํ˜•