Browse Source

Add more service abstraction

This patch add _requestComponents and serviceEndpoint methods to
abstract service, as these methods ared used for both Keystone and
Glance services.

Change-Id: Iccc334c0bbd79c1207855260932dab984f2d9d6c
tags/0.0.1
Corentin Ardeois 2 years ago
parent
commit
773b2b8ee3
3 changed files with 47 additions and 92 deletions
  1. 0
    45
      src/glance.js
  2. 0
    47
      src/keystone.js
  3. 47
    0
      src/util/abstract_service.js

+ 0
- 45
src/glance.js View File

@@ -55,51 +55,6 @@ export default class Glance extends AbstractService {
55 55
     this._config = endpointConfig;
56 56
   }
57 57
 
58
-  /**
59
-   * This method resolves any passed token into an appropriate header, as well as the base URL
60
-   * for the glance API. these variables may then be used to feed other requests.
61
-   *
62
-   * @param {Promise|String} token A promise, or string, representing a token.
63
-   * @returns {Promise} A promise which resolves with [url, token].
64
-   * @private
65
-   */
66
-  _requestComponents (token = null) {
67
-    // Make sure the token is a promise.
68
-    let headerPromise = new Promise((resolve) => resolve(token))
69
-      .then((token) => {
70
-        if (token) {
71
-          return {
72
-            'X-Auth-Token': token
73
-          };
74
-        }
75
-        return {};
76
-      });
77
-    return Promise.all([this.serviceEndpoint(), headerPromise]);
78
-  }
79
-
80
-  /**
81
-   * Return the root API endpoint for the current supported glance version.
82
-   *
83
-   * @returns {Promise.<T>|*} A promise which will resolve with the endpoint URL string.
84
-   */
85
-  serviceEndpoint () {
86
-    if (!this._endpointPromise) {
87
-      this._endpointPromise = this.version()
88
-        .then((version) => {
89
-          if (version.links) {
90
-            for (let i = 0; i < version.links.length; i++) {
91
-              let link = version.links[i];
92
-              if (link.rel === 'self' && link.href) {
93
-                return link.href;
94
-              }
95
-            }
96
-          }
97
-          throw new Error("No service endpoint discovered.");
98
-        });
99
-    }
100
-    return this._endpointPromise;
101
-  }
102
-
103 58
   /**
104 59
    * List the images available on glance.
105 60
    *

+ 0
- 47
src/keystone.js View File

@@ -58,30 +58,6 @@ export default class Keystone extends AbstractService {
58 58
     return pointer;
59 59
   }
60 60
 
61
-  /**
62
-   * This method builds common components of a keystone request. It converts any passed token
63
-   * into a promise, resolves the base URL, and then passes the results as an .all() promise,
64
-   * which may be destructured in a followup request.
65
-   *
66
-   * @param {Promise|String} token A promise, or string, representing a token.
67
-   * @returns {Promise} A promise which resolves with [url, token].
68
-   * @private
69
-   */
70
-  _requestComponents (token = null) {
71
-    // Make sure the token is a promise.
72
-    let headerPromise = Promise
73
-      .resolve(token)
74
-      .then((token) => {
75
-        if (token) {
76
-          return {
77
-            'X-Auth-Token': token
78
-          };
79
-        }
80
-        return {};
81
-      });
82
-    return Promise.all([this.serviceEndpoint(), headerPromise]);
83
-  }
84
-
85 61
   /**
86 62
    * Retrieve all the API versions available.
87 63
    *
@@ -92,29 +68,6 @@ export default class Keystone extends AbstractService {
92 68
       .then((versions) => versions.values);
93 69
   }
94 70
 
95
-  /**
96
-   * Return the root API endpoint for the current supported keystone version.
97
-   *
98
-   * @returns {Promise.<T>|*} A promise which will resolve with the endpoint URL string.
99
-   */
100
-  serviceEndpoint () {
101
-    if (!this._endpointPromise) {
102
-      this._endpointPromise = this.version()
103
-        .then((version) => {
104
-          if (version.links) {
105
-            for (let i = 0; i < version.links.length; i++) {
106
-              let link = version.links[i];
107
-              if (link.rel === 'self' && link.href) {
108
-                return link.href;
109
-              }
110
-            }
111
-          }
112
-          throw new Error("No service endpoint discovered.");
113
-        });
114
-    }
115
-    return this._endpointPromise;
116
-  }
117
-
118 71
   /**
119 72
    * Issue a token from the provided credentials. Credentials will be read from the
120 73
    * configuration, unless they have been explicitly provided. Note that both the userDomainName

+ 47
- 0
src/util/abstract_service.js View File

@@ -80,4 +80,51 @@ export default class AbstractService {
80 80
         throw new Error("No supported API version available.");
81 81
       });
82 82
   }
83
+
84
+  /**
85
+   * Return the root API endpoint for the current supported glance version.
86
+   *
87
+   * @returns {Promise.<T>|*} A promise which will resolve with the endpoint URL string.
88
+   */
89
+  serviceEndpoint () {
90
+    if (!this._endpointPromise) {
91
+      this._endpointPromise = this.version()
92
+        .then((version) => {
93
+          if (version.links) {
94
+            for (let i = 0; i < version.links.length; i++) {
95
+              let link = version.links[i];
96
+              if (link.rel === 'self' && link.href) {
97
+                return link.href;
98
+              }
99
+            }
100
+          }
101
+          throw new Error("No service endpoint discovered.");
102
+        });
103
+    }
104
+    return this._endpointPromise;
105
+  }
106
+
107
+  /**
108
+   * This method builds common components for a request to the implemented service.
109
+   * It converts any passed token into a promise, resolves the base URL, and then passes
110
+   * the results as an .all() promise, which may be destructured in a followup request.
111
+   *
112
+   * @param {Promise|String} token A promise, or string, representing a token.
113
+   * @returns {Promise} A promise which resolves with [url, token].
114
+   * @private
115
+   */
116
+  _requestComponents (token = null) {
117
+    // Make sure the token is a promise.
118
+    let headerPromise = Promise
119
+      .resolve(token)
120
+      .then((token) => {
121
+        if (token) {
122
+          return {
123
+            'X-Auth-Token': token
124
+          };
125
+        }
126
+        return {};
127
+      });
128
+    return Promise.all([this.serviceEndpoint(), headerPromise]);
129
+  }
83 130
 }

Loading…
Cancel
Save