-
Notifications
You must be signed in to change notification settings - Fork 1
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 #12 from usherlabs/feature/parallel_to_sequential
Feature/parallel to sequential
- Loading branch information
Showing
5 changed files
with
60 additions
and
74 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
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
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
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
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 |
---|---|---|
@@ -1,66 +1,20 @@ | ||
import env from "@/env"; | ||
import axios from "axios"; | ||
|
||
class XfkaTwitter { | ||
private initialized = false; | ||
private accessToken: string | null = null; | ||
private SERVER_DOMAIN = "api.twitter.com"; | ||
|
||
constructor( | ||
private apiKey: string, | ||
private apiKeySecret: string, | ||
) {} | ||
constructor(private accessToken: string) {} | ||
|
||
get hosts() { | ||
return ["x.com", "api.x.com", "twitter.com", "api.twitter.com"]; | ||
} | ||
|
||
isAvailable(): boolean { | ||
if (this.apiKey && this.apiKeySecret) { | ||
return true; | ||
} | ||
return false; | ||
get getRequestRate() { | ||
return 60 * 1000; // | ||
} | ||
|
||
isInitialized(): boolean { | ||
return this.initialized; | ||
} | ||
|
||
async requestAccessToken() { | ||
try { | ||
// Fetch bearer token | ||
const response = await axios.post( | ||
`https://${this.SERVER_DOMAIN}/oauth2/token`, | ||
new URLSearchParams({ | ||
grant_type: "client_credentials", | ||
}).toString(), | ||
{ | ||
headers: { | ||
"Content-Type": "application/x-www-form-urlencoded", | ||
}, | ||
auth: { | ||
username: this.apiKey, | ||
password: this.apiKeySecret, | ||
}, | ||
}, | ||
); | ||
const accessToken = response.data.access_token; | ||
|
||
this.accessToken = accessToken; | ||
this.initialized = true; | ||
return accessToken; | ||
} catch (error: any) { | ||
console.error("Error fetching bearer token:", typeof error, error.message); | ||
throw error; | ||
} | ||
} | ||
|
||
getAccessToken(): string | null { | ||
if (!this.initialized) { | ||
throw new Error("Class not initialized"); | ||
} | ||
return this.accessToken; | ||
} | ||
} | ||
|
||
export const instance = new XfkaTwitter(env.integrations.xApiKey, env.integrations.xApiSecret); | ||
export const instance = new XfkaTwitter(env.integrations.xBearerToken); |