diff --git a/Package.swift b/Package.swift index 01fdebc..fac8f18 100644 --- a/Package.swift +++ b/Package.swift @@ -5,7 +5,7 @@ import PackageDescription let package = Package( name: "ToolboxAPIClient", - platforms: [.iOS(.v13)], + platforms: [.iOS(.v13), .macOS(.v10_15), .watchOS(.v5)], products: [ // Products define the executables and libraries a package produces, and make them visible to other packages. .library( diff --git a/Sources/ToolboxAPIClient/BaseNetworkWorker.swift b/Sources/ToolboxAPIClient/BaseNetworkWorker.swift index a93f20e..efb09c9 100644 --- a/Sources/ToolboxAPIClient/BaseNetworkWorker.swift +++ b/Sources/ToolboxAPIClient/BaseNetworkWorker.swift @@ -79,21 +79,4 @@ public class BaseNetworkWorker where T: Codable { .decode(type: T?.self, decoder: JSONDecoder()) .eraseToAnyPublisher() } - - // internal func urlRequestNoMap(httpverb: String, httpBody: Data?, url: URL, headers: Headers) -> AnyPublisher { - // var urlRequest = URLRequest(url: url) - // urlRequest.httpMethod = httpverb - // urlRequest.httpBody = httpBody - // - // headers.forEach { (key, value) in - // if let value = value as? String { - // urlRequest.setValue(value, forHTTPHeaderField: key) - // } - // } - // - // return URLSession.shared.dataTaskPublisher(for: urlRequest) - // .compactMap { $0.response as? HTTPURLResponse } - // .mapError { $0 as Error } - // .eraseToAnyPublisher() - // } } diff --git a/Tests/ToolboxAPIClientTests/Network/Professions/Manager/ProfessionsManagerAPI.swift b/Tests/ToolboxAPIClientTests/Network/Professions/Manager/ProfessionsManagerAPI.swift deleted file mode 100644 index 683e460..0000000 --- a/Tests/ToolboxAPIClientTests/Network/Professions/Manager/ProfessionsManagerAPI.swift +++ /dev/null @@ -1,29 +0,0 @@ -// -// File.swift -// -// -// Created by Zé on 21/03/21. -// - -import Foundation -import Combine -// -import ToolboxAPIClient - -public class ProfessionsManagerAPI: ProfessionRequestable { - public func getProfessions() -> AnyPublisher<[ProfessionDto]?, Error> { - BaseNetworkWorker<[ProfessionDto]>(target: ProfessionTarget.getProfessions).urlRequest() - } - - public func getProfessions(limit: Int) -> AnyPublisher<[ProfessionDto]?, Error> { - BaseNetworkWorker<[ProfessionDto]>(target: ProfessionTarget.getProfessionsWith(limit)).urlRequest() - } - - public func getProfession(professionId: String) -> AnyPublisher { - BaseNetworkWorker(target: ProfessionTarget.getProfessionBy(professionId)).urlRequest() - } - - public func createProfession(_ model: ProfessionDto) -> AnyPublisher { - BaseNetworkWorker(target: ProfessionTarget.postProfession).urlRequest(contentBody: model) - } -} diff --git a/Tests/ToolboxAPIClientTests/Network/Professions/Targets/ProfessionTarget.swift b/Tests/ToolboxAPIClientTests/Network/Professions/Targets/ProfessionTarget.swift deleted file mode 100644 index 2bbca0e..0000000 --- a/Tests/ToolboxAPIClientTests/Network/Professions/Targets/ProfessionTarget.swift +++ /dev/null @@ -1,64 +0,0 @@ -// -// File.swift -// -// -// Created by Zé on 20/03/21. -// - -import Foundation -import Combine -import ToolboxAPIClient - -public protocol ProfessionRequestable { - func getProfessions() -> AnyPublisher<[ProfessionDto]?, Error> - func getProfessions(limit: Int) -> AnyPublisher<[ProfessionDto]?, Error> - func getProfession(professionId: String) -> AnyPublisher - func createProfession(_ model: ProfessionDto) -> AnyPublisher -} - -public enum ProfessionTarget { - case getProfessions - case getProfessionsWith(_ limt: Int) - case getProfessionBy(_ professionId: String) - case postProfession -} - -extension ProfessionTarget: TargetType { - public var queryString: [URLQueryItem] { - switch self { - case .getProfessionsWith(let limit): - return [URLQueryItem(name: "limit", value: "\(limit)")] - default: - return [] - } - } - - public var baseURL: URL { - URL(string: "https://run.mocky.io/v3/")! - } - - public var path: String { - switch self { - case .getProfessions, - .getProfessionsWith: - return "8ae911b5-f8b5-4b67-a341-e85fbc0e635a" - case .postProfession: - return "1131699f-4cfe-4efc-b196-bba55d3e0c01" - case .getProfessionBy(let professionId): - return "8ae911b5-f8b5-4b67-a341-e85fbc0e635a/\(professionId)" - } - } - - public var method: HttpMethodEnum { - switch self { - case .getProfessions, .getProfessionBy, .getProfessionsWith: - return .get - case .postProfession: - return .post - } - } - - public var headers: [String : String]? { - nil - } -} diff --git a/Tests/ToolboxAPIClientTests/Network/Story/Manager/StoryManagerAPI.swift b/Tests/ToolboxAPIClientTests/Network/Story/Manager/StoryManagerAPI.swift new file mode 100644 index 0000000..851ca06 --- /dev/null +++ b/Tests/ToolboxAPIClientTests/Network/Story/Manager/StoryManagerAPI.swift @@ -0,0 +1,29 @@ +// +// File.swift +// +// +// Created by Zé on 21/03/21. +// + +import Foundation +import Combine +// +import ToolboxAPIClient + +public class StoryManagerAPI: StoryRequestable { + public func getStorys() -> AnyPublisher<[StoryDto]?, Error> { + BaseNetworkWorker<[StoryDto]>(target: StoryTarget.getStorys).urlRequest() + } + + public func getStorys(limit: Int) -> AnyPublisher<[StoryDto]?, Error> { + BaseNetworkWorker<[StoryDto]>(target: StoryTarget.getStorysWith(limit)).urlRequest() + } + + public func getStory(storyId: String) -> AnyPublisher { + BaseNetworkWorker(target: StoryTarget.getStoryBy(storyId)).urlRequest() + } + + public func createStory(_ model: StoryDto) -> AnyPublisher { + BaseNetworkWorker(target: StoryTarget.postStory).urlRequest(contentBody: model) + } +} diff --git a/Tests/ToolboxAPIClientTests/Network/Professions/Models/ProfessionDto.swift b/Tests/ToolboxAPIClientTests/Network/Story/Models/StoryDto.swift similarity index 64% rename from Tests/ToolboxAPIClientTests/Network/Professions/Models/ProfessionDto.swift rename to Tests/ToolboxAPIClientTests/Network/Story/Models/StoryDto.swift index 9efa318..ab93bfe 100644 --- a/Tests/ToolboxAPIClientTests/Network/Professions/Models/ProfessionDto.swift +++ b/Tests/ToolboxAPIClientTests/Network/Story/Models/StoryDto.swift @@ -7,7 +7,7 @@ import Foundation -public struct ProfessionDto: Codable { - let id: String? +public struct StoryDto: Codable { + let id: UUID? let title: String } diff --git a/Tests/ToolboxAPIClientTests/Network/Story/Targets/StoryTarget.swift b/Tests/ToolboxAPIClientTests/Network/Story/Targets/StoryTarget.swift new file mode 100644 index 0000000..a9f838c --- /dev/null +++ b/Tests/ToolboxAPIClientTests/Network/Story/Targets/StoryTarget.swift @@ -0,0 +1,64 @@ +// +// File.swift +// +// +// Created by Zé on 20/03/21. +// + +import Foundation +import Combine +import ToolboxAPIClient + +public protocol StoryRequestable { + func getStorys() -> AnyPublisher<[StoryDto]?, Error> + func getStorys(limit: Int) -> AnyPublisher<[StoryDto]?, Error> + func getStory(storyId: String) -> AnyPublisher + func createStory(_ model: StoryDto) -> AnyPublisher +} + +public enum StoryTarget { + case getStorys + case getStorysWith(_ limt: Int) + case getStoryBy(_ professionId: String) + case postStory +} + +extension StoryTarget: TargetType { + public var queryString: [URLQueryItem] { + switch self { + case .getStorysWith(let limit): + return [URLQueryItem(name: "limit", value: "\(limit)")] + default: + return [] + } + } + + public var baseURL: URL { + URL(string: "https://astronomia-api.herokuapp.com/")! + } + + public var path: String { + switch self { + case .getStorys, + .getStorysWith: + return "stories" + case .postStory: + return "stories" + case .getStoryBy(let storyid): + return "stories/\(storyid)" + } + } + + public var method: HttpMethodEnum { + switch self { + case .getStorys, .getStoryBy, .getStorysWith: + return .get + case .postStory: + return .post + } + } + + public var headers: [String : String]? { + ["Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaWQiOiIxMTY3MDNFOS1CRjdFLTQyMEEtQjhFRi1BMjY1MTI5NzlEMjEiLCJzdWIiOiJhc3Ryb25vbWlhX2FwaSIsImV4cCI6NjQwOTIyMTEyMDAsImFkbWluIjp0cnVlfQ.D-ZoYLLVfdGaXrIfVl5BlKslN2oFezBpa0PguKCXmhs"] + } +} diff --git a/Tests/ToolboxAPIClientTests/Tests/app_zeneto_api_moduleTests.swift b/Tests/ToolboxAPIClientTests/Tests/app_zeneto_api_moduleTests.swift index d90709a..b394047 100644 --- a/Tests/ToolboxAPIClientTests/Tests/app_zeneto_api_moduleTests.swift +++ b/Tests/ToolboxAPIClientTests/Tests/app_zeneto_api_moduleTests.swift @@ -11,15 +11,12 @@ import Combine @testable import ToolboxAPIClient class app_zeneto_api_moduleTests: XCTestCase { - let expectation = XCTestExpectation(description: "GET PROFESSIONS DATA FROM MOCKS") + let expectation = XCTestExpectation(description: "GET STORIES DATA FROM MOCKS") let timeout: TimeInterval = 5.0 static var allTests = [ - ("getProfessions", testGetProfessions), - // ("getUsersByID", testGetUserById), -// ("getUsersWithLimit", testGetUserWithLimit), -// ("getUsers", testGetUser), - // ("deleteUser", testDeleteUserById) + ("getStories", testGetStorys), + ("createStrory", testCreateStory) ] override func setUpWithError() throws { @@ -30,15 +27,15 @@ class app_zeneto_api_moduleTests: XCTestCase { // Put teardown code here. This method is called after the invocation of each test method in the class. } - func testGetProfessions() throws { + func testGetStorys() throws { // This is an example of a functional test case. // Use XCTAssert and related functions to verify your tests produce the correct results. // let networkManager = NetworkManager() // let userManager = UserNetworkManager(networkController: networkManager) - let professionWorker = ProfessionsManagerAPI() + let professionWorker = StoryManagerAPI() var subscriptions = Set() - professionWorker.getProfessions() + professionWorker.getStorys() .sink(receiveCompletion: { [weak self] completion in switch completion { case let .failure(error): @@ -48,7 +45,7 @@ class app_zeneto_api_moduleTests: XCTestCase { self?.expectation.fulfill() break } - }, receiveValue: { (value: [ProfessionDto]?) in + }, receiveValue: { (value: [StoryDto]?) in guard let professions = value else { XCTFail() return @@ -59,13 +56,13 @@ class app_zeneto_api_moduleTests: XCTestCase { wait(for: [expectation], timeout: timeout) } - func testCreateProfession() throws { - let professionWorker = ProfessionsManagerAPI() + func testCreateStory() throws { + let professionWorker = StoryManagerAPI() var subscriptions = Set() - let profession = ProfessionDto(id: nil, title: "iOS Engineer") + let profession = StoryDto(id: nil, title: "iOS Engineer") professionWorker - .createProfession(profession) + .createStory(profession) .sink(receiveCompletion: { [weak self] completion in switch completion { case let .failure(error): @@ -75,7 +72,7 @@ class app_zeneto_api_moduleTests: XCTestCase { self?.expectation.fulfill() break } - }, receiveValue: { (value: ProfessionDto?) in + }, receiveValue: { (value: StoryDto?) in XCTAssertNotNil(value) }).store(in: &subscriptions)