This repository has been archived by the owner on Jun 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 25
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 #114 from mironal/bookmark-api
Add support Bookmarks API
- Loading branch information
Showing
8 changed files
with
267 additions
and
0 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
Sources/TwitterAPIKit/APIv2/Bookmarks/BookmarksAPIv2.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,34 @@ | ||
import Foundation | ||
|
||
public protocol BookmarksAPIv2 { | ||
|
||
/// https://developer.twitter.com/en/docs/twitter-api/tweets/bookmarks/api-reference/get-users-id-bookmarks | ||
func getBookmarks( | ||
_ request: GetUsersBookmarksRequestV2 | ||
) -> TwitterAPISessionJSONTask | ||
|
||
/// https://developer.twitter.com/en/docs/twitter-api/tweets/bookmarks/api-reference/post-users-id-bookmarks | ||
func createBookmark( | ||
_ request: PostUsersBookmarksRequestV2 | ||
) -> TwitterAPISessionJSONTask | ||
|
||
/// https://developer.twitter.com/en/docs/twitter-api/tweets/bookmarks/api-reference/delete-users-id-bookmarks-tweet_id | ||
func deleteBookmark( | ||
_ request: DeleteUsersBookmarksRequestV2 | ||
) -> TwitterAPISessionJSONTask | ||
} | ||
|
||
extension TwitterAPIKit.TwitterAPIImplV2: BookmarksAPIv2 { | ||
|
||
func getBookmarks(_ request: GetUsersBookmarksRequestV2) -> TwitterAPISessionJSONTask { | ||
return session.send(request) | ||
} | ||
|
||
func createBookmark(_ request: PostUsersBookmarksRequestV2) -> TwitterAPISessionJSONTask { | ||
return session.send(request) | ||
} | ||
|
||
func deleteBookmark(_ request: DeleteUsersBookmarksRequestV2) -> TwitterAPISessionJSONTask { | ||
return session.send(request) | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
Sources/TwitterAPIKit/APIv2/Bookmarks/Requests/DeleteUsersBookmarksRequestV2.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,29 @@ | ||
import Foundation | ||
|
||
/// https://developer.twitter.com/en/docs/twitter-api/tweets/bookmarks/api-reference/delete-users-id-bookmarks-tweet_id | ||
open class DeleteUsersBookmarksRequestV2: TwitterAPIRequest { | ||
|
||
/// The user ID of an authenticated user who you are removing a Bookmark of a Tweet on behalf of. It must match your own user ID or that of an authenticating user, meaning that you must pass the Access Tokens associated with the user ID when authenticating your request. | ||
public let id: String | ||
public let tweetID: String | ||
|
||
public var method: HTTPMethod { | ||
return .delete | ||
} | ||
|
||
public var path: String { | ||
return "/2/users/\(id)/bookmarks/\(tweetID)" | ||
} | ||
|
||
open var parameters: [String: Any] { | ||
return [:] | ||
} | ||
|
||
public init( | ||
id: String, | ||
tweetID: String | ||
) { | ||
self.id = id | ||
self.tweetID = tweetID | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
Sources/TwitterAPIKit/APIv2/Bookmarks/Requests/GetUsersBookmarksRequestV2.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,59 @@ | ||
import Foundation | ||
|
||
/// https://developer.twitter.com/en/docs/twitter-api/tweets/bookmarks/api-reference/get-users-id-bookmarks | ||
open class GetUsersBookmarksRequestV2: TwitterAPIRequest { | ||
|
||
/// User ID of an authenticated user to request bookmarked Tweets for. | ||
public let id: String | ||
public let expansions: Set<TwitterTweetExpansionsV2>? | ||
public let maxResults: Int? | ||
public let mediaFields: Set<TwitterMediaFieldsV2>? | ||
public let paginationToken: String? | ||
public let placeFields: Set<TwitterPlaceFieldsV2>? | ||
public let pollFields: Set<TwitterPollFieldsV2>? | ||
public let tweetFields: Set<TwitterTweetFieldsV2>? | ||
public let userFields: Set<TwitterUserFieldsV2>? | ||
|
||
public var method: HTTPMethod { | ||
return .get | ||
} | ||
|
||
public var path: String { | ||
return "/2/users/\(id)/bookmarks" | ||
} | ||
|
||
open var parameters: [String: Any] { | ||
var p = [String: Any]() | ||
expansions?.bind(param: &p) | ||
maxResults.map { p["max_results"] = $0 } | ||
mediaFields?.bind(param: &p) | ||
paginationToken.map { p["pagination_token"] = $0 } | ||
placeFields?.bind(param: &p) | ||
pollFields?.bind(param: &p) | ||
tweetFields?.bind(param: &p) | ||
userFields?.bind(param: &p) | ||
return p | ||
} | ||
|
||
public init( | ||
id: String, | ||
expansions: Set<TwitterTweetExpansionsV2>? = .none, | ||
maxResults: Int? = .none, | ||
mediaFields: Set<TwitterMediaFieldsV2>? = .none, | ||
paginationToken: String? = .none, | ||
placeFields: Set<TwitterPlaceFieldsV2>? = .none, | ||
pollFields: Set<TwitterPollFieldsV2>? = .none, | ||
tweetFields: Set<TwitterTweetFieldsV2>? = .none, | ||
userFields: Set<TwitterUserFieldsV2>? = .none | ||
) { | ||
self.id = id | ||
self.expansions = expansions | ||
self.maxResults = maxResults | ||
self.mediaFields = mediaFields | ||
self.paginationToken = paginationToken | ||
self.placeFields = placeFields | ||
self.pollFields = pollFields | ||
self.tweetFields = tweetFields | ||
self.userFields = userFields | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
Sources/TwitterAPIKit/APIv2/Bookmarks/Requests/PostUsersBookmarksRequestV2.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,35 @@ | ||
import Foundation | ||
|
||
/// https://developer.twitter.com/en/docs/twitter-api/tweets/bookmarks/api-reference/post-users-id-bookmarks | ||
open class PostUsersBookmarksRequestV2: TwitterAPIRequest { | ||
|
||
/// The user ID who you are bookmarking a Tweet on behalf of. It must match your own user ID or that of an authenticating user, meaning that you must pass the Access Token associated with the user ID when authenticating your request. | ||
public let id: String | ||
public let tweetID: String | ||
|
||
public var method: HTTPMethod { | ||
return .post | ||
} | ||
|
||
public var path: String { | ||
return "/2/users/\(id)/bookmarks" | ||
} | ||
|
||
public var bodyContentType: BodyContentType { | ||
return .json | ||
} | ||
|
||
open var parameters: [String: Any] { | ||
var p = [String: Any]() | ||
p["tweet_id"] = tweetID | ||
return p | ||
} | ||
|
||
public init( | ||
id: String, | ||
tweetID: String | ||
) { | ||
self.id = id | ||
self.tweetID = tweetID | ||
} | ||
} |
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
26 changes: 26 additions & 0 deletions
26
Tests/TwitterAPIKitTests/APIv2/Bookmarks/DeleteUsersBookmarksRequestV2Tests.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,26 @@ | ||
import TwitterAPIKit | ||
import XCTest | ||
|
||
class DeleteUsersBookmarksRequestV2Tests: XCTestCase { | ||
override func setUpWithError() throws { | ||
} | ||
|
||
override func tearDownWithError() throws { | ||
} | ||
|
||
func test() throws { | ||
let req = DeleteUsersBookmarksRequestV2( | ||
id: "_i_", | ||
tweetID: "_t_" | ||
) | ||
|
||
XCTAssertEqual(req.method, .delete) | ||
XCTAssertEqual(req.baseURLType, .api) | ||
XCTAssertEqual(req.path, "/2/users/_i_/bookmarks/_t_") | ||
XCTAssertEqual(req.bodyContentType, .wwwFormUrlEncoded) | ||
AssertEqualAnyDict( | ||
req.parameters, | ||
[:] | ||
) | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
Tests/TwitterAPIKitTests/APIv2/Bookmarks/GetUsersBookmarksRequestV2Tests.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,53 @@ | ||
import TwitterAPIKit | ||
import XCTest | ||
|
||
class GetUsersBookmarksRequestV2Tests: XCTestCase { | ||
override func setUpWithError() throws { | ||
} | ||
|
||
override func tearDownWithError() throws { | ||
} | ||
|
||
func test() throws { | ||
let req = GetUsersBookmarksRequestV2( | ||
id: "_i_", | ||
expansions: [.authorID], | ||
maxResults: 12, | ||
mediaFields: [.height], | ||
paginationToken: "_p_", | ||
placeFields: [.name], | ||
pollFields: [.options], | ||
tweetFields: [.text], | ||
userFields: [.entities] | ||
) | ||
|
||
XCTAssertEqual(req.method, .get) | ||
XCTAssertEqual(req.baseURLType, .api) | ||
XCTAssertEqual(req.path, "/2/users/_i_/bookmarks") | ||
XCTAssertEqual(req.bodyContentType, .wwwFormUrlEncoded) | ||
AssertEqualAnyDict( | ||
req.parameters, | ||
[ | ||
"expansions": "author_id", | ||
"max_results": 12, | ||
"media.fields": "height", | ||
"pagination_token": "_p_", | ||
"place.fields": "name", | ||
"poll.fields": "options", | ||
"tweet.fields": "text", | ||
"user.fields": "entities", | ||
] | ||
) | ||
} | ||
|
||
func testDefaultArg() throws { | ||
let req = GetUsersBookmarksRequestV2( | ||
id: "_i_" | ||
) | ||
|
||
AssertEqualAnyDict( | ||
req.parameters, | ||
[:] | ||
) | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
Tests/TwitterAPIKitTests/APIv2/Bookmarks/PostUsersBookmarksRequestV2Tests.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 @@ | ||
import TwitterAPIKit | ||
import XCTest | ||
|
||
class PostUsersBookmarksRequestV2Tests: XCTestCase { | ||
override func setUpWithError() throws { | ||
} | ||
|
||
override func tearDownWithError() throws { | ||
} | ||
|
||
func test() throws { | ||
let req = PostUsersBookmarksRequestV2( | ||
id: "_i_", | ||
tweetID: "_t_" | ||
) | ||
|
||
XCTAssertEqual(req.method, .post) | ||
XCTAssertEqual(req.baseURLType, .api) | ||
XCTAssertEqual(req.path, "/2/users/_i_/bookmarks") | ||
XCTAssertEqual(req.bodyContentType, .json) | ||
AssertEqualAnyDict( | ||
req.parameters, | ||
[ | ||
"tweet_id": "_t_" | ||
] | ||
) | ||
} | ||
} |