Skip to content

Commit

Permalink
Add async support for search nethods
Browse files Browse the repository at this point in the history
  • Loading branch information
jjotaum committed Feb 23, 2023
1 parent 306168d commit 264ed4b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
32 changes: 24 additions & 8 deletions Sources/AmuseKit/Networking/DataProvider.swift
Expand Up @@ -17,6 +17,7 @@ public extension AmuseKit {

private var storage: StorageService
private let requestCoordinator: RequestCoordinator
private let decoder: RequestDecoder = JSONDecoder()
private var userCountryCode: String = "us"

public init(_ storageConfiguration: StorageConfiguration,
Expand Down Expand Up @@ -58,7 +59,7 @@ public extension AmuseKit {
)
request.setValue("Bearer \(developerToken)", forHTTPHeaderField: "Authorization")
request.setValue(storage.userToken, forHTTPHeaderField: "Music-User-Token")
return try await requestCoordinator.data(request: request, decoder: JSONDecoder())
return try await requestCoordinator.data(request: request, decoder: decoder)
}

public func catalog<Resource: Codable>(_ resourceType: CatalogResourceConvertible<Resource>, ids: [String]) throws -> AnyPublisher<AmuseKit.ResponseRoot<Resource, EmptyCodable>, Error> {
Expand All @@ -71,11 +72,19 @@ public extension AmuseKit {
)
request.setValue("Bearer \(developerToken)", forHTTPHeaderField: "Authorization")
request.setValue(storage.userToken, forHTTPHeaderField: "Music-User-Token")
return try requestCoordinator.dataTaskPublisher(for: request, decoder: JSONDecoder())
return try requestCoordinator.dataTaskPublisher(for: request, decoder: decoder)
}

@available(iOS 15.0, *)
@available(macOS 12.0, *)
public func catalogSearch(_ resourceTypes: CatalogSearchTypes = .all, limit: Int = 25, searchTerm: String) async throws -> AmuseKit.SearchResponse {
let request = try searchRequest(.search(countryCode: userCountryCode), rawTypes: resourceTypes.map({ $0.rawValue }), limit: limit, searchTerm: searchTerm)
return try await requestCoordinator.data(request: request, decoder: decoder)
}

public func catalogSearch(_ resourceTypes: CatalogSearchTypes = .all, limit: Int = 25, searchTerm: String) throws -> AnyPublisher<AmuseKit.SearchResponse, Error> {
try searchRequest(.search(countryCode: userCountryCode), rawTypes: resourceTypes.map({ $0.rawValue }), limit: limit, searchTerm: searchTerm)
let request = try searchRequest(.search(countryCode: userCountryCode), rawTypes: resourceTypes.map({ $0.rawValue }), limit: limit, searchTerm: searchTerm)
return try requestCoordinator.dataTaskPublisher(for: request, decoder: decoder)
}

// MARK: - Library Methods
Expand All @@ -89,7 +98,7 @@ public extension AmuseKit {
var request = try Router.library(resourceType.rawValue).asURLRequest([])
request.setValue("Bearer \(developerToken)", forHTTPHeaderField: "Authorization")
request.setValue(storage.userToken, forHTTPHeaderField: "Music-User-Token")
return try await requestCoordinator.data(request: request, decoder: JSONDecoder())
return try await requestCoordinator.data(request: request, decoder: decoder)
}

public func library<Resource: Codable>(_ resourceType: LibraryResourceConvertible<Resource>) throws -> AnyPublisher<AmuseKit.ResponseRoot<Resource, EmptyCodable>, Error> {
Expand All @@ -100,17 +109,24 @@ public extension AmuseKit {
var request = try Router.library(resourceType.rawValue).asURLRequest([])
request.setValue("Bearer \(developerToken)", forHTTPHeaderField: "Authorization")
request.setValue(storage.userToken, forHTTPHeaderField: "Music-User-Token")
return try requestCoordinator.dataTaskPublisher(for: request, decoder: JSONDecoder())
return try requestCoordinator.dataTaskPublisher(for: request, decoder: decoder)
}

@available(iOS 15.0, *)
@available(macOS 12.0, *)
public func librarySearch(_ resourceTypes: LibrarySearchTypes = .all, limit: Int = 25, searchTerm: String) async throws -> AmuseKit.LibrarySearchResponse {
let request = try searchRequest(.librarySearch, rawTypes: resourceTypes.map({ $0.rawValue }), limit: limit, searchTerm: searchTerm)
return try await requestCoordinator.data(request: request, decoder: decoder)
}

public func librarySearch(_ resourceTypes: LibrarySearchTypes = .all, limit: Int = 25, searchTerm: String) throws -> AnyPublisher<AmuseKit.LibrarySearchResponse, Error> {
try searchRequest(.librarySearch, rawTypes: resourceTypes.map({ $0.rawValue }), limit: limit, searchTerm: searchTerm)
let request = try searchRequest(.librarySearch, rawTypes: resourceTypes.map({ $0.rawValue }), limit: limit, searchTerm: searchTerm)
return try requestCoordinator.dataTaskPublisher(for: request, decoder: decoder)
}

// MARK: - Private Methods

private func searchRequest<T: Codable>(_ router: Router, rawTypes: [String], limit: Int, searchTerm: String) throws -> AnyPublisher<T, Error> {
private func searchRequest(_ router: Router, rawTypes: [String], limit: Int, searchTerm: String) throws -> URLRequest {
guard let developerToken = storage.developerToken else {
throw AmuseKitError.missingDevToken
}
Expand All @@ -122,7 +138,7 @@ public extension AmuseKit {
]
var request = try router.asURLRequest(queryItems)
request.setValue("Bearer \(developerToken)", forHTTPHeaderField: "Authorization")
return try requestCoordinator.dataTaskPublisher(for: request, decoder: JSONDecoder())
return request
}
}
}
25 changes: 25 additions & 0 deletions Tests/AmuseKitTests/Networking/DataProviderTests.swift
Expand Up @@ -151,6 +151,18 @@ final class DataProviderTests: XCTestCase {
ids: ids)
}

