From 772cb999a5cb878f107a94cf9e1905cbd77e6136 Mon Sep 17 00:00:00 2001 From: JP Maia Date: Fri, 29 Mar 2024 11:48:30 -0300 Subject: [PATCH] DocC documentation --- .../aiXplainKit/Extensions/URL+MimeType.swift | 27 +++++++++++ .../Provider/PipelineProvider.swift | 45 ++++++++++++++----- Sources/aiXplainKit/aiXplainKit.swift | 2 - 3 files changed, 60 insertions(+), 14 deletions(-) diff --git a/Sources/aiXplainKit/Extensions/URL+MimeType.swift b/Sources/aiXplainKit/Extensions/URL+MimeType.swift index 56f0c38..df539dd 100644 --- a/Sources/aiXplainKit/Extensions/URL+MimeType.swift +++ b/Sources/aiXplainKit/Extensions/URL+MimeType.swift @@ -23,6 +23,33 @@ import Foundation import UniformTypeIdentifiers extension URL { + /** + Returns the MIME type associated with the file at the URL's path. + + The `mimeType()` method returns a `String` representing the MIME type of the file at the URL's path. If the file extension is recognized by the system, the method returns the preferred MIME type for that extension. If the file extension is not recognized, the method returns the default MIME type `"application/octet-stream"`. + + MIME types, or Multipurpose Internet Mail Extensions, are used to identify the type of data being transmitted over the Internet. They are especially important when transferring files, as they allow recipient applications to determine how to handle the incoming data. + + - Semantics: + - If the URL's path extension is recognized by the system, the method returns the preferred MIME type for that extension. + - If the URL's path extension is not recognized, the method returns `"application/octet-stream"`. + - If the URL does not have a path extension, the method returns `"application/octet-stream"`. + + - Returns: The MIME type associated with the file at the URL's path. + + - Examples: + ```swift + let fileURL = URL(fileURLWithPath: "/path/to/file.pdf") + let mimeType = fileURL.mimeType() // Returns "application/pdf" + ``` + In this example, the `mimeType()` method returns `"application/pdf"` because the `.pdf` extension is recognized by the system, and the preferred MIME type for PDF files is `"application/pdf"`. + + ```swift + let fileURL = URL(fileURLWithPath: "/path/to/file.unknown") + let mimeType = fileURL.mimeType() // Returns "application/octet-stream" + ``` + In this example, the `mimeType()` method returns `"application/octet-stream"` because the `.unknown` extension is not recognized by the system, and `"application/octet-stream"` is the default MIME type for unknown file types. + */ public func mimeType() -> String { if let mimeType = UTType(filenameExtension: self.pathExtension)?.preferredMIMEType { return mimeType diff --git a/Sources/aiXplainKit/Provider/PipelineProvider.swift b/Sources/aiXplainKit/Provider/PipelineProvider.swift index a7f2bec..ea600e4 100644 --- a/Sources/aiXplainKit/Provider/PipelineProvider.swift +++ b/Sources/aiXplainKit/Provider/PipelineProvider.swift @@ -24,39 +24,60 @@ import Foundation import OSLog + +/** + The `PipelineProvider` class is responsible for fetching `Pipeline` objects from the AiXplain backend. + + This class handles the network request to retrieve pipeline data and parses the response into a `Pipeline` object. + + - Important: You must have a valid API key and backend URL configured in `APIKeyManager` to use this class. + + */ public final class PipelineProvider { private let logger = Logger(subsystem: "AiXplain", category: "PipelineProvider") - + var networking = Networking() - + public init() { self.networking = Networking() } - + internal init(networking: Networking) { self.networking = networking } - + + /** + Fetches a `Pipeline` object from the AiXplain backend. + + This method sends a GET request to the backend URL with the provided `pipelineID` and parses the response into a `Pipeline` object. + + - Parameter pipelineID: The unique identifier of the pipeline to fetch. + - Throws: `PipelineError.missingBackendURL` if the backend URL is missing. + `ModelError.invalidURL` if the backend URL is invalid. + `NetworkingError.invalidStatusCode` if the server returns an unexpected status code. + An error of type `Error` for any other error that may occur during the request or parsing process. + - Returns: A `Pipeline` object containing the data for the requested pipeline. + */ public func get(_ pipelineID: String) async throws -> Pipeline { - + let headers: [String: String] = try networking.buildHeader() - + guard let url = APIKeyManager.shared.BACKEND_URL else { throw PipelineError.missingBackendURL } - + let endpoint = Networking.Endpoint.pipelines(pipelineIdentifier: pipelineID) guard let url = URL(string: url.absoluteString + endpoint.path) else { throw ModelError.invalidURL(url: url.absoluteString + endpoint.path) } - + let response = try await networking.get(url: url, headers: headers) - + if let httpUrlResponse = response.1 as? HTTPURLResponse, httpUrlResponse.statusCode != 200 { throw NetworkingError.invalidStatusCode(statusCode: httpUrlResponse.statusCode) } - + do { let fetchedPipeline = try JSONDecoder().decode(Pipeline.self, from: response.0) fetchedPipeline.id = pipelineID @@ -65,7 +86,7 @@ public final class PipelineProvider { logger.error("\(error.localizedDescription)") throw error } - + } - + } diff --git a/Sources/aiXplainKit/aiXplainKit.swift b/Sources/aiXplainKit/aiXplainKit.swift index f83b75f..29f8b8f 100644 --- a/Sources/aiXplainKit/aiXplainKit.swift +++ b/Sources/aiXplainKit/aiXplainKit.swift @@ -23,10 +23,8 @@ import Foundation -/// Test public final class AiXplainKit { public let keyManager = APIKeyManager.shared public static let shared = AiXplainKit() -// public var logLevel: ParrotLogger.LogSeverity = .info }