const Config = require('../config.json');
const fetch = require('node-fetch');

module.exports = class Fetcher {
  get(url) {
    const options = { method: 'GET' };
    return this.makeRequest(url, options);
  }

  post(url, params) {
    const body = [];
    for (const key in params) {
      body.push(key + '=' + encodeURIComponent(params[key]));
    }
    const options = { method: 'POST', body: body.join('&') };
    return this.makeRequest(url, options);
  }

  makeRequest(url, options) {
    if (typeof options.headers === 'undefined') {
      options.headers = {};
    }
    options.headers['User-Agent'] = Config.user_agent;
    return new Promise((resolve, reject) => {
      fetch(url, options).then((response) => {
        const respType = response.headers.get('Content-Type');
        const status = response.status + ' ' + response.statusText;
        if (respType === 'text/html') {
          response.text().then((body) => {
            if (response.ok) {
              resolve({ body: body });
            } else {
              reject({ url: url, status: status, body: body });
            }
          }).catch((error) => {
            console.error('failed to parse html response', error);
            reject({ error: error });
          });
        } else {
          response.json().then((json) => {
            if (response.ok) {
              resolve(json);
            } else {
              json.url = url;
              json.status = status;
              reject(json);
            }
          }).catch((error) => {
            console.error('failed to parse json response', error);
            reject({ error: error });
          });
        }
      });
    });
  }
}

@@ -733,25 +733,12 @@
     
-const respType = 
+if (
 resp
@@ -773,122 +773,256 @@
 pe')
-;%0A        const status = response.status + ' ' + response.statusText;%0A        if (respType === 'text/html'
+ === 'text/html') %7B%0A          this.handleHtmlResponse(response, resolve, reject);%0A        %7D else %7B%0A          this.handleJsonResponse(response, resolve, reject);%0A        %7D%0A      %7D);%0A    %7D);%0A  %7D%0A%0A  handleHtmlResponse(response, resolve, reject
 ) %7B%0A    
     
@@ -1013,30 +1013,24 @@
 ject) %7B%0A    
-   
-   
 response.tex
@@ -1046,30 +1046,24 @@
 (body) =%3E %7B%0A
-      
       if (re
@@ -1075,38 +1075,32 @@
 e.ok) %7B%0A        
-      
 resolve(%7B body: 
@@ -1106,38 +1106,32 @@
  body %7D);%0A      
-  
-    
 %7D else %7B%0A       
@@ -1121,32 +1121,75 @@
  %7D else %7B%0A      
+  const status = this.getStatus(response);%0A
         reject(%7B
@@ -1232,38 +1232,26 @@
 y %7D);%0A      
-     
- 
 %7D%0A
-      
     %7D).catch
@@ -1256,38 +1256,32 @@
 ch((error) =%3E %7B%0A
-      
       console.er
@@ -1323,38 +1323,32 @@
 , error);%0A      
-     
- 
 reject(%7B error: 
@@ -1365,41 +1365,67 @@
     
-      %7D);%0A        %7D else %7B%0A      
+%7D);%0A  %7D%0A%0A  handleJsonResponse(response, resolve, reject) %7B%0A
     
@@ -1453,30 +1453,24 @@
 (json) =%3E %7B%0A
-      
       if (re
@@ -1490,22 +1490,16 @@
         
-      
 resolve(
@@ -1511,22 +1511,16 @@
 ;%0A      
-   
-   
 %7D else %7B
@@ -1524,30 +1524,24 @@
 e %7B%0A        
-     
- 
 json.url = u
@@ -1552,22 +1552,16 @@
         
-      
 json.sta
@@ -1570,22 +1570,34 @@
 s = 
-status;%0A      
+this.getStatus(response);%0A
     
@@ -1620,30 +1620,18 @@
 ;%0A      
-    
-  %7D%0A      
+%7D%0A
     %7D).c
@@ -1648,22 +1648,16 @@
 r) =%3E %7B%0A
-      
       co
@@ -1715,22 +1715,16 @@
 ;%0A      
-   
-   
 reject(%7B
@@ -1741,22 +1741,16 @@
 ror %7D);%0A
-      
     %7D);%0A
@@ -1755,32 +1755,89 @@
 ;%0A  
- 
-     %7D%0A      %7D);%0A    %7D)
+%7D%0A%0A  getStatus(response) %7B%0A    return response.status + ' ' + response.statusText
 ;%0A  

Refactor fetcher makeRequest a bit