This commit is contained in:
Ivan Vorobei
2019-06-26 22:12:35 +03:00
parent 2477f5d038
commit 48001a8e9a
1363 changed files with 6 additions and 3 deletions

View File

@@ -0,0 +1,36 @@
//
// RepositoryModel.swift
// GitHubSearchWithSwiftUI
//
// Created by marty-suzuki on 2019/06/06.
// Copyright © 2019 jp.marty-suzuki. All rights reserved.
//
import Combine
import Foundation
enum RepositoryAPI {
static func search(query: String) -> AnyPublisher<[Repository], Error> {
guard var components = URLComponents(string: "https://api.github.com/search/repositories") else {
return Publishers.Empty<[Repository], Error>().eraseToAnyPublisher()
}
components.queryItems = [URLQueryItem(name: "q", value: query)]
guard let url = components.url else {
return Publishers.Empty<[Repository], Error>().eraseToAnyPublisher()
}
var request = URLRequest(url: url)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
return URLSession.shared.combine.send(request: request)
.decode(type: ItemResponse<Repository>.self, decoder: decoder)
.map { $0.items }
.handleEvents(receiveOutput: { print($0) },
receiveCompletion: { print($0)})
.eraseToAnyPublisher()
}
}