-
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 #126 from Tunous/feat/featured-tags
User featured tags
- Loading branch information
Showing
6 changed files
with
125 additions
and
2 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
Examples/swiftyadmin/Sources/swiftyadmin/Account/GetFeaturedTags.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 @@ | ||
// | ||
// GetFeaturedTags.swift | ||
// Created by Łukasz Rutkowski on 18/03/2023. | ||
// | ||
|
||
import ArgumentParser | ||
import Foundation | ||
import TootSDK | ||
|
||
struct GetFeaturedTags: 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: .customShort("i"), help: "id of the user") | ||
var userID: String | ||
|
||
mutating func run() async throws { | ||
print("Getting featured tags of user with local id: \(userID)") | ||
let client = TootClient(instanceURL: URL(string: url)!, accessToken: token) | ||
|
||
let featuredTags = try await client.getFeaturedTags(forUser: userID) | ||
print(featuredTags) | ||
} | ||
} |
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,20 @@ | ||
// | ||
// Decoding+Helpers.swift | ||
// Created by Łukasz Rutkowski on 18/03/2023. | ||
// | ||
|
||
import Foundation | ||
|
||
extension KeyedDecodingContainerProtocol { | ||
func decodeIntFromString(forKey key: Key) throws -> Int { | ||
do { | ||
return try decode(Int.self, forKey: key) | ||
} catch { | ||
let string = try decode(String.self, forKey: key) | ||
if let int = Int(string) { | ||
return int | ||
} | ||
throw error | ||
} | ||
} | ||
} |
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,43 @@ | ||
// | ||
// DecodingTests.swift | ||
// | ||
// | ||
// Created by Łukasz Rutkowski on 18/03/2023. | ||
// | ||
|
||
import XCTest | ||
@testable import TootSDK | ||
|
||
final class DecodingTests: XCTestCase { | ||
|
||
func testDecodeIntFromString() throws { | ||
try assertDecodes(#"{"int": 2}"#, as: Element(2)) | ||
try assertDecodes(#"{"int": "5"}"#, as: Element(5)) | ||
try assertDecodes(#"{"int": -12}"#, as: Element(-12)) | ||
try assertDecodes(#"{"int": "-7"}"#, as: Element(-7)) | ||
} | ||
|
||
private func assertDecodes(_ json: String, as element: Element) throws { | ||
let decoder = JSONDecoder() | ||
let data = Data(json.utf8) | ||
let decodedElement = try decoder.decode(Element.self, from: data) | ||
XCTAssertEqual(decodedElement, element) | ||
} | ||
|
||
struct Element: Decodable, Equatable { | ||
let int: Int | ||
|
||
init(_ int: Int) { | ||
self.int = int | ||
} | ||
|
||
enum CodingKeys: CodingKey { | ||
case int | ||
} | ||
|
||
init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
self.int = try container.decodeIntFromString(forKey: .int) | ||
} | ||
} | ||
} |