Skip to content

Commit

Permalink
Implemented fetchTags function
Browse files Browse the repository at this point in the history
  • Loading branch information
jezreelbarbosa committed Jan 6, 2025
1 parent f555889 commit 7ecb010
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 31 deletions.
29 changes: 28 additions & 1 deletion Sources/NekosiaAPI/API/NekosiaAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import Foundation
public protocol NekosiaAPIServicing: AnyObject {
typealias ImagesCompletion = (Result<NekosiaAPIModel, NekosiaAPIError>) -> Void
typealias ImageCompletion = (Result<NekosiaImageItemModel, NekosiaAPIError>) -> Void
typealias TagsCompletion = (Result<NekosiaTagsModel, NekosiaAPIError>) -> Void

@discardableResult func fetchImages(category: String, query: Set<NekosiaQueryModel>?, completion: ImagesCompletion?) -> URLSessionDataTask?
@discardableResult func fetchById(_ id: String, completion: ImageCompletion?) -> URLSessionDataTask?
@discardableResult func fetchTags(completion: TagsCompletion?) -> URLSessionDataTask?
}

// MARK: -

@available(macOS 10.13, *)
public final class NekosiaAPI {
// Static Properties

Expand Down Expand Up @@ -94,6 +95,12 @@ extension NekosiaAPI: NekosiaAPIServicing {
let endpoint = NekosiaEndpoint(path: "/getImageById/\(id)")
return makeRequest(endpoint: endpoint, completion: completion)
}

@discardableResult
public func fetchTags(completion: TagsCompletion?) -> URLSessionDataTask? {
let endpoint = NekosiaEndpoint(path: "/tags")
return makeRequest(endpoint: endpoint, completion: completion)
}
}

// MARK: - Async Functions
Expand Down Expand Up @@ -133,6 +140,19 @@ public extension NekosiaAPIServicing {
}
}
}

func fetchTags() async throws -> NekosiaTagsModel {
return try await withCheckedThrowingContinuation { continuation in
fetchTags { result in
switch result {
case let .success(model):
continuation.resume(returning: model)
case let .failure(error):
continuation.resume(throwing: error)
}
}
}
}
}

// MARK: - Completion functions
Expand All @@ -154,6 +174,7 @@ public extension NekosiaAPIServicing {
public extension NekosiaAPIServicing {
typealias ImagesSuccessCompletion = (_ model: NekosiaAPIModel) -> Void
typealias ImageSuccessCompletion = (_ model: NekosiaImageItemModel) -> Void
typealias TagsSuccessCompletion = (_ model: NekosiaTagsModel) -> Void
typealias APIErrorCompletion = (_ error: NekosiaAPIError) -> Void

private func completion<T, U>(s: ((T) -> Void)?, f: ((U) -> Void)?) -> (Result<T, U>) -> Void {
Expand Down Expand Up @@ -192,6 +213,12 @@ public extension NekosiaAPIServicing {
onFailure: APIErrorCompletion?) -> URLSessionDataTask? {
return fetchById(id, completion: completion(s: onSuccess, f: onFailure))
}

@discardableResult
func fetchTags(onSuccess: TagsSuccessCompletion?,
onFailure: APIErrorCompletion?) -> URLSessionDataTask? {
return fetchTags(completion: completion(s: onSuccess, f: onFailure))
}
}

// MARK: - Logger helper
Expand Down
12 changes: 12 additions & 0 deletions Sources/NekosiaAPI/Models/NekosiaAPIModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,15 @@ public struct NekosiaArtistModel: Decodable, Equatable {
self.profile = profile
}
}

public struct NekosiaTagsModel: Decodable, Equatable {
public let tags: [String]
public let anime: [String]
public let characters: [String]

public init(tags: [String], anime: [String], characters: [String]) {
self.tags = tags
self.anime = anime
self.characters = characters
}
}
61 changes: 31 additions & 30 deletions Sources/NekosiaAPISample/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,50 @@ nekosiaAPI.isLoggerEnabled = true
if #available(iOS 13.0, macOS 10.15, watchOS 6, tvOS 13.0, *) {
Task {
do {
let ntags = try await nekosiaAPI.fetchTags()
print(ntags)
let cat = try await NekosiaAPI.shared.fetchImages(category: "catgirl")
print(cat)
let model = try await nekosiaAPI.fetchShadowImages(query: [.count(1), .additionalTags(tags), .blacklistedTags(["boy"]), .session("id"), .id("123456"), .rating(.questionable)])
print(model)
let image = try await nekosiaAPI.fetchById("66a7792dbf843e6bbe6eddc c")
let image = try await nekosiaAPI.fetchById("66a7792dbf843e6bbe6eddcc")
print(image)
} catch let error {
print(error)
}
}
} else {
// Fallback on earlier versions
}

nekosiaAPI.fetchImages(category: "catgirl", query: [
.count(1),
.additionalTags(tags),
.rating(.safe),
.session("id"),
.id("123456")
]) { model in
print(model)
} onFailure: { error in
print(error)
}
nekosiaAPI.fetchImages(category: "catgirl", query: [
.count(1),
.additionalTags(tags),
.rating(.safe),
.session("id"),
.id("123456")
]) { model in
print(model)
} onFailure: { error in
print(error)
}

nekosiaAPI.fetchById("66a7792dbf843e6bbe6eddccz") { model in
print(model)
} onFailure: { error in
print(error)
}
nekosiaAPI.fetchById("66a7792dbf843e6bbe6eddccz") { model in
print(model)
} onFailure: { error in
print(error)
}

nekosiaAPI.fetchShadowImages(query: [
.count(1),
.additionalTags(tags),
.blacklistedTags(["boy"]),
.session("id"),
.id("1234"),
.rating(.questionable)
]) { model in
print(model)
} onFailure: { error in
print(error)
nekosiaAPI.fetchShadowImages(query: [
.count(1),
.additionalTags(tags),
.blacklistedTags(["boy"]),
.session("id"),
.id("1234"),
.rating(.questionable)
]) { model in
print(model)
} onFailure: { error in
print(error)
}
}

RunLoop.main.run()

0 comments on commit 7ecb010

Please sign in to comment.