-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #150 from Tunous/feat/tags
API to get, follow and unfollow tags
- Loading branch information
Showing
7 changed files
with
150 additions
and
1 deletion.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
Examples/swiftyadmin/Sources/swiftyadmin/Account/FollowTag.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// FollowTag.swift | ||
// Created by Łukasz Rutkowski on 21/04/2023. | ||
// | ||
|
||
import ArgumentParser | ||
import Foundation | ||
import TootSDK | ||
|
||
struct FollowTag: AsyncParsableCommand { | ||
|
||
@Option(name: .short, help: "URL to the instance to connect to") | ||
var url: String | ||
|
||
@Option(name: .short, help: "Access token for an account with sufficient permissions.") | ||
var token: String | ||
|
||
@Option(name: .shortAndLong, help: "id of the tag") | ||
var id: String | ||
|
||
mutating func run() async throws { | ||
print("Following tag with local id: \(id)") | ||
let client = TootClient(instanceURL: URL(string: url)!, accessToken: token) | ||
|
||
let tag = try await client.followTag(id) | ||
print(tag) | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
Examples/swiftyadmin/Sources/swiftyadmin/Account/GetTag.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// GetTag.swift | ||
// Created by Łukasz Rutkowski on 21/04/2023. | ||
// | ||
|
||
import ArgumentParser | ||
import Foundation | ||
import TootSDK | ||
|
||
struct GetTag: AsyncParsableCommand { | ||
|
||
@Option(name: .short, help: "URL to the instance to connect to") | ||
var url: String | ||
|
||
@Option(name: .short, help: "Access token for an account with sufficient permissions.") | ||
var token: String | ||
|
||
@Option(name: .shortAndLong, help: "id of the tag") | ||
var id: String | ||
|
||
mutating func run() async throws { | ||
print("Getting tag with local id: \(id)") | ||
let client = TootClient(instanceURL: URL(string: url)!, accessToken: token) | ||
|
||
let tag = try await client.getTag(id) | ||
print(tag) | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
Examples/swiftyadmin/Sources/swiftyadmin/Account/UnfollowTag.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// UnfollowTag.swift | ||
// Created by Łukasz Rutkowski on 21/04/2023. | ||
// | ||
|
||
import ArgumentParser | ||
import Foundation | ||
import TootSDK | ||
|
||
struct UnfollowTag: AsyncParsableCommand { | ||
|
||
@Option(name: .short, help: "URL to the instance to connect to") | ||
var url: String | ||
|
||
@Option(name: .short, help: "Access token for an account with sufficient permissions.") | ||
var token: String | ||
|
||
@Option(name: .shortAndLong, help: "id of the tag") | ||
var id: String | ||
|
||
mutating func run() async throws { | ||
print("Unfollowing tag with local id: \(id)") | ||
let client = TootClient(instanceURL: URL(string: url)!, accessToken: token) | ||
|
||
let tag = try await client.unfollowTag(id) | ||
print(tag) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// | ||
// TootClient+Tags.swift | ||
// Created by Łukasz Rutkowski on 21/04/2023. | ||
// | ||
|
||
import Foundation | ||
|
||
public extension TootClient { | ||
|
||
/// Get a tag. | ||
/// - Parameter id: Name of the tag. | ||
func getTag(_ id: String) async throws -> Tag { | ||
let req = HTTPRequestBuilder { | ||
$0.url = getURL(["api", "v1", "tags", id]) | ||
$0.method = .get | ||
} | ||
|
||
return try await fetch(Tag.self, req) | ||
} | ||
|
||
/// Follow a tag. | ||
/// - Parameter id: Name of the tag. | ||
@discardableResult | ||
func followTag(_ id: String) async throws -> Tag { | ||
try requireFlavour(flavoursSupportingFollowingTags) | ||
let req = HTTPRequestBuilder { | ||
$0.url = getURL(["api", "v1", "tags", id, "follow"]) | ||
$0.method = .post | ||
} | ||
|
||
return try await fetch(Tag.self, req) | ||
} | ||
|
||
/// Unfollow a tag. | ||
/// - Parameter id: Name of the tag. | ||
@discardableResult | ||
func unfollowTag(_ id: String) async throws -> Tag { | ||
try requireFlavour(flavoursSupportingFollowingTags) | ||
let req = HTTPRequestBuilder { | ||
$0.url = getURL(["api", "v1", "tags", id, "unfollow"]) | ||
$0.method = .post | ||
} | ||
|
||
return try await fetch(Tag.self, req) | ||
} | ||
|
||
/// Tells whether current flavour supports following or unfollowing tags. | ||
var canFollowTags: Bool { | ||
flavoursSupportingFollowingTags.contains(flavour) | ||
} | ||
|
||
private var flavoursSupportingFollowingTags: Set<TootSDKFlavour> { | ||
[.mastodon, .friendica] | ||
} | ||
} |