Просмотр исходного кода

replace restQiitaAPI function with restQiitaAPIService class

yusuketk 7 лет назад
Родитель
Сommit
009ca1e243
4 измененных файлов с 105 добавлено и 69 удалено
  1. 18 1
      lib/crowi/index.js
  2. 84 0
      lib/service/rest-qiita-API.js
  3. 3 9
      lib/util/importer.js
  4. 0 59
      lib/util/restQiitaAPI.js

+ 18 - 1
lib/crowi/index.js

@@ -38,6 +38,7 @@ function Crowi(rootdir, env) {
   this.interceptorManager = {};
   this.passportService = null;
   this.globalNotificationService = null;
+  this.restQiitaAPIService = null;
   this.xss = new Xss();
 
   this.tokens = null;
@@ -93,6 +94,8 @@ Crowi.prototype.init = function() {
       return self.setupCsrf();
     }).then(function() {
       return self.setUpGlobalNotification();
+    }).then(function() {
+      return self.setUpRestQiitaAPI();
     });
 };
 
@@ -253,6 +256,10 @@ Crowi.prototype.getGlobalNotificationService = function() {
   return this.globalNotificationService;
 };
 
+Crowi.prototype.getRestQiitaAPIService = function() {
+  return this.restQiitaAPIService;
+};
+
 Crowi.prototype.setupPassport = function() {
   const config = this.getConfig();
   const Config = this.model('Config');
@@ -276,7 +283,7 @@ Crowi.prototype.setupPassport = function() {
     this.passportService.setupLdapStrategy();
     this.passportService.setupGoogleStrategy();
     this.passportService.setupGitHubStrategy();
-    this.passportService.setupTwitterStrategy(); 
+    this.passportService.setupTwitterStrategy();
   }
   catch (err) {
     logger.error(err);
@@ -470,4 +477,14 @@ Crowi.prototype.setUpGlobalNotification = function() {
   }
 };
 
+/**
+ * setup RestQiitaAPIService
+ */
+Crowi.prototype.setUpRestQiitaAPI = function() {
+  const RestQiitaAPIService = require('../service/rest-qiita-API');
+  if (this.restQiitaAPIService == null) {
+    this.restQiitaAPIService = new RestQiitaAPIService(this);
+  }
+};
+
 module.exports = Crowi;

+ 84 - 0
lib/service/rest-qiita-API.js

@@ -0,0 +1,84 @@
+const debug = require('debug')('growi:service:GlobalNotification');
+
+function getAxios(team, token) {
+  return require('axios').create({
+    baseURL: `https://${team}.qiita.com/api/v2`,
+    headers: {
+      'Content-Type': 'application/json',
+      'X-Requested-With': 'XMLHttpRequest',
+      'authorization': `Bearer ${token}`
+    },
+    responseType: 'json'
+  });
+};
+
+/**
+ * the service class of restQiitaAPI
+ * Qiita API v2 documant https://qiita.com/api/v2/docs
+ */
+
+class RestQiitaAPIService {
+
+  constructor(crowi) {
+    this.crowi = crowi;
+    this.config = crowi.getConfig();
+    this.team = this.config.crowi['importer:qiita:team_name'];
+    this.token = this.config.crowi['importer:qiita:access_token'];
+    this.axios = getAxios(this.team, this.token);
+  };
+
+  /**
+   * get Qiita API
+   * @memberof RestQiitaAPI
+   * @param {string} path
+   */
+  async restAPI(path) {
+    return this.axios.get(path)
+      .then(function(res) {
+        const data = res.data;
+        const total = res.headers['total-count'];
+        return {data, total};
+      })
+      .catch(function(err) {
+        return err;
+      });
+  };
+
+  /**
+   * get Qiita user
+   * @memberof RestQiitaAPI
+   */
+  async getQiitaUser() {
+    try {
+      const res = await this.restAPI('/users');
+      const user = res.data;
+      if(user.length > 0) {
+        return user;
+      }
+    } catch (err) {
+      throw err;
+    }
+  };
+
+
+  /**
+   * get Qiita pages
+   * @memberof RestQiitaAPI
+   * @param {string} pageNum
+   * @param {string} per_page
+   */
+  async getQiitaPages(pageNum, per_page) {
+    try {
+      const res = await this.restAPI(`/items?page=${pageNum}&per_page=${per_page}`);
+      const pages = res.data;
+      const total = res.total;
+      if(pages.length > 0) {
+        return {pages, total};
+      }
+    } catch (err) {
+      throw err;
+    }
+  };
+}
+
+module.exports = RestQiitaAPIService;

+ 3 - 9
lib/util/importer.js

@@ -8,8 +8,7 @@ module.exports = crowi => {
   const esa = require('esa-nodejs');
   const config = crowi.getConfig();
   const createGrowiPages = require('./createGrowiPagesFromImports')(crowi);
-  const restQiitaAPI = require('./restQiitaAPI');
-  let qiitaClient;
+  const restQiitaAPIService = crowi.getRestQiitaAPIService();
   let importer = {};
   let esaClient = {};
 
@@ -22,11 +21,6 @@ module.exports = crowi => {
       accessToken: config.crowi['importer:esa:access_token'],
     });
     logger.info('initialize esa importer');
-    qiitaClient = restQiitaAPI(
-      config.crowi['importer:qiita:team_name'],
-      config.crowi['importer:qiita:access_token']
-    );
-    logger.info('initialize qiita importer');
   };
 
   /**
@@ -83,7 +77,7 @@ module.exports = crowi => {
   const importPostsFromQiita = (pageNum, user, errors) => {
     return new Promise((resolve, reject) => {
       const per_page = '100';
-      qiitaClient.getQiitaPages(pageNum, per_page)
+      restQiitaAPIService.getQiitaPages(pageNum, per_page)
       .then(function(res) {
         const next = pageNum * per_page + 1;
         const postsReceived = res.pages;
@@ -171,7 +165,7 @@ module.exports = crowi => {
    */
   importer.testConnectionToQiita = async() => {
     try {
-      await qiitaClient.getQiitaUser();
+      await restQiitaAPIService.getQiitaUser();
     } catch (err) {
       throw err;
     };

+ 0 - 59
lib/util/restQiitaAPI.js

@@ -1,59 +0,0 @@
-'use strict';
-
-// Qiita API v2 documant https://qiita.com/api/v2/docs
-
-module.exports = function(qiitaTeam, qiitaToken) {
-  var restQiitaAPI = {};
-  const team = qiitaTeam;
-  const token = qiitaToken;
-
-  const axiosBase = require('axios');
-  const axios = axiosBase.create({
-    baseURL: `https://${team}.qiita.com/api/v2`,
-    headers: {
-      'Content-Type': 'application/json',
-      'X-Requested-With': 'XMLHttpRequest',
-      'authorization': `Bearer ${token}`
-    },
-    responseType: 'json'
-  });
-
-  function restAPI(path) {
-    return axios.get(path)
-      .then(function(res) {
-        const data = res.data;
-        const total = res.headers['total-count'];
-        return {data, total};
-      })
-      .catch(function(err) {
-        return err;
-      });
-  };
-
-  restQiitaAPI.getQiitaUser = async function() {
-    try {
-      const res = await restAPI('/users');
-      const user = res.data;
-      if(user.length > 0) {
-        return user;
-      }
-    } catch (err) {
-      throw err;
-    }
-  };
-
-  restQiitaAPI.getQiitaPages = async function(pageNum, per_page) {
-    try {
-      const res = await restAPI(`/items?page=${pageNum}&per_page=${per_page}`);
-      const pages = res.data;
-      const total = res.total;
-      if(pages.length > 0) {
-        return {pages, total};
-      }
-    } catch (err) {
-      throw err;
-    }
-  };
-
-  return restQiitaAPI;
-}