@available(iOS 15.0, *)
@available(macOS 12.0, *)
func test_async_catalogSearchRequest_withValidResponse_returnsResults() async throws {
let sut: AmuseKit.DataProvider = .mock(resourceName: "catalog_search")
let response = try await sut.catalogSearch(searchTerm: "")
XCTAssertNotNil(response.results?.playlists)
XCTAssertNotNil(response.results?.albums)
XCTAssertNotNil(response.results?.artists)
XCTAssertNotNil(response.results?.songs)
XCTAssertNotNil(response.results?.musicVideos)
}

func test_catalogSearchRequest_withValidResponse_returnsResults() throws {
let completionExpectation = XCTestExpectation(description: "completion should be called")
let valueExpectation = XCTestExpectation(description: "value callback should be called")
Expand Down Expand Up @@ -249,6 +261,17 @@ final class DataProviderTests: XCTestCase {
resourceType: .songs)
}

@available(iOS 15.0, *)
@available(macOS 12.0, *)
func test_async_librarySearchRequest_withValidResponse_returnsResults() async throws {
let sut: AmuseKit.DataProvider = .mock(resourceName: "library_search")
let response = try await sut.librarySearch(searchTerm: "")
XCTAssertNotNil(response.results?.playlists)
XCTAssertNotNil(response.results?.albums)
XCTAssertNotNil(response.results?.artists)
XCTAssertNotNil(response.results?.songs)
}

func test_LibrarySearchRequest_withValidResponse_returnsResults() throws {
let completionExpectation = XCTestExpectation(description: "completion should be called")
let valueExpectation = XCTestExpectation(description: "value callback should be called")
Expand Down Expand Up @@ -279,6 +302,7 @@ final class DataProviderTests: XCTestCase {
("test_catalogMusicVideosRequest_withValidResponse_returnsData", test_catalogMusicVideosRequest_withValidResponse_returnsData),
("test_catalogPlaylistsRequest_withValidResponse_returnsData", test_catalogPlaylistsRequest_withValidResponse_returnsData),
("test_catalogSongsRequest_withValidResponse_returnsData", test_catalogSongsRequest_withValidResponse_returnsData),
("test_async_catalogSearchRequest_withValidResponse_returnsResults", test_async_catalogSearchRequest_withValidResponse_returnsResults),
("test_catalogSearchRequest_withValidResponse_returnsResults", test_catalogSearchRequest_withValidResponse_returnsResults),
// Library
("test_libraryRequest_withMissingDeveloperToken_throwsError", test_libraryRequest_withMissingDeveloperToken_throwsError),
Expand All @@ -287,6 +311,7 @@ final class DataProviderTests: XCTestCase {
("test_libraryMusicVideosRequest_withValidResponse_returnsData", test_libraryMusicVideosRequest_withValidResponse_returnsData),
("test_libraryPlaylistsRequest_withValidResponse_returnsData", test_libraryPlaylistsRequest_withValidResponse_returnsData),
("test_librarySongsRequest_withValidResponse_returnsData", test_librarySongsRequest_withValidResponse_returnsData),
("test_async_librarySearchRequest_withValidResponse_returnsResults", test_async_librarySearchRequest_withValidResponse_returnsResults),
("test_LibrarySearchRequest_withValidResponse_returnsResults", test_LibrarySearchRequest_withValidResponse_returnsResults)
]
}
Expand Down

0 comments on commit 264ed4b

Please sign in to comment.