-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathService.swift
More file actions
43 lines (33 loc) · 1.32 KB
/
Service.swift
File metadata and controls
43 lines (33 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//
// Service.swift
// GitHubApp
//
// Created by Rodrigo Borges on 30/09/21.
//
import Foundation
struct Service {
private let networkManager: NetworkManagerProtocol
init(networkManager: NetworkManagerProtocol = NetworkManager()) {
self.networkManager = networkManager
}
func getRepositories(for user: String, completion: @escaping (Result<[String], Error>) -> Void) {
let urlString = "https://api.github.com/users/devpass-tech/repos"
guard let url = URL(string: urlString) else {
completion(.failure(NSError(domain: "Invalid URL", code: 0, userInfo: nil)))
return
}
let request = URLRequest(url: url)
networkManager.get(request: request) { (result: Result<[RepositoryModel], Error>) in
switch result {
case .success(let repositories):
let repositoryNames = repositories.prefix(10).map { $0.name }
let repositoryUrls = repositories.prefix(10).map { $0.repositoryUrl }
print("Repository names: \(repositoryNames)")
print("Repository URLs: \(repositoryUrls)")
completion(.success(repositoryNames))
case .failure(let error):
completion(.failure(error))
}
}
}
}