import Dotenv from 'dotenv';
import goodGuyHttp from 'good-guy-http';
import url from 'url';

Dotenv.config();

// List: "DMIRS accounts"
// https://twitter.com/CommerceWA/lists/dmirs-accounts
const dmirsEndpointUrl = url.format({
  protocol: 'https',
  hostname: 'api.twitter.com',
  pathname: '1.1/lists/statuses.json',
  query: {
    count: 50,
    include_rts: true,
    // If this is passed as a Number, JavaScript will "round it" and break the URL
    list_id: '864326054462095361',
  },
});

// List: "Perth transport"
// https://twitter.com/CommerceWA/lists/perth-transport
const transportEndpointUrl = url.format({
  protocol: 'https',
  hostname: 'api.twitter.com',
  pathname: '1.1/lists/statuses.json',
  query: {
    count: 50,
    include_rts: true,
    // If this is passed as a Number, JavaScript will "round it" and break the URL
    list_id: '864392667689398272',
  },
});

if (!process.env.TWITTER_BEARER_TOKEN) {
  throw Error('Error: Twitter bearer token must be set in TWITTER_BEARER_TOKEN environment variable.');
}

const goodGuy = goodGuyHttp({
  defaultCaching: {
    timeToLive: 60000,
  },
  headers: {
    Authorization: `Bearer ${process.env.TWITTER_BEARER_TOKEN}`,
  },
  json: true,
  proxy: process.env.HTTP_PROXY,
  timeout: 15000,
});

// Transport related tweets from a list owned by @CommerceWA on Twitter
exports.getTweets = async function getTweets(request, reply) {
  let twitterEndpointUrl = '';
  if (request.params.list === 'dmirs') {
    twitterEndpointUrl = dmirsEndpointUrl;
  } else if (request.params.list === 'transport') {
    twitterEndpointUrl = transportEndpointUrl;
  }
  try {
    const response = await goodGuy(twitterEndpointUrl);
    const tweets = response.body;
    // console.dir(tweets);
    // console.log(`Number of tweets = ${tweets.length}`);
    reply(tweets);
  } catch (error) {
    console.log('Fetch of tweets failed', error);
  }
};

@@ -885,16 +885,81 @@
 %7D,%0A%7D);%0A%0A
+exports.getTweets = async function getTweets(request, reply) %7B%0A  
 if (!pro
@@ -991,16 +991,18 @@
 OKEN) %7B%0A
+  
   throw 
@@ -1101,11 +1101,15 @@
 ');%0A
+  
 %7D%0A%0A
+  
 cons
@@ -1134,16 +1134,18 @@
 yHttp(%7B%0A
+  
   defaul
@@ -1152,24 +1152,26 @@
 tCaching: %7B%0A
+  
     timeToLi
@@ -1181,23 +1181,27 @@
  60000,%0A
+  
   %7D,%0A  
+ 
+ 
 headers:
@@ -1199,24 +1199,26 @@
  headers: %7B%0A
+  
     Authoriz
@@ -1270,21 +1270,25 @@
 OKEN%7D%60,%0A
+  
   %7D,%0A
+  
   json: 
@@ -1293,16 +1293,18 @@
 : true,%0A
+  
   proxy:
@@ -1328,16 +1328,18 @@
 _PROXY,%0A
+  
   timeou
@@ -1352,148 +1352,15 @@
 00,%0A
+  
 %7D);%0A%0A
-// Transport related tweets from a list owned by @CommerceWA on Twitter%0Aexports.getTweets = async function getTweets(request, reply) %7B%0A
   le

Handle missing Twitter configuration better