diff --git a/.github/workflows/swift.yml b/.github/workflows/swift.yml index 1a8edb5..bcf97db 100644 --- a/.github/workflows/swift.yml +++ b/.github/workflows/swift.yml @@ -8,7 +8,7 @@ on: jobs: build: - runs-on: macos-latest + runs-on: macos-12 steps: - uses: actions/checkout@v2 - uses: actions/cache@v2 diff --git a/Sources/TwitterAPIKit/APIv2/DirectMessage/DirectMessageAPIv2.swift b/Sources/TwitterAPIKit/APIv2/DirectMessage/DirectMessageAPIv2.swift new file mode 100644 index 0000000..20d8b5d --- /dev/null +++ b/Sources/TwitterAPIKit/APIv2/DirectMessage/DirectMessageAPIv2.swift @@ -0,0 +1,45 @@ +import Foundation + +open class DirectMessageAPIv2: TwitterAPIBase { + /// https://developer.twitter.com/en/docs/twitter-api/direct-messages/lookup/api-reference/get-dm_events + public func getDmEvents( + _ request: GetDmEventsRequestV2 + ) -> TwitterAPISessionJSONTask { + return session.send(request) + } + + /// https://developer.twitter.com/en/docs/twitter-api/direct-messages/lookup/api-reference/get-dm_conversations-with-participant_id-dm_events + public func getDmEventsWithParticipantId( + _ request: GetDmConversationsWithParticipantIdDmEventsRequestV2 + ) -> TwitterAPISessionJSONTask { + return session.send(request) + } + + /// https://developer.twitter.com/en/docs/twitter-api/direct-messages/lookup/api-reference/get-dm_conversations-dm_conversation_id-dm_events + public func getDmEventsByConversationsId( + _ request: GetDmConversationsIdDmEventsRequestV2 + ) -> TwitterAPISessionJSONTask { + return session.send(request) + } + + /// https://developer.twitter.com/en/docs/twitter-api/direct-messages/manage/api-reference/post-dm_conversations-dm_conversation_id-messages + public func postDmConversationById( + _ request: PostDmConversationByIdRequestV2 + ) -> TwitterAPISessionJSONTask { + return session.send(request) + } + + /// https://developer.twitter.com/en/docs/twitter-api/direct-messages/manage/api-reference/post-dm_conversations-with-participant_id-messages + public func postDmConversationWithUser( + _ request: PostDmConversationWithUserRequestV2 + ) -> TwitterAPISessionJSONTask { + return session.send(request) + } + + /// https://developer.twitter.com/en/docs/twitter-api/direct-messages/manage/api-reference/post-dm_conversations + public func postDmConversation( + _ request: PostDmConversationRequestV2 + ) -> TwitterAPISessionJSONTask { + return session.send(request) + } +} diff --git a/Sources/TwitterAPIKit/APIv2/DirectMessage/Requests/GetDmConversationsIdDmEventsRequestV2.swift b/Sources/TwitterAPIKit/APIv2/DirectMessage/Requests/GetDmConversationsIdDmEventsRequestV2.swift new file mode 100644 index 0000000..46de57f --- /dev/null +++ b/Sources/TwitterAPIKit/APIv2/DirectMessage/Requests/GetDmConversationsIdDmEventsRequestV2.swift @@ -0,0 +1,68 @@ +import Foundation + +/// Returns DM Events for a DM Conversation +/// Required OAuth 2.0 scopes: dm.read, tweet.read, users.read +open class GetDmConversationsIdDmEventsRequestV2: TwitterAPIRequest { + + /// The DM Conversation ID. + public let id: String + /// The maximum number of results. + public let maxResults: Int? + /// This parameter is used to get a specified 'page' of results. + public let paginationToken: String? + /// The set of event_types to include in the results. + public let eventTypes: Set? + /// A comma separated list of DmEvent fields to display. + public let dmEventFields: Set? + /// A comma separated list of fields to expand. + public let expansions: Set? + /// A comma separated list of Media fields to display. + public let mediaFields: Set? + /// A comma separated list of User fields to display. + public let userFields: Set? + /// A comma separated list of Tweet fields to display. + public let tweetFields: Set? + + public var method: HTTPMethod { + return .get + } + + public var path: String { + return "/2/dm_conversations/\(id)/dm_events" + } + + open var parameters: [String: Any] { + var p = [String: Any]() + maxResults.map { p["max_results"] = $0 } + paginationToken.map { p["pagination_token"] = $0 } + eventTypes?.bind(param: &p) + dmEventFields?.bind(param: &p) + expansions?.bind(param: &p) + mediaFields?.bind(param: &p) + userFields?.bind(param: &p) + tweetFields?.bind(param: &p) + return p + } + + public init( + id: String, + maxResults: Int? = .none, + paginationToken: String? = .none, + eventTypes: Set? = .none, + dmEventFields: Set? = .none, + expansions: Set? = .none, + mediaFields: Set? = .none, + userFields: Set? = .none, + tweetFields: Set? = .none + ) { + self.id = id + self.maxResults = maxResults + self.paginationToken = paginationToken + self.eventTypes = eventTypes + self.dmEventFields = dmEventFields + self.expansions = expansions + self.mediaFields = mediaFields + self.userFields = userFields + self.tweetFields = tweetFields + } +} diff --git a/Sources/TwitterAPIKit/APIv2/DirectMessage/Requests/GetDmConversationsWithParticipantIdDmEventsRequestV2.swift b/Sources/TwitterAPIKit/APIv2/DirectMessage/Requests/GetDmConversationsWithParticipantIdDmEventsRequestV2.swift new file mode 100644 index 0000000..f494d99 --- /dev/null +++ b/Sources/TwitterAPIKit/APIv2/DirectMessage/Requests/GetDmConversationsWithParticipantIdDmEventsRequestV2.swift @@ -0,0 +1,68 @@ +import Foundation + +/// Returns DM Events for a DM Conversation +/// Required OAuth 2.0 scopes: dm.read, tweet.read, users.read +open class GetDmConversationsWithParticipantIdDmEventsRequestV2: TwitterAPIRequest { + + /// The ID of the participant user for the One to One DM conversation. + public let participantID: String + /// The maximum number of results. + public let maxResults: Int? + /// This parameter is used to get a specified 'page' of results. + public let paginationToken: String? + /// The set of event_types to include in the results. + public let eventTypes: Set? + /// A comma separated list of DmEvent fields to display. + public let dmEventFields: Set? + /// A comma separated list of fields to expand. + public let expansions: Set? + /// A comma separated list of Media fields to display. + public let mediaFields: Set? + /// A comma separated list of User fields to display. + public let userFields: Set? + /// A comma separated list of Tweet fields to display. + public let tweetFields: Set? + + public var method: HTTPMethod { + return .get + } + + public var path: String { + return "/2/dm_conversations/with/\(participantID)/dm_events" + } + + open var parameters: [String: Any] { + var p = [String: Any]() + maxResults.map { p["max_results"] = $0 } + paginationToken.map { p["pagination_token"] = $0 } + eventTypes?.bind(param: &p) + dmEventFields?.bind(param: &p) + expansions?.bind(param: &p) + mediaFields?.bind(param: &p) + userFields?.bind(param: &p) + tweetFields?.bind(param: &p) + return p + } + + public init( + participantID: String, + maxResults: Int? = .none, + paginationToken: String? = .none, + eventTypes: Set? = .none, + dmEventFields: Set? = .none, + expansions: Set? = .none, + mediaFields: Set? = .none, + userFields: Set? = .none, + tweetFields: Set? = .none + ) { + self.participantID = participantID + self.maxResults = maxResults + self.paginationToken = paginationToken + self.eventTypes = eventTypes + self.dmEventFields = dmEventFields + self.expansions = expansions + self.mediaFields = mediaFields + self.userFields = userFields + self.tweetFields = tweetFields + } +} diff --git a/Sources/TwitterAPIKit/APIv2/DirectMessage/Requests/GetDmEventsRequestV2.swift b/Sources/TwitterAPIKit/APIv2/DirectMessage/Requests/GetDmEventsRequestV2.swift new file mode 100644 index 0000000..c2bbdcc --- /dev/null +++ b/Sources/TwitterAPIKit/APIv2/DirectMessage/Requests/GetDmEventsRequestV2.swift @@ -0,0 +1,64 @@ +import Foundation + +/// Returns recent DM Events across DM conversations +/// Required OAuth 2.0 scopes: dm.read, tweet.read, users.read +open class GetDmEventsRequestV2: TwitterAPIRequest { + + /// The maximum number of results. + public let maxResults: Int? + /// This parameter is used to get a specified 'page' of results. + public let paginationToken: String? + /// The set of event_types to include in the results. + public let eventTypes: Set? + /// A comma separated list of DmEvent fields to display. + public let dmEventFields: Set? + /// A comma separated list of fields to expand. + public let expansions: Set? + /// A comma separated list of Media fields to display. + public let mediaFields: Set? + /// A comma separated list of User fields to display. + public let userFields: Set? + /// A comma separated list of Tweet fields to display. + public let tweetFields: Set? + + public var method: HTTPMethod { + return .get + } + + public var path: String { + return "/2/dm_events" + } + + open var parameters: [String: Any] { + var p = [String: Any]() + maxResults.map { p["max_results"] = $0 } + paginationToken.map { p["pagination_token"] = $0 } + eventTypes?.bind(param: &p) + dmEventFields?.bind(param: &p) + expansions?.bind(param: &p) + mediaFields?.bind(param: &p) + userFields?.bind(param: &p) + tweetFields?.bind(param: &p) + return p + } + + public init( + maxResults: Int? = .none, + paginationToken: String? = .none, + eventTypes: Set? = .none, + dmEventFields: Set? = .none, + expansions: Set? = .none, + mediaFields: Set? = .none, + userFields: Set? = .none, + tweetFields: Set? = .none + ) { + self.maxResults = maxResults + self.paginationToken = paginationToken + self.eventTypes = eventTypes + self.dmEventFields = dmEventFields + self.expansions = expansions + self.mediaFields = mediaFields + self.userFields = userFields + self.tweetFields = tweetFields + } +} diff --git a/Sources/TwitterAPIKit/APIv2/DirectMessage/Requests/PostDmConversationByIdRequestV2.swift b/Sources/TwitterAPIKit/APIv2/DirectMessage/Requests/PostDmConversationByIdRequestV2.swift new file mode 100644 index 0000000..8c4e984 --- /dev/null +++ b/Sources/TwitterAPIKit/APIv2/DirectMessage/Requests/PostDmConversationByIdRequestV2.swift @@ -0,0 +1,44 @@ +import Foundation + +/// Creates a new message for a DM Conversation specified by DM Conversation ID +/// Required OAuth 2.0 scopes: dm.write, tweet.read, users.read +open class PostDmConversationByIdRequestV2: TwitterAPIRequest { + + /// The DM Conversation ID. + public let dmConversationID: String + /// Attachments to a DM Event. + public let attachments: [String]? + /// Text of the message. + public let text: String? + + public var method: HTTPMethod { + return .post + } + + public var path: String { + return "/2/dm_conversations/\(dmConversationID)/messages" + } + + public var bodyContentType: BodyContentType { + return .json + } + + open var parameters: [String: Any] { + var p = [String: Any]() + if let attachments = attachments { + p["attachments"] = attachments.map { ["media_id": $0] } + } + text.map { p["text"] = $0 } + return p + } + + public init( + dmConversationID: String, + attachments: [String]? = .none, + text: String? = .none + ) { + self.dmConversationID = dmConversationID + self.attachments = attachments + self.text = text + } +} diff --git a/Sources/TwitterAPIKit/APIv2/DirectMessage/Requests/PostDmConversationRequestV2.swift b/Sources/TwitterAPIKit/APIv2/DirectMessage/Requests/PostDmConversationRequestV2.swift new file mode 100644 index 0000000..cd7bb50 --- /dev/null +++ b/Sources/TwitterAPIKit/APIv2/DirectMessage/Requests/PostDmConversationRequestV2.swift @@ -0,0 +1,57 @@ +import Foundation + +/// Creates a new DM Conversation. +/// Required OAuth 2.0 scopes: dm.write, tweet.read, users.read +open class PostDmConversationRequestV2: TwitterAPIRequest { + + /// The conversation type that is being created. + public enum ConversationType: String { + case group = "Group" + } + + /// The conversation type that is being created. + public let conversationType: ConversationType + /// Participants for the DM Conversation. + public let participantIDs: [String] + /// Attachments to a DM Event. + public let attachments: [String]? + /// Text of the message. + public let text: String? + + public var method: HTTPMethod { + return .post + } + + public var path: String { + return "/2/dm_conversations" + } + + public var bodyContentType: BodyContentType { + return .json + } + + open var parameters: [String: Any] { + var p = [String: Any]() + p["conversation_type"] = conversationType.rawValue + p["participant_ids"] = participantIDs + var message = [String: Any]() + text.map { message["text"] = $0 } + if let attachments = attachments { + message["attachments"] = attachments.map { ["media_id": $0] } + } + p["message"] = message + return p + } + + public init( + conversationType: ConversationType, + participantIDs: [String], + attachments: [String]? = .none, + text: String? = .none + ) { + self.conversationType = conversationType + self.participantIDs = participantIDs + self.attachments = attachments + self.text = text + } +} diff --git a/Sources/TwitterAPIKit/APIv2/DirectMessage/Requests/PostDmConversationWithUserRequestV2.swift b/Sources/TwitterAPIKit/APIv2/DirectMessage/Requests/PostDmConversationWithUserRequestV2.swift new file mode 100644 index 0000000..fe79daf --- /dev/null +++ b/Sources/TwitterAPIKit/APIv2/DirectMessage/Requests/PostDmConversationWithUserRequestV2.swift @@ -0,0 +1,44 @@ +import Foundation + +/// Creates a new message for a DM Conversation with a participant user by ID +/// Required OAuth 2.0 scopes: dm.write, tweet.read, users.read +open class PostDmConversationWithUserRequestV2: TwitterAPIRequest { + + /// The ID of the recipient user that will receive the DM. + public let participantID: String + /// Attachments to a DM Event. + public let attachments: [String]? + /// Text of the message. + public let text: String? + + public var method: HTTPMethod { + return .post + } + + public var path: String { + return "/2/dm_conversations/with/\(participantID)/messages" + } + + public var bodyContentType: BodyContentType { + return .json + } + + open var parameters: [String: Any] { + var p = [String: Any]() + if let attachments = attachments { + p["attachments"] = attachments.map { ["media_id": $0] } + } + text.map { p["text"] = $0 } + return p + } + + public init( + participantID: String, + attachments: [String]? = .none, + text: String? = .none + ) { + self.participantID = participantID + self.attachments = attachments + self.text = text + } +} diff --git a/Sources/TwitterAPIKit/APIv2/DirectMessage/TwitterDirectMessageEventTypeV2.swift b/Sources/TwitterAPIKit/APIv2/DirectMessage/TwitterDirectMessageEventTypeV2.swift new file mode 100644 index 0000000..cc5ee32 --- /dev/null +++ b/Sources/TwitterAPIKit/APIv2/DirectMessage/TwitterDirectMessageEventTypeV2.swift @@ -0,0 +1,18 @@ +import Foundation + +/// The set of event_types to include in the results. +public enum TwitterDirectMessageEventTypeV2: String { + case messageCreate = "MessageCreate" + case participantsJoin = "ParticipantsJoin" + case participantsLeave = "ParticipantsLeave" +} + +extension TwitterDirectMessageEventTypeV2: TwitterAPIv2RequestParameter { + public var stringValue: String { return rawValue } +} + +extension Set where Element == TwitterDirectMessageEventTypeV2 { + func bind(param: inout [String: Any]) { + param["event_types"] = commaSeparatedString + } +} diff --git a/Sources/TwitterAPIKit/APIv2/ExpansionsV2.swift b/Sources/TwitterAPIKit/APIv2/ExpansionsV2.swift index 07117c9..a7213ff 100644 --- a/Sources/TwitterAPIKit/APIv2/ExpansionsV2.swift +++ b/Sources/TwitterAPIKit/APIv2/ExpansionsV2.swift @@ -108,3 +108,30 @@ public enum TwitterSpaceExpansionsV2: TwitterExpansionsParameterV2, Hashable { .hostIDs, ] } + +/// A comma separated list of fields to expand. +/// expansions +public enum TwitterDmEventExpansionsV2: TwitterExpansionsParameterV2, Hashable { + case attachmentsMediaKeys + case participantIDs + case referencedTweetsID + case senderID + case other(String) + + public var stringValue: String { + switch self { + case .attachmentsMediaKeys: return "attachments.media_keys" + case .participantIDs: return "participant_ids" + case .referencedTweetsID: return "referenced_tweets.id" + case .senderID: return "sender_id" + case .other(let string): return string + } + } + + public static let all: Set = [ + .attachmentsMediaKeys, + .participantIDs, + .referencedTweetsID, + .senderID, + ] +} diff --git a/Sources/TwitterAPIKit/APIv2/FieldsV2.swift b/Sources/TwitterAPIKit/APIv2/FieldsV2.swift index 2bb67d0..5ed79d6 100644 --- a/Sources/TwitterAPIKit/APIv2/FieldsV2.swift +++ b/Sources/TwitterAPIKit/APIv2/FieldsV2.swift @@ -482,3 +482,75 @@ extension Set where Element == TwitterTopicFieldsV2 { param["topic.fields"] = commaSeparatedString } } + +/// A comma separated list of DmEvent fields to display. +/// dm_event.fields +public enum TwitterDmEventFieldsV2: TwitterAPIv2RequestParameter, Hashable { + case attachments + case createdAt + case dmConversationID + case eventType + case id + case participantIDs + case referencedTweets + case senderID + case text + case other(String) + + public var stringValue: String { + switch self { + case .attachments: return "attachments" + case .createdAt: return "created_at" + case .dmConversationID: return "dm_conversation_id" + case .eventType: return "event_type" + case .id: return "id" + case .participantIDs: return "participant_ids" + case .referencedTweets: return "referenced_tweets" + case .senderID: return "sender_id" + case .text: return "text" + case .other(let string): return string + } + } + + public static let all: Set = [ + .attachments, + .createdAt, + .dmConversationID, + .eventType, + .id, + .participantIDs, + .referencedTweets, + .senderID, + .text, + ] +} + +extension Set where Element == TwitterDmEventFieldsV2 { + func bind(param: inout [String: Any]) { + param["dm_event.fields"] = commaSeparatedString + } +} + +/// A comma separated list of DmConversation fields to display. +/// dm_conversation.fields +public enum TwitterDmConversationFieldsV2: TwitterAPIv2RequestParameter, Hashable { + case id + case other(String) + + public var stringValue: String { + switch self { + case .id: return "id" + case .other(let string): return string + } + } + + public static let all: Set = [ + .id + ] +} + +extension Set where Element == TwitterDmConversationFieldsV2 { + func bind(param: inout [String: Any]) { + param["dm_conversation.fields"] = commaSeparatedString + } +} diff --git a/Sources/TwitterAPIKit/APIv2/TwitterAPIv2.swift b/Sources/TwitterAPIKit/APIv2/TwitterAPIv2.swift index 74e12b7..a3fc517 100644 --- a/Sources/TwitterAPIKit/APIv2/TwitterAPIv2.swift +++ b/Sources/TwitterAPIKit/APIv2/TwitterAPIv2.swift @@ -17,6 +17,7 @@ open class TwitterAPIv2 { public let tweet: TweetAPIv2 public let tweetCount: TweetCountAPIv2 public let user: UserAPIv2 + public let dm: DirectMessageAPIv2 public init(session: TwitterAPISession) { blockAndMute = .init(session: session) @@ -33,6 +34,7 @@ open class TwitterAPIv2 { tweet = .init(session: session) tweetCount = .init(session: session) user = .init(session: session) + dm = .init(session: session) } } diff --git a/Sources/TwitterAPIKit/TwitterAPI+Flat.generated.swift b/Sources/TwitterAPIKit/TwitterAPI+Flat.generated.swift index 354bbe1..6272406 100644 --- a/Sources/TwitterAPIKit/TwitterAPI+Flat.generated.swift +++ b/Sources/TwitterAPIKit/TwitterAPI+Flat.generated.swift @@ -1,4 +1,4 @@ -// Generated using Sourcery 1.8.2 — https://github.com/krzysztofzablocki/Sourcery +// Generated using Sourcery 1.9.0 — https://github.com/krzysztofzablocki/Sourcery // DO NOT EDIT // swift-format-ignore-file @@ -677,4 +677,25 @@ extension TwitterAPIv2 { public func getMe(_ request: GetUsersMeRequestV2) -> TwitterAPISessionJSONTask { return user.getMe(request) } + + // MARK: - DirectMessageAPIv2 + + public func getDmEvents(_ request: GetDmEventsRequestV2) -> TwitterAPISessionJSONTask { + return dm.getDmEvents(request) + } + public func getDmEventsWithParticipantId(_ request: GetDmConversationsWithParticipantIdDmEventsRequestV2) -> TwitterAPISessionJSONTask { + return dm.getDmEventsWithParticipantId(request) + } + public func getDmEventsByConversationsId(_ request: GetDmConversationsIdDmEventsRequestV2) -> TwitterAPISessionJSONTask { + return dm.getDmEventsByConversationsId(request) + } + public func postDmConversationById(_ request: PostDmConversationByIdRequestV2) -> TwitterAPISessionJSONTask { + return dm.postDmConversationById(request) + } + public func postDmConversationWithUser(_ request: PostDmConversationWithUserRequestV2) -> TwitterAPISessionJSONTask { + return dm.postDmConversationWithUser(request) + } + public func postDmConversation(_ request: PostDmConversationRequestV2) -> TwitterAPISessionJSONTask { + return dm.postDmConversation(request) + } } diff --git a/Tests/TwitterAPIKitTests/APIv2/DirectMessage/GetDmConversationsIdDmEventsRequestV2Tests.swift b/Tests/TwitterAPIKitTests/APIv2/DirectMessage/GetDmConversationsIdDmEventsRequestV2Tests.swift new file mode 100644 index 0000000..192f10f --- /dev/null +++ b/Tests/TwitterAPIKitTests/APIv2/DirectMessage/GetDmConversationsIdDmEventsRequestV2Tests.swift @@ -0,0 +1,53 @@ +import TwitterAPIKit +import XCTest + +class GetDmConversationsIdDmEventsRequestV2Tests: XCTestCase { + override func setUpWithError() throws { + } + + override func tearDownWithError() throws { + } + + func test() throws { + let req = GetDmConversationsIdDmEventsRequestV2( + id: "_i_", + maxResults: 11, + paginationToken: "_p_", + eventTypes: [.messageCreate, .participantsJoin], + dmEventFields: [.senderID], + expansions: [.participantIDs], + mediaFields: [.durationMs], + userFields: [.id], + tweetFields: [.text] + ) + + XCTAssertEqual(req.method, .get) + XCTAssertEqual(req.baseURLType, .api) + XCTAssertEqual(req.path, "/2/dm_conversations/_i_/dm_events") + XCTAssertEqual(req.bodyContentType, .wwwFormUrlEncoded) + AssertEqualAnyDict( + req.parameters, + [ + "max_results": 11, + "pagination_token": "_p_", + "event_types": "MessageCreate,ParticipantsJoin", + "dm_event.fields": "sender_id", + "expansions": "participant_ids", + "media.fields": "duration_ms", + "user.fields": "id", + "tweet.fields": "text", + ] + ) + } + + func testDefaultArg() throws { + let req = GetDmConversationsIdDmEventsRequestV2( + id: "i" + ) + XCTAssertEqual(req.path, "/2/dm_conversations/i/dm_events") + AssertEqualAnyDict( + req.parameters, + [:] + ) + } +} diff --git a/Tests/TwitterAPIKitTests/APIv2/DirectMessage/GetDmConversationsWithParticipantIdDmEventsRequestV2Tests.swift b/Tests/TwitterAPIKitTests/APIv2/DirectMessage/GetDmConversationsWithParticipantIdDmEventsRequestV2Tests.swift new file mode 100644 index 0000000..1c3b4f9 --- /dev/null +++ b/Tests/TwitterAPIKitTests/APIv2/DirectMessage/GetDmConversationsWithParticipantIdDmEventsRequestV2Tests.swift @@ -0,0 +1,54 @@ +import TwitterAPIKit +import XCTest + +class GetDmConversationsWithParticipantIdDmEventsRequestV2Tests: XCTestCase { + override func setUpWithError() throws { + } + + override func tearDownWithError() throws { + } + + func test() throws { + let req = GetDmConversationsWithParticipantIdDmEventsRequestV2( + participantID: "_p_id", + maxResults: 12, + paginationToken: "_p_", + eventTypes: [.participantsLeave], + dmEventFields: [.eventType], + expansions: [.attachmentsMediaKeys], + mediaFields: [], + userFields: [], + tweetFields: [] + ) + + XCTAssertEqual(req.method, .get) + XCTAssertEqual(req.baseURLType, .api) + XCTAssertEqual(req.path, "/2/dm_conversations/with/_p_id/dm_events") + XCTAssertEqual(req.bodyContentType, .wwwFormUrlEncoded) + AssertEqualAnyDict( + req.parameters, + [ + "max_results": 12, + "pagination_token": "_p_", + "event_types": "ParticipantsLeave", + "dm_event.fields": "event_type", + "expansions": "attachments.media_keys", + "media.fields": "", + "user.fields": "", + "tweet.fields": "", + ] + ) + } + + func testDefaultArg() throws { + let req = GetDmConversationsWithParticipantIdDmEventsRequestV2( + participantID: "p" + ) + + XCTAssertEqual(req.path, "/2/dm_conversations/with/p/dm_events") + AssertEqualAnyDict( + req.parameters, + [:] + ) + } +} diff --git a/Tests/TwitterAPIKitTests/APIv2/DirectMessage/GetDmEventsRequestV2Tests.swift b/Tests/TwitterAPIKitTests/APIv2/DirectMessage/GetDmEventsRequestV2Tests.swift new file mode 100644 index 0000000..14f7f57 --- /dev/null +++ b/Tests/TwitterAPIKitTests/APIv2/DirectMessage/GetDmEventsRequestV2Tests.swift @@ -0,0 +1,41 @@ +import TwitterAPIKit +import XCTest + +class GetDmEventsRequestV2Tests: XCTestCase { + override func setUpWithError() throws { + } + + override func tearDownWithError() throws { + } + + func test() throws { + let req = GetDmEventsRequestV2( + maxResults: 10, + paginationToken: "_p_", + eventTypes: [], + dmEventFields: [.referencedTweets], + expansions: [.referencedTweetsID], + mediaFields: [.altText], + userFields: [.username], + tweetFields: [.lang] + ) + + XCTAssertEqual(req.method, .get) + XCTAssertEqual(req.baseURLType, .api) + XCTAssertEqual(req.path, "/2/dm_events") + XCTAssertEqual(req.bodyContentType, .wwwFormUrlEncoded) + AssertEqualAnyDict( + req.parameters, + [ + "max_results": 10, + "pagination_token": "_p_", + "event_types": "", + "dm_event.fields": "referenced_tweets", + "expansions": "referenced_tweets.id", + "media.fields": "alt_text", + "user.fields": "username", + "tweet.fields": "lang", + ] + ) + } +} diff --git a/Tests/TwitterAPIKitTests/APIv2/DirectMessage/PostDmConversationByIdRequestV2Tests.swift b/Tests/TwitterAPIKitTests/APIv2/DirectMessage/PostDmConversationByIdRequestV2Tests.swift new file mode 100644 index 0000000..839463a --- /dev/null +++ b/Tests/TwitterAPIKitTests/APIv2/DirectMessage/PostDmConversationByIdRequestV2Tests.swift @@ -0,0 +1,42 @@ +import TwitterAPIKit +import XCTest + +class PostDmConversationByIdRequestV2Tests: XCTestCase { + override func setUpWithError() throws { + } + + override func tearDownWithError() throws { + } + + func test() throws { + let req = PostDmConversationByIdRequestV2( + dmConversationID: "_d_id", + attachments: ["1", "2"], + text: "_t_" + ) + + XCTAssertEqual(req.method, .post) + XCTAssertEqual(req.baseURLType, .api) + XCTAssertEqual(req.path, "/2/dm_conversations/_d_id/messages") + XCTAssertEqual(req.bodyContentType, .json) + AssertEqualAnyDict( + req.parameters, + [ + "attachments": [["media_id": "1"], ["media_id": "2"]], + "text": "_t_", + ] + ) + } + + func testDefaultArg() throws { + let req = PostDmConversationByIdRequestV2( + dmConversationID: "_d_" + ) + + XCTAssertEqual(req.path, "/2/dm_conversations/_d_/messages") + AssertEqualAnyDict( + req.parameters, + [:] + ) + } +} diff --git a/Tests/TwitterAPIKitTests/APIv2/DirectMessage/PostDmConversationRequestV2Tests.swift b/Tests/TwitterAPIKitTests/APIv2/DirectMessage/PostDmConversationRequestV2Tests.swift new file mode 100644 index 0000000..7edf056 --- /dev/null +++ b/Tests/TwitterAPIKitTests/APIv2/DirectMessage/PostDmConversationRequestV2Tests.swift @@ -0,0 +1,51 @@ +import TwitterAPIKit +import XCTest + +class PostDmConversationRequestV2Tests: XCTestCase { + override func setUpWithError() throws { + } + + override func tearDownWithError() throws { + } + + func test() throws { + let req = PostDmConversationRequestV2( + conversationType: .group, + participantIDs: ["a", "b"], + attachments: ["2", "3"], + text: "_t_" + ) + + XCTAssertEqual(req.method, .post) + XCTAssertEqual(req.baseURLType, .api) + XCTAssertEqual(req.path, "/2/dm_conversations") + XCTAssertEqual(req.bodyContentType, .json) + AssertEqualAnyDict( + req.parameters, + [ + "conversation_type": "Group", + "participant_ids": ["a", "b"], + "message": [ + "attachments": [["media_id": "2"], ["media_id": "3"]], + "text": "_t_", + ], + ] + ) + } + + func testDefaultArg() throws { + let req = PostDmConversationRequestV2( + conversationType: .group, + participantIDs: ["a", "b", "c"] + ) + + AssertEqualAnyDict( + req.parameters, + [ + "conversation_type": "Group", + "participant_ids": ["a", "b", "c"], + "message": [:], + ] + ) + } +} diff --git a/Tests/TwitterAPIKitTests/APIv2/DirectMessage/PostDmConversationWithUserRequestV2Tests.swift b/Tests/TwitterAPIKitTests/APIv2/DirectMessage/PostDmConversationWithUserRequestV2Tests.swift new file mode 100644 index 0000000..41aaa05 --- /dev/null +++ b/Tests/TwitterAPIKitTests/APIv2/DirectMessage/PostDmConversationWithUserRequestV2Tests.swift @@ -0,0 +1,42 @@ +import TwitterAPIKit +import XCTest + +class PostDmConversationWithUserRequestV2Tests: XCTestCase { + override func setUpWithError() throws { + } + + override func tearDownWithError() throws { + } + + func test() throws { + let req = PostDmConversationWithUserRequestV2( + participantID: "_p_", + attachments: ["10"], + text: "_t_" + ) + + XCTAssertEqual(req.method, .post) + XCTAssertEqual(req.baseURLType, .api) + XCTAssertEqual(req.path, "/2/dm_conversations/with/_p_/messages") + XCTAssertEqual(req.bodyContentType, .json) + AssertEqualAnyDict( + req.parameters, + [ + "attachments": [["media_id": "10"]], + "text": "_t_", + ] + ) + } + + func testDefaultArg() throws { + let req = PostDmConversationWithUserRequestV2( + participantID: "_p_id" + ) + + XCTAssertEqual(req.path, "/2/dm_conversations/with/_p_id/messages") + AssertEqualAnyDict( + req.parameters, + [:] + ) + } +} diff --git a/Tests/TwitterAPIKitTests/APIv2/ExpansionsV2Tests.swift b/Tests/TwitterAPIKitTests/APIv2/ExpansionsV2Tests.swift index ed4907e..2683e7f 100644 --- a/Tests/TwitterAPIKitTests/APIv2/ExpansionsV2Tests.swift +++ b/Tests/TwitterAPIKitTests/APIv2/ExpansionsV2Tests.swift @@ -90,4 +90,23 @@ class ExpansionsV2Tests: XCTestCase { XCTAssertEqual(TwitterSpaceExpansionsV2.all.count, 5) } + func testTwitterDmEventExpansionsV2() throws { + let allCases: [TwitterDmEventExpansionsV2] = [ + .attachmentsMediaKeys, + .participantIDs, + .referencedTweetsID, + .senderID, + .other("~~~"), + ].shuffled() + + // curl https://api.twitter.com/2/openapi.json | jq '.components.parameters.DmEventExpansionsParameter.schema.items.enum | sort | join(",")' + XCTAssertEqual( + allCases.commaSeparatedString, + "attachments.media_keys,participant_ids,referenced_tweets.id,sender_id,~~~" + ) + } + + func testTwitterDmEventExpansionsV2All() throws { + XCTAssertEqual(TwitterDmEventExpansionsV2.all.count, 4) + } } diff --git a/Tests/TwitterAPIKitTests/APIv2/FieldsV2Tests.swift b/Tests/TwitterAPIKitTests/APIv2/FieldsV2Tests.swift index a3115b7..ea56727 100644 --- a/Tests/TwitterAPIKitTests/APIv2/FieldsV2Tests.swift +++ b/Tests/TwitterAPIKitTests/APIv2/FieldsV2Tests.swift @@ -226,4 +226,40 @@ class FieldsV2Tests: XCTestCase { func testTwitterTopicFieldsV2All() throws { XCTAssertEqual(TwitterTopicFieldsV2.all.count, 3) } + + func testTwitterDmEventFieldsV2() throws { + let allCases: [TwitterDmEventFieldsV2] = [ + .attachments, + .createdAt, + .dmConversationID, + .eventType, + .id, + .participantIDs, + .referencedTweets, + .senderID, + .text, + .other("~"), + ].shuffled() + // curl https://api.twitter.com/2/openapi.json | jq '.components.parameters.DmEventFieldsParameter.schema.items.enum | sort | join(",")' + XCTAssertEqual( + allCases.commaSeparatedString, + "attachments,created_at,dm_conversation_id,event_type,id,participant_ids,referenced_tweets,sender_id,text,~" + ) + } + + func testTwitterDmEventFieldsV2All() throws { + XCTAssertEqual(TwitterDmEventFieldsV2.all.count, 9) + } + + func testTwitterDmConversationFieldsV2() throws { + let allCases: [TwitterDmConversationFieldsV2] = [ + .id, + .other("~"), + ].shuffled() + XCTAssertEqual(allCases.commaSeparatedString, "id,~") + } + + func testTwitterDmConversationFieldsV2All() throws { + XCTAssertEqual(TwitterDmConversationFieldsV2.all.count, 1) + } }