-
Notifications
You must be signed in to change notification settings - Fork 22
/
Twift+Likes.swift
111 lines (98 loc) 路 6.47 KB
/
Twift+Likes.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import Foundation
extension Twift {
/// Causes the user ID to Like the target Tweet.
///
/// Equivalent to `POST /2/users/:user_id/likes`
/// - Parameters:
/// - tweetId: The ID of the Tweet that you would like the `userId` to Like.
/// - userId: The user ID who you are liking a Tweet on behalf of. It must match your own user ID or that of an authenticating user. When set to `nil`, this method will try to use the currently-authenticated user's ID.
/// - Returns: A response object containing a ``LikeResponse``
public func likeTweet(_ tweetId: Tweet.ID, userId: User.ID? = nil) async throws -> TwitterAPIData<LikeResponse> {
guard let userId = userId ?? authenticatedUserId else { throw TwiftError.MissingUserID }
let body = ["tweet_id": tweetId]
let encodedBody = try JSONSerialization.data(withJSONObject: body, options: [])
return try await call(route: .userLikes(userId),
method: .POST,
body: encodedBody,
expectedReturnType: TwitterAPIData.self)
}
/// Causes the user ID to unlike the target Tweet.
///
/// Equivalent to `DELETE /2/users/:user_id/likes/:tweet_id`
/// - Parameters:
/// - tweetId: The ID of the Tweet that you would like the `userId` to unlike.
/// - userId: The user ID who you are removing Like of a Tweet on behalf of. It must match your own user ID or that of an authenticating user. When set to `nil`, this method will try to use the currently-authenticated user's ID.
/// - Returns: A response object containing a ``LikeResponse``
public func unlikeTweet(_ tweetId: Tweet.ID, userId: User.ID? = nil) async throws -> TwitterAPIData<LikeResponse> {
guard let userId = userId ?? authenticatedUserId else { throw TwiftError.MissingUserID }
return try await call(route: .deleteUserLikes(userId, tweetId: tweetId),
method: .DELETE,
expectedReturnType: TwitterAPIData.self)
}
/// Allows you to get information about a Tweet鈥檚 liking users.
///
/// Equivalent to `GET /2/tweets/:id/liking_users`
/// - Parameters:
/// - tweetId: Tweet ID of the Tweet to request liking users of.
/// - fields: Any additional fields to include on returned objects
/// - expansions: Objects and their corresponding fields that should be expanded in the `includes` property
/// - paginationToken: This parameter is used to move forwards or backwards through 'pages' of results, based on the value of the next_token or previous_token in the response.
/// - maxResults: Specifies the number of Tweets to try and retrieve, up to a maximum of 100 per distinct request. By default, 10 results are returned if this parameter is not supplied. The minimum permitted value is 10. It is possible to receive less than the max_results per request throughout the pagination process.
/// - Returns: A response object containing an array of Users that like the target Tweet
public func getLikingUsers(for tweetId: Tweet.ID,
fields: Set<User.Field> = [],
expansions: [User.Expansions],
paginationToken: String? = nil,
maxResults: Int = 10
) async throws -> TwitterAPIDataIncludesAndMeta<[User], User.Includes, Meta> {
switch maxResults {
case 1...100:
break
default:
throw TwiftError.RangeOutOfBoundsError(min: 1, max: 100, fieldName: "maxResults", actual: maxResults)
}
var queryItems = [URLQueryItem(name: "max_results", value: "\(maxResults)")]
if let paginationToken = paginationToken { queryItems.append(URLQueryItem(name: "pagination_token", value: paginationToken)) }
queryItems = queryItems + fieldsAndExpansions(for: User.self, fields: fields, expansions: expansions)
return try await call(route: .likingUsers(tweetId),
method: .GET,
queryItems: queryItems,
expectedReturnType: TwitterAPIDataIncludesAndMeta.self)
}
/// Allows you to get information about a user鈥檚 liked Tweets.
///
/// Equivalent to `GET /2/users/:id/liked_tweets`
/// - Parameters:
/// - userId: User ID of the user to request liked Tweets for. When set to `nil`, this method will try to use the currently-authenticated user's ID.
/// - fields: Any additional fields to include on returned objects
/// - expansions: Objects and their corresponding fields that should be expanded in the `includes` property
/// - paginationToken: This parameter is used to move forwards or backwards through 'pages' of results, based on the value of the next_token or previous_token in the response.
/// - maxResults: Specifies the number of Tweets to try and retrieve, up to a maximum of 100 per distinct request. By default, 10 results are returned if this parameter is not supplied. The minimum permitted value is 10. It is possible to receive less than the max_results per request throughout the pagination process.
/// - Returns: A response object containing an array of Tweets liked by the target User
public func getLikedTweets(for userId: User.ID? = nil,
fields: Set<Tweet.Field> = [],
expansions: [Tweet.Expansions],
paginationToken: String? = nil,
maxResults: Int = 10
) async throws -> TwitterAPIDataAndIncludes<[Tweet], Tweet.Includes> {
guard let userId = userId ?? authenticatedUserId else { throw TwiftError.MissingUserID }
switch maxResults {
case 10...100:
break
default:
throw TwiftError.RangeOutOfBoundsError(min: 10, max: 100, fieldName: "maxResults", actual: maxResults)
}
var queryItems = [URLQueryItem(name: "max_results", value: "\(maxResults)")]
if let paginationToken = paginationToken { queryItems.append(URLQueryItem(name: "pagination_token", value: paginationToken)) }
queryItems = queryItems + fieldsAndExpansions(for: Tweet.self, fields: fields, expansions: expansions)
return try await call(route: .likedTweets(userId),
method: .GET,
queryItems: queryItems,
expectedReturnType: TwitterAPIDataAndIncludes.self)
}
}
/// A response object containing information relating to Like-related API requests
public struct LikeResponse: Codable {
/// Indicates whether the user likes the specified Tweet as a result of this request.
let liked: Bool
}