Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APIClientの修正 #32

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions Colombia/Sources/APIClient/APIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,28 @@ struct APIClient {
let url = requestable.url
let request = URLRequest(url: url)
let task = URLSession.shared.dataTask(with: request) { resultData, response, error in
guard let resultData = resultData else {
if let error = error {
observer.onError(error)
}
if let error = error {
observer.onError(APIError.unknown(error))
}
guard let resultData = resultData, response != nil else {
observer.onError(APIError.response)
return
}
do {
let decodeData = try decoder.decode(T.Response.self, from: resultData)
observer.onNext(decodeData)
} catch let error {
observer.onError(error)
observer.onError(APIError.decode(error))
}
}
task.resume()
return Disposables.create()
}
}
}

enum APIError: Error {
case decode(Error)
case response
case unknown(Error)
}
6 changes: 3 additions & 3 deletions Colombia/Sources/APIClient/AnnictAPIRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ enum AccessToken {
}

enum Endpoint {
//とりあえずworksだけ実装
// TODO: とりあえずworksだけ実装
case works

func endpoint() -> String {
Expand All @@ -45,9 +45,9 @@ struct AnnictAPIRequest:Requestable {
switch endpoint {
case .works:
baseURL.queryItems = [
//1ページあたりのデータの数、とりあえず20を指定
// TODO: 1ページあたりのデータの数、とりあえず20を指定
URLQueryItem(name: "per_page", value: "20"),
//とりあえずid,title,recommended_urlのみを受け取る
// TODO: とりあえずid,title,recommended_urlのみを受け取る
URLQueryItem(name: "fields", value: "id,title,images"),
URLQueryItem(name: "access_token", value: AccessToken.annictAPI)
]
Expand Down