Swiftpack.co - lcharlick/PlexKit as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
lcharlick/PlexKit
An async, type-safe Plex interface in Swift.
.package(url: "https://github.com/lcharlick/PlexKit.git", from: "1.3.4")

PlexKit

PlexKit provides asynchronous, type-safe access to a small portion of the Plex API.

Installation

Swift Package Manager

Create a Package.swift file.

import PackageDescription

let package = Package(
    name: "SampleProject",
    dependencies: [
        .Package(url: "https://github.com/lcharlick/PlexKit.git" from: "1.0.0")
    ]
)

CocoaPods

pod 'PlexKit'

Requirements

  • iOS 10.0+
  • macOS 10.12+
  • tvOS 10.0+

Getting Started

Authentication

Before accessing any resources, we need an authentication token:

import PlexKit

// Client identifier is required for most endpoints.
let info = Plex.ClientInfo(clientIdentifier: UUID().uuidString)

let client = Plex(sessionConfiguration: .default, clientInfo: info)

client.request(
    // plex.tv endpoints are namespaced under `Plex.ServiceRequest`.
    Plex.ServiceRequest.SimpleAuthentication(
        username: "USER",
        password: "PASS"
    )
) { result in
    switch result {
    case .success(let response):
        print("Hello, \(response.user.title)!")
        print("Your authentication token is \(response.user.authenticationToken)")
    case .failure(let error):
        print("An error occurred: \(error)")
    }
}

Finding a server

Once we have a token, we can hit every plex.tv endpoint, or access a server instance. To find a server (or "resource"), we can ask plex.tv:

client.request(
    Plex.ServiceRequest.Resources(),
    token: token
) { result in
    switch result {
    case .success(let response):
        print("Found \(response.count) resources")
        let servers = response.filter { $0.capabilities.contains(.server)}
        print("\(servers.count) of which are servers.")
    case .failure(let error):
        print("An error occurred: \(error)")
    }
}

Accessing libraries

client.request(
    // Resource-related requests are namespaced under `Plex.Request`.
    Plex.Request.Libraries(),
    from: url,
    token: token
) { result in
    switch result {
    case .success(let response):
        let libraries = response.mediaContainer.directory
        print("Found \(libraries.count) libraries")
        let musicLibraries = libraries.filter { $0.type == .artist }
        print("\(musicLibraries.count) are music libraries")
    case .failure(let error):
        print("An error occurred: \(error)")
    }
}

Notes

  • Check the Plex.Request and Plex.ServiceRequest namespaces for available endpoints.

  • PlexKit models map directly to data returned by Plex API. Where possible I've cleaned these up for Swift, though more work can be done here.

  • As it was originally written for Prism and Prologue, PlexKit mainly concentrates on the audio component of Plex, though other media types work, too.

License

PlexKit is available under the MIT license. See the LICENSE file for more info.

GitHub

link
Stars: 25
Last commit: 5 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics