refactor: Adjust create tweet action to use new http client
This commit is contained in:
@@ -1,10 +1,12 @@
|
|||||||
|
import TwitterClient from './client';
|
||||||
import CreateTweet from './actions/create-tweet';
|
import CreateTweet from './actions/create-tweet';
|
||||||
import { IJSONObject } from '@automatisch/types';
|
|
||||||
|
|
||||||
export default class Actions {
|
export default class Actions {
|
||||||
|
client: TwitterClient;
|
||||||
createTweet: CreateTweet;
|
createTweet: CreateTweet;
|
||||||
|
|
||||||
constructor(connectionData: IJSONObject, parameters: IJSONObject) {
|
constructor(client: TwitterClient) {
|
||||||
this.createTweet = new CreateTweet(connectionData, parameters);
|
this.client = client;
|
||||||
|
this.createTweet = new CreateTweet(client);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,23 +1,17 @@
|
|||||||
import TwitterApi, { TwitterApiTokens } from 'twitter-api-v2';
|
import TwitterClient from '../client';
|
||||||
import { IJSONObject } from '@automatisch/types';
|
|
||||||
|
|
||||||
export default class CreateTweet {
|
export default class CreateTweet {
|
||||||
client: TwitterApi;
|
client: TwitterClient;
|
||||||
parameters: IJSONObject;
|
|
||||||
|
|
||||||
constructor(connectionData: IJSONObject, parameters: IJSONObject) {
|
constructor(client: TwitterClient) {
|
||||||
this.client = new TwitterApi({
|
this.client = client;
|
||||||
appKey: connectionData.consumerKey,
|
|
||||||
appSecret: connectionData.consumerSecret,
|
|
||||||
accessToken: connectionData.accessToken,
|
|
||||||
accessSecret: connectionData.accessSecret,
|
|
||||||
} as TwitterApiTokens);
|
|
||||||
|
|
||||||
this.parameters = parameters;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async run() {
|
async run() {
|
||||||
const tweet = await this.client.v1.tweet(this.parameters.tweet as string);
|
const response = await this.client.createTweet.run(
|
||||||
return tweet;
|
this.client.parameters.tweet as string
|
||||||
|
);
|
||||||
|
|
||||||
|
return response.data.data;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -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}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -7,6 +7,7 @@ import VerifyAccessToken from './endpoints/verify-access-token';
|
|||||||
import GetCurrentUser from './endpoints/get-current-user';
|
import GetCurrentUser from './endpoints/get-current-user';
|
||||||
import GetUserByUsername from './endpoints/get-user-by-username';
|
import GetUserByUsername from './endpoints/get-user-by-username';
|
||||||
import GetUserTweets from './endpoints/get-user-tweets';
|
import GetUserTweets from './endpoints/get-user-tweets';
|
||||||
|
import CreateTweet from './endpoints/create-tweet';
|
||||||
|
|
||||||
export default class TwitterClient {
|
export default class TwitterClient {
|
||||||
appData: IApp;
|
appData: IApp;
|
||||||
@@ -20,6 +21,7 @@ export default class TwitterClient {
|
|||||||
getCurrentUser: GetCurrentUser;
|
getCurrentUser: GetCurrentUser;
|
||||||
getUserByUsername: GetUserByUsername;
|
getUserByUsername: GetUserByUsername;
|
||||||
getUserTweets: GetUserTweets;
|
getUserTweets: GetUserTweets;
|
||||||
|
createTweet: CreateTweet;
|
||||||
|
|
||||||
static baseUrl = 'https://api.twitter.com';
|
static baseUrl = 'https://api.twitter.com';
|
||||||
|
|
||||||
@@ -55,5 +57,6 @@ export default class TwitterClient {
|
|||||||
this.getCurrentUser = new GetCurrentUser(this);
|
this.getCurrentUser = new GetCurrentUser(this);
|
||||||
this.getUserByUsername = new GetUserByUsername(this);
|
this.getUserByUsername = new GetUserByUsername(this);
|
||||||
this.getUserTweets = new GetUserTweets(this);
|
this.getUserTweets = new GetUserTweets(this);
|
||||||
|
this.createTweet = new CreateTweet(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -25,6 +25,6 @@ export default class Twitter implements IService {
|
|||||||
|
|
||||||
this.authenticationClient = new Authentication(this.client);
|
this.authenticationClient = new Authentication(this.client);
|
||||||
this.triggers = new Triggers(this.client);
|
this.triggers = new Triggers(this.client);
|
||||||
// this.actions = new Actions(connectionData, parameters);
|
this.actions = new Actions(this.client);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user