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

refactor: removes Alamofire from reachable #25

Merged
merged 1 commit into from
Jun 8, 2024
Merged
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
8 changes: 3 additions & 5 deletions Sources/OllamaKit/OllamaKit+CopyModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,12 @@ extension OllamaKit {
/// - Parameter data: The ``OKCopyModelRequestData`` used to request the model copy.
/// - Returns: A `AnyPublisher<Void, Error>` that completes when the copy operation is done.
public func copyModel(data: OKCopyModelRequestData) -> AnyPublisher<Void, Error> {
let request: URLRequest

do {
request = try OKRouter.copyModel(data: data).asURLRequest()
let request = try OKRouter.copyModel(data: data).asURLRequest()

return OKHTTPClient.shared.sendRequest(for: request)
} catch {
return Fail(error: error).eraseToAnyPublisher()
}

return OKHTTPClient.shared.sendRequest(for: request)
}
}
8 changes: 3 additions & 5 deletions Sources/OllamaKit/OllamaKit+DeleteModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,12 @@ extension OllamaKit {
/// - Parameter data: The ``OKDeleteModelRequestData`` used to request the model deletion.
/// - Returns: A `AnyPublisher<Void, Error>` that completes when the deletion operation is done.
public func deleteModel(data: OKDeleteModelRequestData) -> AnyPublisher<Void, Error> {
let request: URLRequest

do {
request = try OKRouter.deleteModel(data: data).asURLRequest()
let request = try OKRouter.deleteModel(data: data).asURLRequest()

return OKHTTPClient.shared.sendRequest(for: request)
} catch {
return Fail(error: error).eraseToAnyPublisher()
}

return OKHTTPClient.shared.sendRequest(for: request)
}
}
8 changes: 3 additions & 5 deletions Sources/OllamaKit/OllamaKit+Embeddings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,12 @@ extension OllamaKit {
/// - Parameter data: The ``OKEmbeddingsRequestData`` used to query the API for embeddings from a specific model.
/// - Returns: A `AnyPublisher<OKEmbeddingsResponse, AFError>` that emits embeddings.
public func embeddings(data: OKEmbeddingsRequestData) -> AnyPublisher<OKEmbeddingsResponse, Error> {
let request: URLRequest

do {
request = try OKRouter.embeddings(data: data).asURLRequest()
let request = try OKRouter.embeddings(data: data).asURLRequest()

return OKHTTPClient.shared.sendRequest(for: request, with: OKEmbeddingsResponse.self)
} catch {
return Fail(error: error).eraseToAnyPublisher()
}

return OKHTTPClient.shared.sendRequest(for: request, with: OKEmbeddingsResponse.self)
}
}
9 changes: 3 additions & 6 deletions Sources/OllamaKit/OllamaKit+ModelInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// Created by Kevin Hermawan on 01/01/24.
//

import Alamofire
import Combine
import Foundation

Expand Down Expand Up @@ -49,14 +48,12 @@ extension OllamaKit {
/// - Parameter data: The ``OKModelInfoRequestData`` used to query the API for specific model information.
/// - Returns: A `AnyPublisher<OKModelInfoResponse, Error>` that emits detailed information about the model.
public func modelInfo(data: OKModelInfoRequestData) -> AnyPublisher<OKModelInfoResponse, Error> {
let request: URLRequest

do {
request = try OKRouter.modelInfo(data: data).asURLRequest()
let request = try OKRouter.modelInfo(data: data).asURLRequest()

return OKHTTPClient.shared.sendRequest(for: request, with: OKModelInfoResponse.self)
} catch {
return Fail(error: error).eraseToAnyPublisher()
}

return OKHTTPClient.shared.sendRequest(for: request, with: OKModelInfoResponse.self)
}
}
8 changes: 3 additions & 5 deletions Sources/OllamaKit/OllamaKit+Models.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,12 @@ extension OllamaKit {
///
/// - Returns: A `AnyPublisher<OKModelResponse, Error>` that emits the list of available models.
public func models() -> AnyPublisher<OKModelResponse, Error> {
let request: URLRequest

do {
request = try OKRouter.models.asURLRequest()
let request = try OKRouter.models.asURLRequest()

return OKHTTPClient.shared.sendRequest(for: request, with: OKModelResponse.self)
} catch {
return Fail(error: error).eraseToAnyPublisher()
}

return OKHTTPClient.shared.sendRequest(for: request, with: OKModelResponse.self)
}
}
22 changes: 12 additions & 10 deletions Sources/OllamaKit/OllamaKit+Reachable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// Created by Kevin Hermawan on 01/01/24.
//

import Alamofire
import Combine
import Foundation

Expand All @@ -21,10 +20,9 @@ extension OllamaKit {
///
/// - Returns: `true` if the Ollama API is reachable, `false` otherwise.
public func reachable() async -> Bool {
let request = AF.request(router.root).validate()

do {
_ = try await request.serializingData().value
let request = try OKRouter.root.asURLRequest()
try await OKHTTPClient.shared.sendRequest(for: request)

return true
} catch {
Expand All @@ -48,11 +46,15 @@ extension OllamaKit {
///
/// - Returns: A `AnyPublisher<Bool, Never>` that emits `true` if the API is reachable, `false` otherwise.
public func reachable() -> AnyPublisher<Bool, Never> {
let request = AF.request(router.root).validate()

return request.publishData().value()
.map { _ in true }
.replaceError(with: false)
.eraseToAnyPublisher()
do {
let request = try OKRouter.root.asURLRequest()

return OKHTTPClient.shared.sendRequest(for: request)
.map { _ in true }
.replaceError(with: false)
.eraseToAnyPublisher()
} catch {
return Just(false).eraseToAnyPublisher()
}
}
}
Loading