refactor: Adjust create tweet action to use new http client

This commit is contained in:
Faruk AYDIN
2022-08-20 00:49:01 +03:00
committed by Ali BARIN
parent 17010f9283
commit cd6c5216ff
5 changed files with 56 additions and 19 deletions

View File

@@ -1,10 +1,12 @@
import TwitterClient from './client';
import CreateTweet from './actions/create-tweet';
import { IJSONObject } from '@automatisch/types';
export default class Actions {
client: TwitterClient;
createTweet: CreateTweet;
constructor(connectionData: IJSONObject, parameters: IJSONObject) {
this.createTweet = new CreateTweet(connectionData, parameters);
constructor(client: TwitterClient) {
this.client = client;
this.createTweet = new CreateTweet(client);
}
}

View File

@@ -1,23 +1,17 @@
import TwitterApi, { TwitterApiTokens } from 'twitter-api-v2';
import { IJSONObject } from '@automatisch/types';
import TwitterClient from '../client';
export default class CreateTweet {
client: TwitterApi;
parameters: IJSONObject;
client: TwitterClient;
constructor(connectionData: IJSONObject, parameters: IJSONObject) {
this.client = new TwitterApi({
appKey: connectionData.consumerKey,
appSecret: connectionData.consumerSecret,
accessToken: connectionData.accessToken,
accessSecret: connectionData.accessSecret,
} as TwitterApiTokens);
this.parameters = parameters;
constructor(client: TwitterClient) {
this.client = client;
}
async run() {
const tweet = await this.client.v1.tweet(this.parameters.tweet as string);
return tweet;
const response = await this.client.createTweet.run(
this.client.parameters.tweet as string
);
return response.data.data;
}
}

View File

@@ -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}`);
}
}
}

View File

@@ -7,6 +7,7 @@ import VerifyAccessToken from './endpoints/verify-access-token';
import GetCurrentUser from './endpoints/get-current-user';
import GetUserByUsername from './endpoints/get-user-by-username';
import GetUserTweets from './endpoints/get-user-tweets';
import CreateTweet from './endpoints/create-tweet';
export default class TwitterClient {
appData: IApp;
@@ -20,6 +21,7 @@ export default class TwitterClient {
getCurrentUser: GetCurrentUser;
getUserByUsername: GetUserByUsername;
getUserTweets: GetUserTweets;
createTweet: CreateTweet;
static baseUrl = 'https://api.twitter.com';
@@ -55,5 +57,6 @@ export default class TwitterClient {
this.getCurrentUser = new GetCurrentUser(this);
this.getUserByUsername = new GetUserByUsername(this);
this.getUserTweets = new GetUserTweets(this);
this.createTweet = new CreateTweet(this);
}
}

View File

@@ -25,6 +25,6 @@ export default class Twitter implements IService {
this.authenticationClient = new Authentication(this.client);
this.triggers = new Triggers(this.client);
// this.actions = new Actions(connectionData, parameters);
this.actions = new Actions(this.client);
}
}