import Twitter from 'twitter'
import S3TweetRepository from './lib/s3-tweet-repository'
import slscrypt from '../node_modules/serverless-crypt/dists/slscrypt'

const tweetRepository = new S3TweetRepository(process.env.TWEET_CACHE_BUCKET_NAME)

const handler = (event, context, callback) => {
  return createTwitterClient(process.env.TWITTER_REST_BASE_URL)
    .then(client => {
      const params = {
        screen_name: 'RealTimeWWII',
        trim_user: true,
        exclude_replies: true,
        include_rts: false,
        count: 200,
        tweet_mode: 'extended'
      }
      return client.get('statuses/user_timeline', params)
    })
    .then(tweets => tweetRepository.saveLatestTweets(tweets))
    .catch(err => callback(err))
}

const createTwitterClient = async (twitterRestBaseUrl) => {
  return new Twitter({
    consumer_key: await slscrypt.get('twitter_consumer_key'),
    consumer_secret: await slscrypt.get('twitter_consumer_secret'),
    access_token_key: await slscrypt.get('twitter_access_token_key'),
    access_token_secret: await slscrypt.get('twitter_access_token_secret'),
    rest_base: twitterRestBaseUrl
  })
}

export {handler}

@@ -247,163 +247,42 @@
 nst 
-handler = (event, context, callback) =%3E %7B%0A  return createTwitterClient(process.env.TWITTER_REST_BASE_URL)%0A    .then(client =%3E %7B%0A      const params = %7B%0A    
+createParams = () =%3E %7B%0A  return %7B%0A
     
@@ -310,20 +310,16 @@
 eWWII',%0A
-    
     trim
@@ -335,20 +335,16 @@
 ue,%0A    
- 
-   
 exclude_
@@ -362,20 +362,16 @@
 ue,%0A    
-   
- 
 include_
@@ -382,20 +382,16 @@
  false,%0A
-    
     coun
@@ -402,20 +402,16 @@
 00,%0A    
- 
-   
 tweet_mo
@@ -431,26 +431,179 @@
 '%0A  
-    %7D%0A      return
+%7D%0A%7D%0A%0Aconst handler = async (event, context, callback) =%3E %7B%0A  try %7B%0A    const client = await createTwitterClient(process.env.TWITTER_REST_BASE_URL)%0A    const tweets = await
  cli
@@ -640,42 +640,33 @@
 e', 
-params)%0A    %7D)%0A    .then(tweets =%3E
+createParams())%0A    await
  twe
@@ -706,27 +706,30 @@
 ets)
-)
 %0A  
+%7D
  
- .
 catch
+ 
 (err
- =%3E
+) %7B%0A   
  cal
@@ -738,17 +738,20 @@
 ack(err)
-)
+%0A  %7D
 %0A%7D%0A%0Acons

Convert promise chain to async await.