iOS

[iOS] Alamofire๋กœ API ํ˜ธ์ถœํ•˜๊ธฐ (GET)

yevdev 2022. 10. 25. 10:19
๐Ÿ“Œ Alamofire ?
- ๋น„๋™๊ธฐ๋กœ ์ˆ˜ํ–‰ํ•˜๋Š” swift๊ธฐ๋ฐ˜์˜ HTTP ๋„คํŠธ์›Œํ‚น ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ (ํ†ต์‹  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ)

 

Alamofire ์„ค์น˜ ๋ฐ ์‚ฌ์šฉ ์ค€๋น„

1. ์ผ๋‹จ CocoaPod ์„ค์น˜

2. Alamofire ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜

pod 'Alamofire', '~> 5.2'

3. Import

import Alamofire

 

API GET

๊ฐ€์ง€๊ณ  ์˜ฌ ๋ฐ์ดํ„ฐ List

 

GET ๋ฉ”์„œ๋“œ ์ž‘์„ฑ ๋ฐฉ๋ฒ•

1. url : API ์ฃผ๊ณ 

2. method : ํ†ต์‹ ๋ฐฉ์‹

3. parameters : post ํ†ต์‹ ์‹œ ํ•„์š”

4. encoding : URL์ด๊ธฐ ๋•Œ๋ฌธ์—, URLEncoding

5. headers : json ํ˜•์‹์œผ๋กœ ๋ฐ›๊ฒŒ๋”

6. validate : ํ™•์ธ์ฝ”๋“œ

7. responseJSON : ๋ฐ์ดํ„ฐ ๋ฐ›๋Š” ๋ถ€๋ถ„ 

import Alamofire

func getData(_ completedHandler : @escaping (Data)->Void){
    let url = "https://vjsel.herokuapp.com/book/comments/1"
    AF.request(
        url,
        method: .get,
        parameters: nil,
        encoding: URLEncoding.default,
        headers: ["Content-Type":"application/json", "Accept":"application/json"]
    )
    .validate(statusCode: 200..<500)
    .responseJSON(completionHandler: { response in
        switch response.result{
        case .success:
            guard let result = response.data else {return}
            do {
                let decoder = JSONDecoder()
                let json = try decoder.decode(Data.self, from: result)

                completedHandler(json)
            } catch {
                print("error!\(error)")
            }
        default:
            return
        }
    })
}

 

 


 

 

.responseJSON , ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜ค๋Š” ๋ถ€๋ถ„์„ ๋” ์ž์„ธํžˆ ๋ด๋ณด์ž

.responseJSON(completionHandler: { response in
    switch response.result{
    case .success:
        guard let result = response.data else {return}
        do {
            let decoder = JSONDecoder()
            let json = try decoder.decode(Data.self, from: result)

            completedHandler(json)
        } catch {
            print("error!\(error)")
        }
    default:
        return
    }
})

- response.result๋กœ ์ฒ˜๋ฆฌ ์ƒํƒœ๋ฅผ ํ™•์ธ == .success ์ผ ๊ฒฝ์šฐ ์ •์ƒ์ฒ˜๋ฆฌ

 

 

๐Ÿ“Œ JSONDecoder() 

- JSON ํŒŒ์‹ฑ ํด๋ž˜์Šค

- ์ฒซ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ : ๊ตฌ์กฐ์ฒด ํƒ€์ž… → ํ•ด๋‹น ๊ตฌ์กฐ์ฒด์— ๋งž๊ฒŒ JSON ์„ ํŒŒ์‹ฑํ•ด์ค€๋‹ค.

- ๋‘๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ(from: ) : ๋„˜์–ด์˜จ ๋ฐ์ดํ„ฐ

- ์˜ˆ์ œ์˜ Data.self ํƒ€์ž…์œผ๋กœ ๋„˜๊ฒจ์ง„ ๊ตฌ์กฐ์ฒด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. (Decodable ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•œ ๊ตฌ์กฐ์ฒด์—ฌ์•ผ ํ• ๊ฒƒ)

struct Data: Decodable {
    var comments: [Comment]
}

struct Comment: Decodable, Hashable {
    let _id: String
    let content: String
    let userId: String
    let createdAt: String
    let updateAt: String
}

extension Comment {
    static var list = [
        Comment(_id: " ", content: "ใ…‡ใ…‡", userId: "ใ…‡ใ…‡", createdAt: "ใ…‡ใ…‡", updateAt: "ใ…‡ใ…‡")
    ]
}

๐Ÿšซ ํ•ด๋‹น ๊ตฌ์กฐ์ฒด์—๋Š” ๊ผญ ๋ฐ์ดํ„ฐ์˜ ์‹๋ณ„์ž(_ id)๊ฐ€ ๋“ค์–ด๊ฐ€์•ผ ๋ฐ์ดํ„ฐ๊ฐ€ ์ „๋ถ€ ์ž˜ ํŒŒ์‹ฑ๋œ๋‹ค.

 

 

๐Ÿ“Œ Completion Handler?

- ์–ด๋– ํ•œ ์ผ์ด ๋๋‚ฌ์„ ๋•Œ ํ–‰ํ•ด์•ผ ํ•  ์ผ

- ํ•ด๋‹น ์˜ˆ์ œ์—์„œ๋Š” ์„œ๋ฒ„์—์„œ ๊ฐ€์ ธ์˜จ ๋ฐ์ดํ„ฐ๋ฅผ json ํŒŒ์‹ฑํ•˜์—ฌ CompletionHandler์˜ ์ธ์ž๋กœ ๋„ฃ์–ด์ฃผ๊ณ  ์žˆ๋‹ค.

- ๋„คํŠธ์›Œํฌ์—์„œ๋Š” ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ return์œผ๋กœ ๋ฐ˜ํ™˜ํ•ด์ฃผ์ง€ ๋ชปํ•ด์„œ ์ด๋Ÿฐ ๋ฐฉ์‹์œผ๋กœ ๋งŽ์ด ์‚ฌ์šฉํ•จ!