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

Feature integracao api #197

Open
wants to merge 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
9826D90E2788B8A900E05400 /* DetailView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9826D90D2788B8A900E05400 /* DetailView.swift */; };
9826D9102788B8EA00E05400 /* SettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9826D90F2788B8EA00E05400 /* SettingsView.swift */; };
9826D9132788BAC200E05400 /* DebugViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9826D9122788BAC200E05400 /* DebugViewController.swift */; };
C11BB0F529E46288002E99B5 /* NetworManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = C11BB0F429E46288002E99B5 /* NetworManager.swift */; };
C18492CA29DCB15400043B88 /* RepositoryCellView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C18492C929DCB15400043B88 /* RepositoryCellView.swift */; };
C19CC14529DE1FD100E604F1 /* RepositoryCellViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C19CC14429DE1FD100E604F1 /* RepositoryCellViewTests.swift */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -83,6 +84,7 @@
9826D90F2788B8EA00E05400 /* SettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsView.swift; sourceTree = "<group>"; };
9826D9122788BAC200E05400 /* DebugViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DebugViewController.swift; sourceTree = "<group>"; };
B22BF462A559B5AA593DB184 /* Pods-GitHubApp-GitHubAppUITests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-GitHubApp-GitHubAppUITests.debug.xcconfig"; path = "Target Support Files/Pods-GitHubApp-GitHubAppUITests/Pods-GitHubApp-GitHubAppUITests.debug.xcconfig"; sourceTree = "<group>"; };
C11BB0F429E46288002E99B5 /* NetworManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworManager.swift; sourceTree = "<group>"; };
C18492C929DCB15400043B88 /* RepositoryCellView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RepositoryCellView.swift; sourceTree = "<group>"; };
C19CC14429DE1FD100E604F1 /* RepositoryCellViewTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RepositoryCellViewTests.swift; sourceTree = "<group>"; };
D3A41DBB4385986B06ADD4B6 /* Pods-GitHubApp.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-GitHubApp.debug.xcconfig"; path = "Target Support Files/Pods-GitHubApp/Pods-GitHubApp.debug.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -214,6 +216,7 @@
isa = PBXGroup;
children = (
9826D8EA2788B43E00E05400 /* Service.swift */,
C11BB0F429E46288002E99B5 /* NetworManager.swift */,
);
path = Service;
sourceTree = "<group>";
Expand Down Expand Up @@ -536,6 +539,7 @@
9826D9052788B43F00E05400 /* ListView.swift in Sources */,
9826D9032788B43F00E05400 /* DetailViewController.swift in Sources */,
C18492CA29DCB15400043B88 /* RepositoryCellView.swift in Sources */,
C11BB0F529E46288002E99B5 /* NetworManager.swift in Sources */,
9826D9022788B43F00E05400 /* SettingsViewController.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@

import Foundation

struct Repository {
struct RepositoryModel: Decodable {
let id: Int
let name: String
let repositoryUrl: String

enum CodingKeys: String, CodingKey {
case id, name
case repositoryUrl = "html_url"
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,10 @@ final class ListViewController: UIViewController {
}

override func viewDidAppear(_ animated: Bool) {

service.fetchList { repositories in

DispatchQueue.main.async {

self.listView.updateView(with: repositories)
}
}


service.getRepositories(for: "devpass-tech", completion: { _ in

} )
}

override func loadView() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//
// NetworManager.swift
// GitHubApp
//
// Created by GISELE TOLEDO on 10/04/23.
//

import Foundation

protocol NetworkManagerProtocol {
func get<T: Decodable>(request: URLRequest, completion: @escaping (Result<T, Error>) -> Void)
}

final class NetworkManager: NetworkManagerProtocol {

private let session: URLSession

init(session: URLSession = .shared) {
self.session = session
}

func get<T: Decodable>(request: URLRequest, completion: @escaping (Result<T, Error>) -> Void) {
let task = session.dataTask(with: request) { data, response, error in
if let error = error {
completion(.failure(error))
return
}

guard let data = data, let httpResponse = response as? HTTPURLResponse else {
completion(.failure(NSError(domain: "Invalid response", code: 0, userInfo: nil)))
return
}

guard (200...299).contains(httpResponse.statusCode) else {
let statusCode = httpResponse.statusCode
let localizedDescription = HTTPURLResponse.localizedString(forStatusCode: statusCode)
completion(.failure(NSError(domain: "HTTP Error", code: statusCode, userInfo: [NSLocalizedDescriptionKey: localizedDescription])))
return
}

do {
let decodedData = try JSONDecoder().decode(T.self, from: data)
completion(.success(decodedData))
} catch {
completion(.failure(error))
}
}

task.resume()
}
}
35 changes: 31 additions & 4 deletions solutions/devsprint-matheus-reis-8/GitHubApp/Service/Service.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,36 @@
import Foundation

struct Service {

func fetchList(_ completion: ([String]) -> Void) {

completion(["Repository 1", "Repository 2", "Repository 3"])

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

}