Skip to content

Commit 6d975e4

Browse files
committed
Use currently-authenticated user ID for convenience
1 parent a116147 commit 6d975e4

File tree

10 files changed

+99
-37
lines changed

10 files changed

+99
-37
lines changed

Sources/Twift+Blocks.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,20 @@ extension Twift {
77
///
88
/// Equivalent to `GET /2/users/:id/blocking`.
99
/// - Parameters:
10-
/// - userId: The user ID whose blocked users you would like to retrieve
10+
/// - userId: The user ID whose blocked users you would like to retrieve. When set to `nil`, this method will try to use the currently-authenticated user's ID.
1111
/// - fields: Any additional fields to include on returned objects
1212
/// - expansions: Objects and their corresponding fields that should be expanded in the `includes` property
1313
/// - paginationToken: When iterating over pages of results, you can pass in the `nextToken` from the previously-returned value to get the next page of results
1414
/// - maxResults: The maximum number of results to fetch.
1515
/// - Returns: A Twitter API response object containing an array of ``User`` structs and any pinned tweets in the `includes` property
16-
public func getBlockedUsers(for userId: User.ID,
16+
public func getBlockedUsers(for userId: User.ID? = nil,
1717
fields: Set<User.Field> = [],
1818
expansions: [User.Expansions] = [],
1919
paginationToken: String? = nil,
2020
maxResults: Int = 100
2121
) async throws -> TwitterAPIDataIncludesAndMeta<[User], User.Includes, Meta> {
22+
guard let userId = userId ?? authenticatedUserId else { throw TwiftError.MissingUserID }
23+
2224
switch maxResults {
2325
case 0...1000:
2426
break
@@ -46,7 +48,9 @@ extension Twift {
4648
/// - sourceUserId: The user ID who you would like to initiate the block on behalf of. It must match the user ID of the currently authenticated user.
4749
/// - targetUserId: The user ID of the user you would like the source user to block.
4850
/// - Returns: A ``BlockResponse`` indicating the blocked status.
49-
public func blockUser(sourceUserId: User.ID, targetUserId: User.ID) async throws -> TwitterAPIData<BlockResponse> {
51+
public func blockUser(sourceUserId: User.ID? = nil, targetUserId: User.ID) async throws -> TwitterAPIData<BlockResponse> {
52+
guard let sourceUserId = sourceUserId ?? authenticatedUserId else { throw TwiftError.MissingUserID }
53+
5054
let body = ["target_user_id": targetUserId]
5155
let serializedBody = try JSONSerialization.data(withJSONObject: body)
5256
return try await call(route: .blocking(sourceUserId),

Sources/Twift+Errors.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ public enum TwiftError: Error {
1515
/// This error is thrown when the called function expected an integer within a specified range but was passed a value outside that range.
1616
case RangeOutOfBoundsError(min: Int = 1, max: Int = 1000, fieldName: String, actual: Int)
1717

18+
/// This error is thrown when the function called expects a User ID but the caller did not provide one and one could not be found in the current Twift instance.
19+
case MissingUserID
20+
1821
/// The human-readable description for the error
1922
public var description: String {
2023
switch self {
@@ -26,6 +29,8 @@ public enum TwiftError: Error {
2629
return "Unknown Error"
2730
case .RangeOutOfBoundsError(let min, let max, let fieldName, let actual):
2831
return "Expected a value between \(min) and \(max) for field \"\(fieldName)\" but got \(actual)"
32+
case .MissingUserID:
33+
return "The function called expects a User ID but the caller did not provide one, and one could not be found in the current Twift instance."
2934
}
3035
}
3136
}

Sources/Twift+Follows.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ extension Twift {
2020
/// - paginationToken: When iterating over pages of results, you can pass in the `nextToken` from the previously-returned value to get the next page of results
2121
/// - maxResults: The maximum number of results to fetch.
2222
/// - Returns: A Twitter API response object containing an array of ``User`` structs and any pinned tweets in the `includes` property
23-
public func getFollowing(_ userId: User.ID,
23+
public func getFollowing(_ userId: User.ID? = nil,
2424
fields: Set<User.Field> = [],
2525
expansions: [User.Expansions] = [],
2626
paginationToken: String? = nil,
2727
maxResults: Int = 100
2828
) async throws -> TwitterAPIDataIncludesAndMeta<[User], User.Includes, Meta> {
29+
guard let userId = userId ?? authenticatedUserId else { throw TwiftError.MissingUserID }
30+
2931
switch maxResults {
3032
case 0...1000:
3133
break
@@ -55,12 +57,14 @@ extension Twift {
5557
/// - paginationToken: When iterating over pages of results, you can pass in the `nextToken` from the previously-returned value to get the next page of results
5658
/// - maxResults: The maximum number of results to fetch.
5759
/// - Returns: A Twitter API response object containing an array of ``User`` structs and any pinned tweets in the `includes` property
58-
public func getFollowers(_ userId: User.ID,
60+
public func getFollowers(_ userId: User.ID? = nil,
5961
fields: Set<User.Field> = [],
6062
expansions: [User.Expansions] = [],
6163
paginationToken: String? = nil,
6264
maxResults: Int = 100
6365
) async throws -> TwitterAPIDataIncludesAndMeta<[User], User.Includes, Meta> {
66+
guard let userId = userId ?? authenticatedUserId else { throw TwiftError.MissingUserID }
67+
6468
switch maxResults {
6569
case 0...1000:
6670
break
@@ -93,9 +97,10 @@ extension Twift {
9397
/// - targetUserId: The user ID of the user that you would like the `sourceUserId` to follow.
9498
/// - Returns: A ``FollowResponse`` indicating whether the source user is now following the target user, and whether the follow request is pending
9599
public func followUser(
96-
sourceUserId: User.ID,
100+
sourceUserId: User.ID? = nil,
97101
targetUserId: User.ID
98102
) async throws -> TwitterAPIData<FollowResponse> {
103+
guard let sourceUserId = sourceUserId ?? authenticatedUserId else { throw TwiftError.MissingUserID }
99104
let body = ["target_user_id": targetUserId]
100105
let serializedBody = try JSONSerialization.data(withJSONObject: body)
101106
return try await call(route: .following(sourceUserId),
@@ -113,9 +118,10 @@ extension Twift {
113118
/// - sourceUserId: The authenticated user ID who you would like to initiate the unfollow on behalf of.
114119
/// - targetUserId: The user ID of the user that you would like the `sourceUserId` to unfollow.
115120
/// - Returns: A ``FollowResponse`` indicating whether the source user is now following the target user
116-
public func unfollowUser(sourceUserId: User.ID,
121+
public func unfollowUser(sourceUserId: User.ID? = nil,
117122
targetUserId: User.ID
118123
) async throws -> TwitterAPIData<FollowResponse> {
124+
guard let sourceUserId = sourceUserId ?? authenticatedUserId else { throw TwiftError.MissingUserID }
119125
return try await call(route: .deleteFollow(sourceUserId: sourceUserId, targetUserId: targetUserId),
120126
method: .DELETE,
121127
expectedReturnType: TwitterAPIData.self)

Sources/Twift+Likes.swift

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ extension Twift {
88
/// - tweetId: The ID of the Tweet that you would like the `userId` to Like.
99
/// - 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.
1010
/// - Returns: A response object containing a ``LikeResponse``
11-
public func likeTweet(_ tweetId: Tweet.ID, userId: User.ID) async throws -> TwitterAPIData<LikeResponse> {
11+
public func likeTweet(_ tweetId: Tweet.ID, userId: User.ID? = nil) async throws -> TwitterAPIData<LikeResponse> {
12+
guard let userId = userId ?? authenticatedUserId else { throw TwiftError.MissingUserID }
13+
1214
let body = ["tweet_id": tweetId]
1315
let encodedBody = try JSONSerialization.data(withJSONObject: body, options: [])
1416

@@ -25,7 +27,9 @@ extension Twift {
2527
/// - tweetId: The ID of the Tweet that you would like the `userId` to unlike.
2628
/// - 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.
2729
/// - Returns: A response object containing a ``LikeResponse``
28-
public func unlikeTweet(_ tweetId: Tweet.ID, userId: User.ID) async throws -> TwitterAPIData<LikeResponse> {
30+
public func unlikeTweet(_ tweetId: Tweet.ID, userId: User.ID? = nil) async throws -> TwitterAPIData<LikeResponse> {
31+
guard let userId = userId ?? authenticatedUserId else { throw TwiftError.MissingUserID }
32+
2933
return try await call(route: .deleteUserLikes(userId, tweetId: tweetId),
3034
method: .DELETE,
3135
expectedReturnType: TwitterAPIData.self)
@@ -61,12 +65,14 @@ extension Twift {
6165
/// - 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.
6266
/// - 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.
6367
/// - Returns: A response object containing an array of Tweets liked by the target User
64-
public func getLikedTweets(for userId: User.ID,
65-
fields: Set<Tweet.Field> = [],
66-
expansions: [Tweet.Expansions],
67-
paginationToken: String? = nil,
68-
maxResults: Int = 10
68+
public func getLikedTweets(for userId: User.ID? = nil,
69+
fields: Set<Tweet.Field> = [],
70+
expansions: [Tweet.Expansions],
71+
paginationToken: String? = nil,
72+
maxResults: Int = 10
6973
) async throws -> TwitterAPIDataAndIncludes<[Tweet], Tweet.Includes> {
74+
guard let userId = userId ?? authenticatedUserId else { throw TwiftError.MissingUserID }
75+
7076
switch maxResults {
7177
case 10...100:
7278
break

Sources/Twift+Lists.swift

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ extension Twift {
1414
/// - maxResults: The maximum number of results to fetch.
1515
/// - Returns: A response object containing an array of Tweets, included expansions, and meta data for pagination
1616
public func getListTweets(_ listId: List.ID,
17-
fields: Set<Tweet.Field>,
18-
expansions: [Tweet.Expansions],
19-
paginationToken: String? = nil,
20-
maxResults: Int = 100
17+
fields: Set<Tweet.Field>,
18+
expansions: [Tweet.Expansions],
19+
paginationToken: String? = nil,
20+
maxResults: Int = 100
2121
) async throws -> TwitterAPIDataIncludesAndMeta<[Tweet], Tweet.Includes, Meta> {
2222
switch maxResults {
2323
case 1...100:
@@ -68,12 +68,14 @@ extension Twift {
6868
/// - paginationToken: When iterating over pages of results, you can pass in the `nextToken` from the previously-returned value to get the next page of results
6969
/// - maxResults: The maximum number of results to fetch.
7070
/// - Returns: A response object containing an array of Lists owned by the user id, any requested expansions, and a meta object with pagination tokens
71-
public func getUserOwnedLists(_ userId: User.ID,
71+
public func getUserOwnedLists(_ userId: User.ID? = nil,
7272
fields: Set<List.Field>,
7373
expansions: [List.Expansions],
7474
paginationToken: String?,
7575
maxResults: Int = 100
7676
) async throws -> TwitterAPIDataIncludesAndMeta<[List], List.Includes, Meta> {
77+
guard let userId = userId ?? authenticatedUserId else { throw TwiftError.MissingUserID }
78+
7779
switch maxResults {
7880
case 1...100:
7981
break
@@ -116,12 +118,14 @@ extension Twift {
116118
/// - paginationToken: When iterating over pages of results, you can pass in the `nextToken` from the previously-returned value to get the next page of results
117119
/// - maxResults: The maximum number of results to fetch.
118120
/// - Returns: A response object containing an array of Lists the user is a member of, any expanded objects, and a meta object with pagination tokens.
119-
public func getListMemberships(for userId: User.ID,
121+
public func getListMemberships(for userId: User.ID? = nil,
120122
fields: Set<List.Field>,
121123
expansions: [List.Expansions],
122124
paginationToken: String?,
123125
maxResults: Int = 100
124126
) async throws -> TwitterAPIDataIncludesAndMeta<[List], List.Includes, Meta> {
127+
guard let userId = userId ?? authenticatedUserId else { throw TwiftError.MissingUserID }
128+
125129
switch maxResults {
126130
case 1...100:
127131
break
@@ -216,7 +220,9 @@ extension Twift {
216220
/// - listId: The ID of the List that you would like the user id to unfollow.
217221
/// - userId: The user ID who you are unfollowing a List on behalf of. It must match your own user ID or that of an authenticating user
218222
/// - Returns: A response object containing the result of the unfollow request
219-
public func unfollowList(_ listId: List.ID, userId: User.ID) async throws -> TwitterAPIData<FollowResponse> {
223+
public func unfollowList(_ listId: List.ID, userId: User.ID? = nil) async throws -> TwitterAPIData<FollowResponse> {
224+
guard let userId = userId ?? authenticatedUserId else { throw TwiftError.MissingUserID }
225+
220226
return try await call(route: .userFollowingLists(userId, listId: listId),
221227
method: .DELETE,
222228
expectedReturnType: TwitterAPIData.self)
@@ -227,7 +233,9 @@ extension Twift {
227233
/// - listId: The ID of the List that you would like the user id to follow.
228234
/// - userId: The user ID who you are following a List on behalf of. It must match your own user ID or that of an authenticating user
229235
/// - Returns: A response object containing the result of the follow request
230-
public func followList(_ listId: List.ID, userId: User.ID) async throws -> TwitterAPIData<FollowResponse> {
236+
public func followList(_ listId: List.ID, userId: User.ID? = nil) async throws -> TwitterAPIData<FollowResponse> {
237+
guard let userId = userId ?? authenticatedUserId else { throw TwiftError.MissingUserID }
238+
231239
return try await call(route: .userFollowingLists(userId),
232240
method: .POST,
233241
expectedReturnType: TwitterAPIData.self)
@@ -275,12 +283,14 @@ extension Twift {
275283
/// - paginationToken: When iterating over pages of results, you can pass in the `nextToken` from the previously-returned value to get the next page of results
276284
/// - maxResults: The maximum number of results to fetch.
277285
/// - Returns: A response object containing an array of lists followed by the user, any requested expansions, and a meta object with pagination information
278-
public func getFollowedLists(_ userId: User.ID,
286+
public func getFollowedLists(_ userId: User.ID? = nil,
279287
fields: Set<List.Field> = [],
280288
expansions: [List.Expansions],
281289
paginationToken: String? = nil,
282290
maxResults: Int = 100
283291
) async throws -> TwitterAPIDataIncludesAndMeta<[List], List.Includes, Meta> {
292+
guard let userId = userId ?? authenticatedUserId else { throw TwiftError.MissingUserID }
293+
284294
switch maxResults {
285295
case 1...100:
286296
break
@@ -311,7 +321,9 @@ extension Twift {
311321
/// - listId: The ID of the List that you would like the user id to pin.
312322
/// - userId: The user ID who you are pinning a List on behalf of. It must match your own user ID or that of an authenticating user
313323
/// - Returns: A response object containing the result of this pin list request
314-
public func pinList(_ listId: List.ID, userId: User.ID) async throws -> TwitterAPIData<PinnedResponse> {
324+
public func pinList(_ listId: List.ID, userId: User.ID? = nil) async throws -> TwitterAPIData<PinnedResponse> {
325+
guard let userId = userId ?? authenticatedUserId else { throw TwiftError.MissingUserID }
326+
315327
let body = ["list_id": listId]
316328
let serializedBody = try JSONSerialization.data(withJSONObject: body)
317329
return try await call(route: .userPinnedLists(userId),
@@ -327,7 +339,9 @@ extension Twift {
327339
/// - listId: The ID of the List that you would like the user id to unpin.
328340
/// - userId: The user ID who you are unpinning a List on behalf of. It must match your own user ID or that of an authenticating user
329341
/// - Returns: A response object containing the result of this unpin list request
330-
public func unpinList(_ listId: List.ID, userId: User.ID) async throws -> TwitterAPIData<PinnedResponse> {
342+
public func unpinList(_ listId: List.ID, userId: User.ID? = nil) async throws -> TwitterAPIData<PinnedResponse> {
343+
guard let userId = userId ?? authenticatedUserId else { throw TwiftError.MissingUserID }
344+
331345
return try await call(route: .userPinnedLists(userId, listId: listId),
332346
method: .DELETE,
333347
expectedReturnType: TwitterAPIData.self)
@@ -339,10 +353,12 @@ extension Twift {
339353
/// - fields: Any additional fields to include on returned objects
340354
/// - expansions: Objects and their corresponding fields that should be expanded in the `includes` property
341355
/// - Returns: A response object containing an array of lists pinned by the user, any requested expansions, and a meta object with pagination information
342-
public func getPinnedLists(_ userId: User.ID,
356+
public func getPinnedLists(_ userId: User.ID? = nil,
343357
fields: Set<List.Field> = [],
344358
expansions: [List.Expansions]
345359
) async throws -> TwitterAPIDataAndIncludes<[List], List.Includes> {
360+
guard let userId = userId ?? authenticatedUserId else { throw TwiftError.MissingUserID }
361+
346362
return try await call(route: .userPinnedLists(userId),
347363
method: .GET,
348364
queryItems: fieldsAndExpansions(for: List.self, fields: fields, expansions: expansions),

0 commit comments

Comments
 (0)