Browse Source

Created common service abstraction

This patch creates a common class named AbstractService from which
all API classes can inherit common logic.

Change-Id: I18339ac19ed2b97a7b9b3d6a0f1e01292080c422
Michael Krotscheck 2 years ago
parent
commit
acbc25ce99
3 changed files with 45 additions and 6 deletions
  1. 4
    3
      src/glance.js
  2. 4
    3
      src/keystone.js
  3. 37
    0
      src/util/abstract_service.js

+ 4
- 3
src/glance.js View File

@@ -13,7 +13,7 @@
13 13
  * the License for the specific language governing permissions and limitations
14 14
  * under the License.
15 15
  */
16
-import Http from './util/http';
16
+import AbstractService from './util/abstract_service';
17 17
 
18 18
 /**
19 19
  * A list of all supported versions. Please keep this array sorted by most recent.
@@ -25,7 +25,7 @@ const supportedGlanceVersions = [
25 25
   'v2.3'
26 26
 ];
27 27
 
28
-export default class Glance {
28
+export default class Glance extends AbstractService {
29 29
 
30 30
   /**
31 31
    * This class provides direct, idempotent, low-level access to the Glance API of a specific
@@ -43,6 +43,8 @@ export default class Glance {
43 43
    * @param {{}} endpointConfig The configuration element for a specific glance endpoint.
44 44
    */
45 45
   constructor (endpointConfig) {
46
+    super();
47
+
46 48
     // Sanity checks.
47 49
     if (!endpointConfig || !endpointConfig.url) {
48 50
       throw new Error('An endpoint configuration is required.');
@@ -50,7 +52,6 @@ export default class Glance {
50 52
     // Clone the config, so that this instance is immutable
51 53
     // at runtime (no modifying the config after the fact).
52 54
     this._config = Object.assign({}, endpointConfig);
53
-    this.http = new Http();
54 55
   }
55 56
 
56 57
   /**

+ 4
- 3
src/keystone.js View File

@@ -1,4 +1,4 @@
1
-import Http from './util/http';
1
+import AbstractService from './util/abstract_service';
2 2
 
3 3
 /**
4 4
  * A list of all supported versions. Please keep this array sorted by most recent.
@@ -10,7 +10,7 @@ const supportedKeystoneVersions = [
10 10
   'v3.7'
11 11
 ];
12 12
 
13
-export default class Keystone {
13
+export default class Keystone extends AbstractService {
14 14
 
15 15
   /**
16 16
    * This class provides direct, idempotent, low-level access to the Keystone API of a specific
@@ -23,6 +23,8 @@ export default class Keystone {
23 23
    * @see http://docs.openstack.org/developer/os-client-config/#site-specific-file-locations
24 24
    */
25 25
   constructor (cloudConfig) {
26
+    super();
27
+
26 28
     // Sanity checks.
27 29
     if (!cloudConfig) {
28 30
       throw new Error('A configuration is required.');
@@ -30,7 +32,6 @@ export default class Keystone {
30 32
     // Clone the config, so that this instance is immutable
31 33
     // at runtime (no modifying the config after the fact).
32 34
     this.cloudConfig = Object.assign({}, cloudConfig);
33
-    this.http = new Http();
34 35
   }
35 36
 
36 37
   /**

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

@@ -0,0 +1,37 @@
1
+/*
2
+ * Copyright (c) 2016 Hewlett Packard Enterprise Development L.P.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5
+ * use this file except in compliance with the License. You may obtain a copy
6
+ * of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
13
+ * the License for the specific language governing permissions and limitations
14
+ * under the License.
15
+ */
16
+
17
+import Http from './http';
18
+
19
+/**
20
+ * An abstract implementation of our services, which includes logic common to all of our services.
21
+ *
22
+ * @author Michael Krotscheck
23
+ */
24
+export default class AbstractService {
25
+
26
+  /**
27
+   * Our HTTP service instance.
28
+   *
29
+   * @returns {Http} Our HTTP service instance.
30
+   */
31
+  get http () {
32
+    if (!this._http) {
33
+      this._http = new Http();
34
+    }
35
+    return this._http;
36
+  }
37
+}

Loading…
Cancel
Save