Skip to content
This repository was archived by the owner on Jun 30, 2023. It is now read-only.

Commit 7bbdd82

Browse files
authored
Merge pull request #148 from mironal/DM-v2
Add Direct Message API v2
2 parents 79e90e9 + d1dedd9 commit 7bbdd82

21 files changed

+870
-2
lines changed

.github/workflows/swift.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ on:
88

99
jobs:
1010
build:
11-
runs-on: macos-latest
11+
runs-on: macos-12
1212
steps:
1313
- uses: actions/checkout@v2
1414
- uses: actions/cache@v2
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import Foundation
2+
3+
open class DirectMessageAPIv2: TwitterAPIBase {
4+
/// https://developer.twitter.com/en/docs/twitter-api/direct-messages/lookup/api-reference/get-dm_events
5+
public func getDmEvents(
6+
_ request: GetDmEventsRequestV2
7+
) -> TwitterAPISessionJSONTask {
8+
return session.send(request)
9+
}
10+
11+
/// https://developer.twitter.com/en/docs/twitter-api/direct-messages/lookup/api-reference/get-dm_conversations-with-participant_id-dm_events
12+
public func getDmEventsWithParticipantId(
13+
_ request: GetDmConversationsWithParticipantIdDmEventsRequestV2
14+
) -> TwitterAPISessionJSONTask {
15+
return session.send(request)
16+
}
17+
18+
/// https://developer.twitter.com/en/docs/twitter-api/direct-messages/lookup/api-reference/get-dm_conversations-dm_conversation_id-dm_events
19+
public func getDmEventsByConversationsId(
20+
_ request: GetDmConversationsIdDmEventsRequestV2
21+
) -> TwitterAPISessionJSONTask {
22+
return session.send(request)
23+
}
24+
25+
/// https://developer.twitter.com/en/docs/twitter-api/direct-messages/manage/api-reference/post-dm_conversations-dm_conversation_id-messages
26+
public func postDmConversationById(
27+
_ request: PostDmConversationByIdRequestV2
28+
) -> TwitterAPISessionJSONTask {
29+
return session.send(request)
30+
}
31+
32+
/// https://developer.twitter.com/en/docs/twitter-api/direct-messages/manage/api-reference/post-dm_conversations-with-participant_id-messages
33+
public func postDmConversationWithUser(
34+
_ request: PostDmConversationWithUserRequestV2
35+
) -> TwitterAPISessionJSONTask {
36+
return session.send(request)
37+
}
38+
39+
/// https://developer.twitter.com/en/docs/twitter-api/direct-messages/manage/api-reference/post-dm_conversations
40+
public func postDmConversation(
41+
_ request: PostDmConversationRequestV2
42+
) -> TwitterAPISessionJSONTask {
43+
return session.send(request)
44+
}
45+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import Foundation
2+
3+
/// Returns DM Events for a DM Conversation
4+
/// Required OAuth 2.0 scopes: dm.read, tweet.read, users.read
5+
open class GetDmConversationsIdDmEventsRequestV2: TwitterAPIRequest {
6+
7+
/// The DM Conversation ID.
8+
public let id: String
9+
/// The maximum number of results.
10+
public let maxResults: Int?
11+
/// This parameter is used to get a specified 'page' of results.
12+
public let paginationToken: String?
13+
/// The set of event_types to include in the results.
14+
public let eventTypes: Set<TwitterDirectMessageEventTypeV2>?
15+
/// A comma separated list of DmEvent fields to display.
16+
public let dmEventFields: Set<TwitterDmEventFieldsV2>?
17+
/// A comma separated list of fields to expand.
18+
public let expansions: Set<TwitterDmEventExpansionsV2>?
19+
/// A comma separated list of Media fields to display.
20+
public let mediaFields: Set<TwitterMediaFieldsV2>?
21+
/// A comma separated list of User fields to display.
22+
public let userFields: Set<TwitterUserFieldsV2>?
23+
/// A comma separated list of Tweet fields to display.
24+
public let tweetFields: Set<TwitterTweetFieldsV2>?
25+
26+
public var method: HTTPMethod {
27+
return .get
28+
}
29+
30+
public var path: String {
31+
return "/2/dm_conversations/\(id)/dm_events"
32+
}
33+
34+
open var parameters: [String: Any] {
35+
var p = [String: Any]()
36+
maxResults.map { p["max_results"] = $0 }
37+
paginationToken.map { p["pagination_token"] = $0 }
38+
eventTypes?.bind(param: &p)
39+
dmEventFields?.bind(param: &p)
40+
expansions?.bind(param: &p)
41+
mediaFields?.bind(param: &p)
42+
userFields?.bind(param: &p)
43+
tweetFields?.bind(param: &p)
44+
return p
45+
}
46+
47+
public init(
48+
id: String,
49+
maxResults: Int? = .none,
50+
paginationToken: String? = .none,
51+
eventTypes: Set<TwitterDirectMessageEventTypeV2>? = .none,
52+
dmEventFields: Set<TwitterDmEventFieldsV2>? = .none,
53+
expansions: Set<TwitterDmEventExpansionsV2>? = .none,
54+
mediaFields: Set<TwitterMediaFieldsV2>? = .none,
55+
userFields: Set<TwitterUserFieldsV2>? = .none,
56+
tweetFields: Set<TwitterTweetFieldsV2>? = .none
57+
) {
58+
self.id = id
59+
self.maxResults = maxResults
60+
self.paginationToken = paginationToken
61+
self.eventTypes = eventTypes
62+
self.dmEventFields = dmEventFields
63+
self.expansions = expansions
64+
self.mediaFields = mediaFields
65+
self.userFields = userFields
66+
self.tweetFields = tweetFields
67+
}
68+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import Foundation
2+
3+
/// Returns DM Events for a DM Conversation
4+
/// Required OAuth 2.0 scopes: dm.read, tweet.read, users.read
5+
open class GetDmConversationsWithParticipantIdDmEventsRequestV2: TwitterAPIRequest {
6+
7+
/// The ID of the participant user for the One to One DM conversation.
8+
public let participantID: String
9+
/// The maximum number of results.
10+
public let maxResults: Int?
11+
/// This parameter is used to get a specified 'page' of results.
12+
public let paginationToken: String?
13+
/// The set of event_types to include in the results.
14+
public let eventTypes: Set<TwitterDirectMessageEventTypeV2>?
15+
/// A comma separated list of DmEvent fields to display.
16+
public let dmEventFields: Set<TwitterDmEventFieldsV2>?
17+
/// A comma separated list of fields to expand.
18+
public let expansions: Set<TwitterDmEventExpansionsV2>?
19+
/// A comma separated list of Media fields to display.
20+
public let mediaFields: Set<TwitterMediaFieldsV2>?
21+
/// A comma separated list of User fields to display.
22+
public let userFields: Set<TwitterUserFieldsV2>?
23+
/// A comma separated list of Tweet fields to display.
24+
public let tweetFields: Set<TwitterTweetFieldsV2>?
25+
26+
public var method: HTTPMethod {
27+
return .get
28+
}
29+
30+
public var path: String {
31+
return "/2/dm_conversations/with/\(participantID)/dm_events"
32+
}
33+
34+
open var parameters: [String: Any] {
35+
var p = [String: Any]()
36+
maxResults.map { p["max_results"] = $0 }
37+
paginationToken.map { p["pagination_token"] = $0 }
38+
eventTypes?.bind(param: &p)
39+
dmEventFields?.bind(param: &p)
40+
expansions?.bind(param: &p)
41+
mediaFields?.bind(param: &p)
42+
userFields?.bind(param: &p)
43+
tweetFields?.bind(param: &p)
44+
return p
45+
}
46+
47+
public init(
48+
participantID: String,
49+
maxResults: Int? = .none,
50+
paginationToken: String? = .none,
51+
eventTypes: Set<TwitterDirectMessageEventTypeV2>? = .none,
52+
dmEventFields: Set<TwitterDmEventFieldsV2>? = .none,
53+
expansions: Set<TwitterDmEventExpansionsV2>? = .none,
54+
mediaFields: Set<TwitterMediaFieldsV2>? = .none,
55+
userFields: Set<TwitterUserFieldsV2>? = .none,
56+
tweetFields: Set<TwitterTweetFieldsV2>? = .none
57+
) {
58+
self.participantID = participantID
59+
self.maxResults = maxResults
60+
self.paginationToken = paginationToken
61+
self.eventTypes = eventTypes
62+
self.dmEventFields = dmEventFields
63+
self.expansions = expansions
64+
self.mediaFields = mediaFields
65+
self.userFields = userFields
66+
self.tweetFields = tweetFields
67+
}
68+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import Foundation
2+
3+
/// Returns recent DM Events across DM conversations
4+
/// Required OAuth 2.0 scopes: dm.read, tweet.read, users.read
5+
open class GetDmEventsRequestV2: TwitterAPIRequest {
6+
7+
/// The maximum number of results.
8+
public let maxResults: Int?
9+
/// This parameter is used to get a specified 'page' of results.
10+
public let paginationToken: String?
11+
/// The set of event_types to include in the results.
12+
public let eventTypes: Set<TwitterDirectMessageEventTypeV2>?
13+
/// A comma separated list of DmEvent fields to display.
14+
public let dmEventFields: Set<TwitterDmEventFieldsV2>?
15+
/// A comma separated list of fields to expand.
16+
public let expansions: Set<TwitterDmEventExpansionsV2>?
17+
/// A comma separated list of Media fields to display.
18+
public let mediaFields: Set<TwitterMediaFieldsV2>?
19+
/// A comma separated list of User fields to display.
20+
public let userFields: Set<TwitterUserFieldsV2>?
21+
/// A comma separated list of Tweet fields to display.
22+
public let tweetFields: Set<TwitterTweetFieldsV2>?
23+
24+
public var method: HTTPMethod {
25+
return .get
26+
}
27+
28+
public var path: String {
29+
return "/2/dm_events"
30+
}
31+
32+
open var parameters: [String: Any] {
33+
var p = [String: Any]()
34+
maxResults.map { p["max_results"] = $0 }
35+
paginationToken.map { p["pagination_token"] = $0 }
36+
eventTypes?.bind(param: &p)
37+
dmEventFields?.bind(param: &p)
38+
expansions?.bind(param: &p)
39+
mediaFields?.bind(param: &p)
40+
userFields?.bind(param: &p)
41+
tweetFields?.bind(param: &p)
42+
return p
43+
}
44+
45+
public init(
46+
maxResults: Int? = .none,
47+
paginationToken: String? = .none,
48+
eventTypes: Set<TwitterDirectMessageEventTypeV2>? = .none,
49+
dmEventFields: Set<TwitterDmEventFieldsV2>? = .none,
50+
expansions: Set<TwitterDmEventExpansionsV2>? = .none,
51+
mediaFields: Set<TwitterMediaFieldsV2>? = .none,
52+
userFields: Set<TwitterUserFieldsV2>? = .none,
53+
tweetFields: Set<TwitterTweetFieldsV2>? = .none
54+
) {
55+
self.maxResults = maxResults
56+
self.paginationToken = paginationToken
57+
self.eventTypes = eventTypes
58+
self.dmEventFields = dmEventFields
59+
self.expansions = expansions
60+
self.mediaFields = mediaFields
61+
self.userFields = userFields
62+
self.tweetFields = tweetFields
63+
}
64+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import Foundation
2+
3+
/// Creates a new message for a DM Conversation specified by DM Conversation ID
4+
/// Required OAuth 2.0 scopes: dm.write, tweet.read, users.read
5+
open class PostDmConversationByIdRequestV2: TwitterAPIRequest {
6+
7+
/// The DM Conversation ID.
8+
public let dmConversationID: String
9+
/// Attachments to a DM Event.
10+
public let attachments: [String]?
11+
/// Text of the message.
12+
public let text: String?
13+
14+
public var method: HTTPMethod {
15+
return .post
16+
}
17+
18+
public var path: String {
19+
return "/2/dm_conversations/\(dmConversationID)/messages"
20+
}
21+
22+
public var bodyContentType: BodyContentType {
23+
return .json
24+
}
25+
26+
open var parameters: [String: Any] {
27+
var p = [String: Any]()
28+
if let attachments = attachments {
29+
p["attachments"] = attachments.map { ["media_id": $0] }
30+
}
31+
text.map { p["text"] = $0 }
32+
return p
33+
}
34+
35+
public init(
36+
dmConversationID: String,
37+
attachments: [String]? = .none,
38+
text: String? = .none
39+
) {
40+
self.dmConversationID = dmConversationID
41+
self.attachments = attachments
42+
self.text = text
43+
}
44+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import Foundation
2+
3+
/// Creates a new DM Conversation.
4+
/// Required OAuth 2.0 scopes: dm.write, tweet.read, users.read
5+
open class PostDmConversationRequestV2: TwitterAPIRequest {
6+
7+
/// The conversation type that is being created.
8+
public enum ConversationType: String {
9+
case group = "Group"
10+
}
11+
12+
/// The conversation type that is being created.
13+
public let conversationType: ConversationType
14+
/// Participants for the DM Conversation.
15+
public let participantIDs: [String]
16+
/// Attachments to a DM Event.
17+
public let attachments: [String]?
18+
/// Text of the message.
19+
public let text: String?
20+
21+
public var method: HTTPMethod {
22+
return .post
23+
}
24+
25+
public var path: String {
26+
return "/2/dm_conversations"
27+
}
28+
29+
public var bodyContentType: BodyContentType {
30+
return .json
31+
}
32+
33+
open var parameters: [String: Any] {
34+
var p = [String: Any]()
35+
p["conversation_type"] = conversationType.rawValue
36+
p["participant_ids"] = participantIDs
37+
var message = [String: Any]()
38+
text.map { message["text"] = $0 }
39+
if let attachments = attachments {
40+
message["attachments"] = attachments.map { ["media_id": $0] }
41+
}
42+
p["message"] = message
43+
return p
44+
}
45+
46+
public init(
47+
conversationType: ConversationType,
48+
participantIDs: [String],
49+
attachments: [String]? = .none,
50+
text: String? = .none
51+
) {
52+
self.conversationType = conversationType
53+
self.participantIDs = participantIDs
54+
self.attachments = attachments
55+
self.text = text
56+
}
57+
}

0 commit comments

Comments
 (0)