Documentation pass

This commit is contained in:
2026-10-19 13:47:55 -07:00
parent d124749242
commit 95af1afef5
3 changed files with 37 additions and 3 deletions
+13
View File
@@ -22,6 +22,10 @@ public struct Client {
self.defaultHeaders = headers self.defaultHeaders = headers
} }
/// When all a client needs is the HTTP status code of an endpoint, this function should be used.
/// - Parameter request: Any Emby or Jellyfin `Request` conforming to the `Request` protocol.
/// - Throws:`RequestError.invalidResponse` if the response could not be coerced into a `HTTPURLResponse`.
/// - Returns: The HTTP status code.
func statusRequest(_ request: any Request) async throws -> Int { func statusRequest(_ request: any Request) async throws -> Int {
let urlRequest = try createRequestObject(for: request) let urlRequest = try createRequestObject(for: request)
let (_, response) = try await session.data(for: urlRequest) let (_, response) = try await session.data(for: urlRequest)
@@ -32,6 +36,10 @@ public struct Client {
return response.statusCode return response.statusCode
} }
/// Sends a request to an Emby or Jellyfin server, and builds a model object from that response.
/// - Parameter request: Any Emby or Jellyfin `Request` conforming to the `Request` protocol.
/// - Throws:`RequestError.invalidResponse` if the response could not be coerced into a `HTTPURLResponse`.
/// - Returns: A `Codable` model object whose type is infered from the receiving variable.
func dataRequest<T>(_ request: any Request) async throws -> T? where T: Codable { func dataRequest<T>(_ request: any Request) async throws -> T? where T: Codable {
let urlRequest = try createRequestObject(for: request) let urlRequest = try createRequestObject(for: request)
let (data, response) = try await session.data(for: urlRequest) let (data, response) = try await session.data(for: urlRequest)
@@ -42,6 +50,11 @@ public struct Client {
return try decoder.decode(T.self, from: data) return try decoder.decode(T.self, from: data)
} }
/// Verifies and creates a `URLRequest` from information provided by the `Request`.
/// Merges the `Request` headers, if any, with the default headers.
/// - Parameter request: Any Emby or Jellyfin `Request` conforming to the `Request` protocol.
/// - Throws: `RequestError.badURL` if unable to create a `URL` from the string.
/// - Returns: A fully formed `URLRequest` ready to send.
private func createRequestObject(for request: Request) throws -> URLRequest { private func createRequestObject(for request: Request) throws -> URLRequest {
guard let url = URL(string: "\(server)\(request.endpoint)") else { guard let url = URL(string: "\(server)\(request.endpoint)") else {
throw RequestError.badURL throw RequestError.badURL
+15 -1
View File
@@ -5,12 +5,26 @@
// Created by Alex Loren on 10/13/26. // Created by Alex Loren on 10/13/26.
// //
/// Contains the information of any `User` that is reported back by Emby or Jellyfin.
/// This model does not currently contain all data reported by the server.
/// Properties will become available as needed, not prior.
struct User: Codable { struct User: Codable {
// MARK: - Properties
/// A server generated unique Id for the `User`.
var id: String var id: String
/// The `User` belongs to the server with this Id.
var serverId: String var serverId: String
/// A `User` or server administer given name
var name: String var name: String
/// If `true` the `User` must be authenticated before making requests on their behalf.
var hasPassword: Bool var hasPassword: Bool
var primaryImageTag: String
/// If not `nil` a request can be made to retreive the profile image for the `User`.
var primaryImageTag: String?
enum CodingKeys: String, CodingKey { enum CodingKeys: String, CodingKey {
case id = "Id" case id = "Id"
+9 -2
View File
@@ -5,11 +5,18 @@
// Created by Alex Loren on 10/13/26. // Created by Alex Loren on 10/13/26.
// //
import Foundation /// The base for any `Request` made by JellyKit
protocol Request { protocol Request {
// MARK: - Properties // MARK: - Properties
/// This must begin with a forward slash.
/// Points to the resource on the Emby or Jellyfin server.
var endpoint: String { get set } var endpoint: String { get set }
/// What should this request be sent as?
var method: String { get set } var method: String { get set }
/// Any additional headers that are specific to *this* request.
/// Headers needed for every reqeust should be set at `Client` creation.
var headers: [String:String]? { get set } var headers: [String:String]? { get set }
} }