refactor: Adjust create tweet action to use new http client
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
import TwitterClient from '../index';
|
||||
|
||||
export default class CreateTweet {
|
||||
client: TwitterClient;
|
||||
|
||||
constructor(client: TwitterClient) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
async run(text: string) {
|
||||
try {
|
||||
const token = {
|
||||
key: this.client.connectionData.accessToken as string,
|
||||
secret: this.client.connectionData.accessSecret as string,
|
||||
};
|
||||
|
||||
const requestData = {
|
||||
url: `${TwitterClient.baseUrl}/2/tweets`,
|
||||
method: 'POST',
|
||||
};
|
||||
|
||||
const authHeader = this.client.oauthClient.toHeader(
|
||||
this.client.oauthClient.authorize(requestData, token)
|
||||
);
|
||||
|
||||
const response = await this.client.httpClient.post(
|
||||
`/2/tweets`,
|
||||
{ text },
|
||||
{ headers: { ...authHeader } }
|
||||
);
|
||||
|
||||
return response;
|
||||
} catch (error) {
|
||||
const errorMessage = error.response.data.detail;
|
||||
throw new Error(`Error occured while creating a tweet: ${errorMessage}`);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user