Add examples project

This commit is contained in:
Ivan Vorobei
2019-06-06 11:16:28 +03:00
parent 86fe2d7323
commit b866885a44
962 changed files with 45026 additions and 5 deletions

View File

@@ -0,0 +1,13 @@
//
// ItemResponse.swift
// GitHubSearchWithSwiftUI
//
// Created by marty-suzuki on 2019/06/06.
// Copyright © 2019 jp.marty-suzuki. All rights reserved.
//
import Foundation
struct ItemResponse<T: Decodable>: Decodable {
let items: [T]
}

View File

@@ -0,0 +1,17 @@
//
// Repository.swift
// GitHubSearchWithSwiftUI
//
// Created by marty-suzuki on 2019/06/05.
// Copyright © 2019 jp.marty-suzuki. All rights reserved.
//
import Foundation
struct Repository: Decodable {
let id: Int
let fullName: String
let description: String?
let stargazersCount: Int
let htmlUrl: URL
}

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()
}
}