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 #148 from mironal/DM-v2
Add Direct Message API v2
- Loading branch information
Showing
21 changed files
with
870 additions
and
2 deletions.
There are no files selected for viewing
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
45 changes: 45 additions & 0 deletions
45
Sources/TwitterAPIKit/APIv2/DirectMessage/DirectMessageAPIv2.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,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) | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
...es/TwitterAPIKit/APIv2/DirectMessage/Requests/GetDmConversationsIdDmEventsRequestV2.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,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<TwitterDirectMessageEventTypeV2>? | ||
/// A comma separated list of DmEvent fields to display. | ||
public let dmEventFields: Set<TwitterDmEventFieldsV2>? | ||
/// A comma separated list of fields to expand. | ||
public let expansions: Set<TwitterDmEventExpansionsV2>? | ||
/// A comma separated list of Media fields to display. | ||
public let mediaFields: Set<TwitterMediaFieldsV2>? | ||
/// A comma separated list of User fields to display. | ||
public let userFields: Set<TwitterUserFieldsV2>? | ||
/// A comma separated list of Tweet fields to display. | ||
public let tweetFields: Set<TwitterTweetFieldsV2>? | ||
|
||
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<TwitterDirectMessageEventTypeV2>? = .none, | ||
dmEventFields: Set<TwitterDmEventFieldsV2>? = .none, | ||
expansions: Set<TwitterDmEventExpansionsV2>? = .none, | ||
mediaFields: Set<TwitterMediaFieldsV2>? = .none, | ||
userFields: Set<TwitterUserFieldsV2>? = .none, | ||
tweetFields: Set<TwitterTweetFieldsV2>? = .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 | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
...t/APIv2/DirectMessage/Requests/GetDmConversationsWithParticipantIdDmEventsRequestV2.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,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<TwitterDirectMessageEventTypeV2>? | ||
/// A comma separated list of DmEvent fields to display. | ||
public let dmEventFields: Set<TwitterDmEventFieldsV2>? | ||
/// A comma separated list of fields to expand. | ||
public let expansions: Set<TwitterDmEventExpansionsV2>? | ||
/// A comma separated list of Media fields to display. | ||
public let mediaFields: Set<TwitterMediaFieldsV2>? | ||
/// A comma separated list of User fields to display. | ||
public let userFields: Set<TwitterUserFieldsV2>? | ||
/// A comma separated list of Tweet fields to display. | ||
public let tweetFields: Set<TwitterTweetFieldsV2>? | ||
|
||
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<TwitterDirectMessageEventTypeV2>? = .none, | ||
dmEventFields: Set<TwitterDmEventFieldsV2>? = .none, | ||
expansions: Set<TwitterDmEventExpansionsV2>? = .none, | ||
mediaFields: Set<TwitterMediaFieldsV2>? = .none, | ||
userFields: Set<TwitterUserFieldsV2>? = .none, | ||
tweetFields: Set<TwitterTweetFieldsV2>? = .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 | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
Sources/TwitterAPIKit/APIv2/DirectMessage/Requests/GetDmEventsRequestV2.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,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<TwitterDirectMessageEventTypeV2>? | ||
/// A comma separated list of DmEvent fields to display. | ||
public let dmEventFields: Set<TwitterDmEventFieldsV2>? | ||
/// A comma separated list of fields to expand. | ||
public let expansions: Set<TwitterDmEventExpansionsV2>? | ||
/// A comma separated list of Media fields to display. | ||
public let mediaFields: Set<TwitterMediaFieldsV2>? | ||
/// A comma separated list of User fields to display. | ||
public let userFields: Set<TwitterUserFieldsV2>? | ||
/// A comma separated list of Tweet fields to display. | ||
public let tweetFields: Set<TwitterTweetFieldsV2>? | ||
|
||
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<TwitterDirectMessageEventTypeV2>? = .none, | ||
dmEventFields: Set<TwitterDmEventFieldsV2>? = .none, | ||
expansions: Set<TwitterDmEventExpansionsV2>? = .none, | ||
mediaFields: Set<TwitterMediaFieldsV2>? = .none, | ||
userFields: Set<TwitterUserFieldsV2>? = .none, | ||
tweetFields: Set<TwitterTweetFieldsV2>? = .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 | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
Sources/TwitterAPIKit/APIv2/DirectMessage/Requests/PostDmConversationByIdRequestV2.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,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 | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
Sources/TwitterAPIKit/APIv2/DirectMessage/Requests/PostDmConversationRequestV2.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,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 | ||
} | ||
} |
Oops, something went wrong.