Swiftpack.co - Swiftodon/Mastodon.swift as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by Swiftodon.
Swiftodon/Mastodon.swift 2.1.2
A Mastodon API client written in Swift for iOS, iPadOS and macOS.
⭐️ 75
🕓 4 weeks ago
iOS macOS watchOS
.package(url: "https://github.com/Swiftodon/Mastodon.swift.git", from: "2.1.2")

MastodonClient

GitHub

GitHub Workflow Status (branch)

Howto

This client is designed to connect to any Mastodon instance and interact with it.

MastodonClient contains a few convenience methods to create Apps (OAuth Clients) and interact with the API but you should use the URLSession TargetTypes directly for the time being (as those are feature complete), e.g. for getting your Home Timeline:

let request = try MastodonClient.request(
    for: URL(string: "https://mastodon.social")!,
    target: Mastodon.Timelines.home(nil, nil),
    withBearerToken: token
)

let (data, _) = try await session.data(for: request)

let result = try JSONDecoder().decode([Status].self, from: data)

Given you've got an OAuth Client

let app = App(clientId: "…", clientSecret: "…")

Logging in is as easy as this then:

let client = MastodonClient(baseURL: URL(string: "https://mastodon.tld")!)

let app = App(
    clientId: "a1a2a3a4a5",
    clientSecret: "s3cr3t"
)

let response = try await client.getToken(
    app,
    username: "[email protected]",
    password: "pa4w0rd",
    scope: ["read", "write", "follow"]
)

Provided login was successful and you've retrieved an AccessToken you're free to use all the other APIs, e.g. to retrieve your home timeline using MastodonClientAuthenticated e.g.:

let client = MastodonClient(baseURL: URL(string: "https://mastodon.tld")!)
    .getAuthenticated(token: token)

let result = try await client.getHomeTimeline()

Requirements

  • Xcode 14 / Swift 5

Authors

License

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

GitHub

link
Stars: 75
Last commit: 1 week ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Dependencies

Release Notes

2.1.2
5 weeks ago
  • fix: Status.url is now optional

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