Swiftpack.co - lcharlick/PlexKit as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by lcharlick.
lcharlick/PlexKit 1.7.1
An async, type-safe Plex interface in Swift.
⭐️ 65
🕓 11 weeks ago
iOS macOS watchOS tvOS
.package(url: "https://github.com/lcharlick/PlexKit.git", from: "1.7.1")

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: 65
Last commit: 11 weeks ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

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