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

Use MultiTarget for multiple targets using the same provider #39

Open
wants to merge 1 commit into
base: develop
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
18 changes: 9 additions & 9 deletions Papr/Services/CollectionService/CollectionService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,33 @@ import Moya

struct CollectionService: CollectionServiceType {

private var unsplash: MoyaProvider<Unsplash>
private var unsplash: MoyaProvider<MultiTarget>

init(unsplash: MoyaProvider<Unsplash> = MoyaProvider<Unsplash>()) {
init(unsplash: MoyaProvider<MultiTarget> = MoyaProvider<MultiTarget>()) {
self.unsplash = unsplash
}

func collection(withID id: Int) -> Observable<PhotoCollection> {
return unsplash.rx.request(.collection(id: id))
return unsplash.rx.request(MultiTarget(Unsplash.collection(id: id)))
.map(PhotoCollection.self)
.asObservable()
}

func collections(withUsername username: String) -> Observable<[PhotoCollection]> {
return self.unsplash.rx
.request(.userCollections(username: username, page: 1, perPage: 20))
.request(MultiTarget(Unsplash.userCollections(username: username, page: 1, perPage: 20)))
.map([PhotoCollection].self)
.asObservable()
}

func photos(fromCollectionId id: Int) -> Observable<[Photo]> {
return unsplash.rx.request(.collectionPhotos(id: id, page: 1, perPage: 10))
return unsplash.rx.request(MultiTarget(Unsplash.collectionPhotos(id: id, page: 1, perPage: 10)))
.map([Photo].self)
.asObservable()
}

func addPhotoToCollection(withId id: Int, photoId: String) -> Observable<Result<Photo, String>> {
return unsplash.rx.request(.addPhotoToCollection(collectionID: id, photoID: photoId))
return unsplash.rx.request(MultiTarget(Unsplash.addPhotoToCollection(collectionID: id, photoID: photoId)))
.map(CollectionResponse.self)
.map { $0.photo }
.asObservable()
Expand All @@ -48,7 +48,7 @@ struct CollectionService: CollectionServiceType {
}

func removePhotoFromCollection(withId id: Int, photoId: String) -> Observable<Result<Photo, String>> {
return unsplash.rx.request(.removePhotoFromCollection(collectionID: id, photoID: photoId))
return unsplash.rx.request(MultiTarget(Unsplash.removePhotoFromCollection(collectionID: id, photoID: photoId)))
.map(CollectionResponse.self)
.map { $0.photo }
.asObservable()
Expand All @@ -63,10 +63,10 @@ struct CollectionService: CollectionServiceType {
isPrivate: Bool
) -> Observable<Result<PhotoCollection, String>> {

return unsplash.rx.request(.createCollection(
return unsplash.rx.request(MultiTarget(Unsplash.createCollection(
title: title,
description: description,
isPrivate: isPrivate))
isPrivate: isPrivate)))
.map(PhotoCollection.self)
.asObservable()
.map (Result.success)
Expand Down
24 changes: 12 additions & 12 deletions Papr/Services/PhotoService/PhotoService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ import Moya

struct PhotoService: PhotoServiceType {

private var unsplash: MoyaProvider<Unsplash>
private var unsplash: MoyaProvider<MultiTarget>
//plugins: [NetworkLoggerPlugin(verbose: false)])

init(unsplash: MoyaProvider<Unsplash> = MoyaProvider<Unsplash>()) {
init(unsplash: MoyaProvider<MultiTarget> = MoyaProvider<MultiTarget>()) {
self.unsplash = unsplash
}

func like(photo: Photo) -> Observable<Result<Photo, NonPublicScopeError>> {
return unsplash.rx
.request(.likePhoto(id: photo.id ?? ""))
.request(MultiTarget(Unsplash.likePhoto(id: photo.id ?? "")))
.map(LikeUnlike.self)
.map { $0.photo }
.asObservable()
Expand All @@ -38,7 +38,7 @@ struct PhotoService: PhotoServiceType {

func unlike(photo: Photo) -> Observable<Result<Photo, NonPublicScopeError>> {
return unsplash.rx
.request(.unlikePhoto(id: photo.id ?? ""))
.request(MultiTarget(Unsplash.unlikePhoto(id: photo.id ?? "")))
.map(LikeUnlike.self)
.asObservable()
.map { $0.photo }
Expand All @@ -55,7 +55,7 @@ struct PhotoService: PhotoServiceType {

func photo(withId id: String) -> Observable<Photo> {
return unsplash.rx
.request(.photo(id: id, width: nil, height: nil, rect: nil))
.request(MultiTarget(Unsplash.photo(id: id, width: nil, height: nil, rect: nil)))
.map(Photo.self)
.asObservable()
}
Expand All @@ -69,12 +69,12 @@ struct PhotoService: PhotoServiceType {

if curated {
return unsplash.rx
.request(.curatedPhotos(
.request(MultiTarget(Unsplash.curatedPhotos(
page: pageNumber,
perPage: Constants.photosPerPage,
orderBy: orderBy
)
)
))
.map([Photo].self)
.asObservable()
.map(Result.success)
Expand All @@ -84,12 +84,12 @@ struct PhotoService: PhotoServiceType {
}

return unsplash.rx
.request(.photos(
.request(MultiTarget(Unsplash.photos(
page: pageNumber,
perPage: Constants.photosPerPage,
orderBy: orderBy
)
)
))
.map([Photo].self)
.asObservable()
.map(Result.success)
Expand All @@ -100,18 +100,18 @@ struct PhotoService: PhotoServiceType {

func statistics(of photo: Photo) -> Observable<PhotoStatistics> {
return unsplash.rx
.request(.photoStatistics(
.request(MultiTarget(Unsplash.photoStatistics(
id: photo.id ?? "",
resolution: .days,
quantity: 30)
)
))
.map(PhotoStatistics.self)
.asObservable()
}

func photoDownloadLink(withId id: String) -> Observable<Result<String, String>> {
return unsplash.rx
.request(.photoDownloadLink(id: id))
.request(MultiTarget(Unsplash.photoDownloadLink(id: id)))
.map(Link.self)
.map { $0.url }
.asObservable()
Expand Down
16 changes: 8 additions & 8 deletions Papr/Services/SearchService/SearchService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,40 @@ import RxSwift
import Moya

struct SearchService: SearchServiceType {
private var unsplash: MoyaProvider<Unsplash>
private var unsplash: MoyaProvider<MultiTarget>

init(unsplash: MoyaProvider<Unsplash> = MoyaProvider<Unsplash>()) {
init(unsplash: MoyaProvider<MultiTarget> = MoyaProvider<MultiTarget>()) {
self.unsplash = unsplash
}

func searchPhotos(with query: String, pageNumber: Int) -> Observable<PhotosResult> {
return unsplash.rx.request(.searchPhotos(
return unsplash.rx.request(MultiTarget(Unsplash.searchPhotos(
query: query,
page: pageNumber,
perPage: Constants.photosPerPage,
collections: nil,
orientation: nil)
)
))
.map(PhotosResult.self)
.asObservable()
}

func searchCollections(with query: String, pageNumber: Int) -> Observable<PhotoCollectionsResult> {
return unsplash.rx.request(.searchCollections(
return unsplash.rx.request(MultiTarget(Unsplash.searchCollections(
query: query,
page: pageNumber,
perPage: 10)
)
))
.map(PhotoCollectionsResult.self)
.asObservable()
}

func searchUsers(with query: String, pageNumber: Int) -> Observable<UsersResult> {
return unsplash.rx.request(.searchUsers(
return unsplash.rx.request(MultiTarget(Unsplash.searchUsers(
query: query,
page: pageNumber,
perPage: 10)
)
))
.map(UsersResult.self)
.asObservable()
}
Expand Down
6 changes: 3 additions & 3 deletions Papr/Services/UserService/UserService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import RxSwift

struct UserService: UserServiceType {

private var unsplash: MoyaProvider<Unsplash>
private var unsplash: MoyaProvider<MultiTarget>

init(unsplash: MoyaProvider<Unsplash> = MoyaProvider<Unsplash>()) {
init(unsplash: MoyaProvider<MultiTarget> = MoyaProvider<MultiTarget>()) {
self.unsplash = unsplash
}

func getMe() -> Observable<Result<User, NonPublicScopeError>> {
return unsplash.rx
.request(.getMe)
.request(MultiTarget(Unsplash.getMe))
.map(User.self)
.map(Result.success)
.catchError { error in
Expand Down
6 changes: 3 additions & 3 deletions Papr/Utils/Authentication/UnsplashAuthManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class UnsplashAuthManager {
private let clientSecret: String
private let redirectURL: URL
private let scopes: [String]
private let unplash: MoyaProvider<UnsplashAuthorization>
private let unplash: MoyaProvider<MultiTarget>

// MARK: Init
init(clientID: String, clientSecret: String, scopes: [String] = [Scope.pub.string]) {
Expand All @@ -92,7 +92,7 @@ class UnsplashAuthManager {
self.redirectURL = URL(string: UnsplashSettings.redirectURL.string)!
self.scopes = scopes

unplash = MoyaProvider<UnsplashAuthorization>()
unplash = MoyaProvider<MultiTarget>()
}

// MARK: Public
Expand All @@ -103,7 +103,7 @@ class UnsplashAuthManager {
}

public func accessToken(with code: String, completion: @escaping (String?, Error?) -> Void) {
unplash.request(.accessToken(withCode: code)) { response in
unplash.request(MultiTarget(UnsplashAuthorization.accessToken(withCode: code))) { response in
DispatchQueue.main.async { [unowned self] in
switch response {
case let .success(result):
Expand Down