Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.DS_Store
/.build
**/.build/
/Packages
/*.xcodeproj
xcuserdata/
Expand Down
24 changes: 24 additions & 0 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

129 changes: 104 additions & 25 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,46 +1,125 @@
// swift-tools-version:5.5
// swift-tools-version: 6.1
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

// Currently, CommonCrypto and CryptoKit are not available under Linux.
// If CommonCrypto is not available, swift-crypto should be used.

#if canImport(CommonCrypto)
internal let dependencies: [Package.Dependency] = []
internal let tDependencies: [Target.Dependency] = []
#else // for Linux
internal let dependencies: [Package.Dependency] = [
.package(url: "https://github.com/apple/swift-crypto.git", from: "3.8.0")
]
internal let tDependencies: [Target.Dependency] = [.product(name: "Crypto", package: "swift-crypto")]
#endif

internal let package = Package(
let package = Package(
name: "TwitterAPIKit",
platforms: [
.macOS(.v10_14),
.iOS(.v12),
.tvOS(.v12),
.watchOS(.v6),
.macOS(.v13),
.iOS(.v16),
.tvOS(.v16),
.watchOS(.v9),
],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "TwitterAPIKit",
targets: ["TwitterAPIKit"]
),
],
dependencies: dependencies,
dependencies: [
.package(url: "https://github.com/apple/swift-crypto.git", from: "3.1.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "TwitterAPIKit",
dependencies: tDependencies
dependencies: [
.product(name: "Crypto", package: "swift-crypto"),
]
),
.testTarget(
name: "TwitterAPIKitTests",
dependencies: ["TwitterAPIKit"]
),
.executableTarget(
name: "GetUserByIdExample",
dependencies: ["TwitterAPIKit"],
path: "Sources/Examples/GetUserByIdExample"
),
.executableTarget(
name: "GetUsersByIdsExample",
dependencies: ["TwitterAPIKit"],
path: "Sources/Examples/GetUsersByIdsExample"
),
.executableTarget(
name: "GetUserByUsernameExample",
dependencies: ["TwitterAPIKit"],
path: "Sources/Examples/GetUserByUsernameExample"
),
.executableTarget(
name: "GetUsersByUsernamesExample",
dependencies: ["TwitterAPIKit"],
path: "Sources/Examples/GetUsersByUsernamesExample"
),
.executableTarget(
name: "GetAuthenticatedUserExample",
dependencies: ["TwitterAPIKit"],
path: "Sources/Examples/GetAuthenticatedUserExample"
),
.executableTarget(
name: "RetweetExample",
dependencies: ["TwitterAPIKit"],
path: "Sources/Examples/RetweetExample"
),
.executableTarget(
name: "GetTweetsCountsAllExample",
dependencies: ["TwitterAPIKit"],
path: "Sources/Examples/GetTweetsCountsAllExample"
),
.executableTarget(
name: "GetTweetsCountsRecentExample",
dependencies: ["TwitterAPIKit"],
path: "Sources/Examples/GetTweetsCountsRecentExample"
),
.executableTarget(
name: "GetTweetsSearchAllExample",
dependencies: ["TwitterAPIKit"],
path: "Sources/Examples/GetTweetsSearchAllExample"
),
.executableTarget(
name: "GetTweetsSearchRecentExample",
dependencies: ["TwitterAPIKit"],
path: "Sources/Examples/GetTweetsSearchRecentExample"
),
.executableTarget(
name: "GetTweetsRetweetedByExample",
dependencies: ["TwitterAPIKit"],
path: "Sources/Examples/GetTweetsRetweetedByExample"
),
.executableTarget(
name: "DeleteUsersRetweetsExample",
dependencies: ["TwitterAPIKit"],
path: "Sources/Examples/DeleteUsersRetweetsExample"
),
.executableTarget(
name: "GetUserTweetsExample",
dependencies: ["TwitterAPIKit"],
path: "Sources/Examples/GetUserTweetsExample"
),
.executableTarget(
name: "GetTweetExample",
dependencies: ["TwitterAPIKit"],
path: "Sources/Examples/GetTweetExample"
),
.executableTarget(
name: "PostTweetExample",
dependencies: ["TwitterAPIKit"],
path: "Sources/Examples/PostTweetExample"
),
.executableTarget(
name: "OAuth10aExample",
dependencies: ["TwitterAPIKit"],
path: "Sources/Examples/OAuth10aExample"
),
.executableTarget(
name: "OAuth20Example",
dependencies: ["TwitterAPIKit"],
path: "Sources/Examples/OAuth20Example"
),
.executableTarget(
name: "OAuth20RefreshExample",
dependencies: ["TwitterAPIKit"],
path: "Sources/Examples/OAuth20RefreshExample"
),
]
)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import Foundation
import TwitterAPIKit

/// Demonstrates how to unretweet a tweet by the authenticated user.
/// This example:
/// 1. Creates a Twitter API client using environment variables for authentication
/// 2. Makes a request to unretweet a tweet
/// 3. Prints the result
@main
internal struct DeleteUsersRetweetsExample {
/// EntryPoint
public static func main() async throws {
let client = TwitterAPISession(
authenticationType: .oauth20(
clientId: ProcessInfo.processInfo.environment["TWITTER_CLIENT_ID"] ?? "",
clientSecret: ProcessInfo.processInfo.environment["TWITTER_CLIENT_SECRET"] ?? "",
accessToken: ProcessInfo.processInfo.environment["TWITTER_ACCESS_TOKEN"] ?? "",
refreshToken: nil
)
)

let userId = "YOUR_USER_ID"
let tweetId = "YOUR_TWEET_ID"
let request = DeleteUsersRetweetsRequestV2(userId: userId, tweetId: tweetId)

do {
let response = try await client.send(request)
print("\n🗑️ Unretweet result: \(response.data)")
} catch {
print("Error unretweeting: \(error)")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import Foundation
import TwitterAPIKit

/// Demonstrates how to retrieve the authenticated user's profile information using Twitter API v2.
/// This example:
/// 1. Creates a Twitter API client using environment variables for authentication
/// 2. Makes a request to the /2/users/me endpoint to get the current user's profile
/// 3. Includes additional data like the user's pinned tweet
/// 4. Prints the user's name, username, and their pinned tweet (if one exists)
@main
internal struct GetAuthenticatedUserExample {
/// EntryPoint
public static func main() async throws {
let client = TwitterAPISession(
authenticationType: .oauth10a(
consumerKey: ProcessInfo.processInfo.environment["TWITTER_CONSUMER_KEY"] ?? "",
consumerSecret: ProcessInfo.processInfo.environment["TWITTER_CONSUMER_SECRET"] ?? "",
oauthToken: ProcessInfo.processInfo.environment["TWITTER_OAUTH_TOKEN"] ?? "",
oauthTokenSecret: ProcessInfo.processInfo.environment["TWITTER_OAUTH_TOKEN_SECRET"] ?? ""
)
)

let meRequest = GetUsersMeRequestV2(
expansions: [.pinnedTweetID],
tweetFields: [.createdAt, .text],
userFields: [.name, .username, .profileImageUrl]
)

do {
let response = try await client.send(meRequest)
print("Authenticated User: \(response.data.name) (@\(response.data.username))")
if let pinnedTweet = response.includes?.tweets?.first {
print("Pinned Tweet: \(pinnedTweet.text)")
}
} catch {
print("Error retrieving authenticated user: \(error)")
}
}
}
83 changes: 83 additions & 0 deletions Sources/Examples/GetTweetExample/GetTweetExample.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// main.swift
// Copyright (c) 2025 GetAutomaApp
// All source code and related assets are the property of GetAutomaApp.
// All rights reserved.

import Foundation
import TwitterAPIKit

/// Demonstrates how to retrieve a specific tweet and its associated data using Twitter API v2.
/// This example:
/// 1. Creates a Twitter API client using environment variables for authentication
/// 2. Makes a request to fetch a specific tweet by ID
/// 3. Includes expanded data like author information, metrics, and referenced tweets
/// 4. Prints the tweet's content, metrics (retweets, likes, replies, quotes), and author details
@main
public struct GetTweetExample {
/// Entry Point
public static func main() async throws {
let client = TwitterAPISession(
authenticationType: .oauth10a(
consumerKey: ProcessInfo.processInfo.environment["TWITTER_CONSUMER_KEY"] ?? "",
consumerSecret: ProcessInfo.processInfo.environment["TWITTER_CONSUMER_SECRET"] ?? "",
oauthToken: ProcessInfo.processInfo.environment["TWITTER_OAUTH_TOKEN"] ?? "",
oauthTokenSecret: ProcessInfo.processInfo.environment["TWITTER_OAUTH_TOKEN_SECRET"] ?? ""
)
)

let tweetId = "1931427021776908732"

await handleRequest(client, tweetId: tweetId)
}

private static func logResponse(_ response: GetTweetRequestV2.Response) {
print("Successfully retrieved tweet!")
print("Tweet ID: \(response.data.id)")
print("Tweet text: \(response.data.text)")
print("Created at: \(String(describing: response.data.createdAt))")

if let metrics = response.data.publicMetrics {
print("Metrics:")
print("- Retweets: \(metrics.retweetCount)")
print("- Likes: \(metrics.likeCount)")
print("- Replies: \(metrics.replyCount)")
print("- Quotes: \(metrics.quoteCount)")
}

if let author = response.includes?.users?.first {
print("\nAuthor:")
print("- Name: \(author.name)")
print("- Username: @\(author.username)")
}
}

private static func handleRequest(_ client: TwitterAPISession, tweetId: String) async {
do {
let request = GetTweetRequestV2(
id: tweetId,
expansions: [
.authorID,
.referencedTweetsID,
],
tweetFields: [
.createdAt,
.text,
.publicMetrics,
.entities,
],
userFields: [
.name,
.username,
.profileImageUrl,
]
)

let response = try await client.send(request)

logResponse(response)

} catch {
print("Error retrieving tweet: \(error)")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import Foundation
import TwitterAPIKit

/// Demonstrates how to retrieve tweet counts from the last 7 days.
/// This example:
/// 1. Creates a Twitter API client using environment variables for authentication
/// 2. Makes a request to count tweets matching a search query
/// 3. Specifies a time range and granularity for the counts
/// 4. Prints the tweet counts for each time period
/// Note: This endpoint requires Academic Research access.
@main
internal struct GetTweetsCountsAllExample {
/// EntryPoint
public static func main() async throws {
let client = TwitterAPISession(
authenticationType: .oauth20(
clientId: ProcessInfo.processInfo.environment["TWITTER_CLIENT_ID"] ?? "",
clientSecret: ProcessInfo.processInfo.environment["TWITTER_CLIENT_SECRET"] ?? "",
accessToken: ProcessInfo.processInfo.environment["TWITTER_ACCESS_TOKEN"] ?? "",
refreshToken: nil
)
)

let endTime = Date()

guard let startTime = Calendar.current.date(byAdding: .day, value: -7, to: endTime) else {
throw URLError(.badURL)
}

let request = GetTweetsCountsAllRequestV2(
query: "Twitter",
endTime: endTime,
granularity: .day,
startTime: startTime
)

do {
let response = try await client.send(request)
print("\n📈 Total Tweets: \(response.meta.totalTweetCount) npt: \(response.meta.nextToken ?? "N/A")")
} catch {
print("Error retrieving tweet counts: \(error)")
}
}
}
Loading
Loading