14
14
* permissions and limitations under the License.
15
15
*/
16
16
import { recognition_endpoints } from '../endpoints/recognition_endpoints.js' ;
17
+ import { common_functions } from '../functions/index.js' ;
17
18
18
19
class RecognitionService {
19
20
constructor ( server , port , options , key ) {
20
21
this . server = server ;
21
22
this . port = port ;
22
23
this . options = options ;
23
24
this . key = key ;
24
- }
25
-
26
- /**
27
- * Construct full url from given server and port number
28
- * @returns {String }
29
- */
30
- get_full_url ( isUrlForRecognition ) {
31
- let destination = isUrlForRecognition ? 'api/v1/recognition' : 'api/v1/recognition/faces' ;
32
- let full_url = `${ this . server } :${ this . port } /${ destination } ` ;
33
-
34
- return full_url ;
35
- }
36
-
37
- /**
38
- * Add extra options to url
39
- * @param {Object } options
40
- * @returns {String }
41
- */
42
- add_options_to_url ( url , localOptions , required_parameters ) {
43
- // merge options passed by localy and globally NOTE: global options will override local on if same value passed from both of them
44
- let uniqueOptions = { ...localOptions , ...this . options } ;
45
- let isThereAnyOptions = Object . keys ( uniqueOptions ) ;
46
-
47
- // check whether any parameters passed
48
- if ( isThereAnyOptions . length > 0 ) {
49
- // check whether limit parameter passed and it is required for particular endpoint (ex: it is not requrid for add())
50
- if ( uniqueOptions [ 'limit' ] >= 0 && required_parameters [ 'limit' ] ) {
51
- url = `${ url } ?limit=${ uniqueOptions [ 'limit' ] } `
52
- }
53
-
54
- // check whether det_prob_threshold parameter passed and is it required for particular endpoint
55
- if ( uniqueOptions [ 'det_prob_threshold' ] >= 0 && required_parameters [ 'det_prob_threshold' ] ) {
56
- url = `${ url } &det_prob_threshold=${ uniqueOptions [ 'det_prob_threshold' ] } `
57
- }
58
-
59
- // check whether prediction_count passed and is it required for particular endpoint
60
- if ( uniqueOptions [ 'prediction_count' ] >= 0 && required_parameters [ 'prediction_count' ] ) {
61
- url = `${ url } &prediction_count=${ uniqueOptions [ 'prediction_count' ] } `
62
- }
63
-
64
- // check whether face_plugins passed and is it required for particular endpoint
65
- if ( uniqueOptions [ 'face_plugins' ] && required_parameters [ 'face_plugins' ] ) {
66
- url = `${ url } &face_plugins=${ uniqueOptions [ 'face_plugins' ] } `
67
- }
68
-
69
- // check whether status passed and is it required for particular endpoint
70
- if ( uniqueOptions [ 'status' ] && required_parameters [ 'status' ] ) {
71
- url = `${ url } &status=${ uniqueOptions [ 'status' ] } `
72
- }
73
- }
74
-
75
- return url ;
25
+ this . base_url = 'api/v1/recognition/faces' ;
26
+ this . recognize_base_url = "api/v1/recognition/recognize" ;
76
27
}
77
28
78
29
/**
@@ -81,6 +32,7 @@ class RecognitionService {
81
32
* @returns {Promise }
82
33
*/
83
34
recognize ( image_path , options ) {
35
+ const { get_full_url, add_options_to_url } = common_functions ;
84
36
// add extra parameter(s) name with true value if it is referenced in API documentation for particular endpoint
85
37
// add_options_to_url() adds this parameter to url if user passes some value as option otherwise function ignores this parameter
86
38
let required_url_parameters = {
@@ -92,9 +44,8 @@ class RecognitionService {
92
44
} ;
93
45
94
46
// add parameters to basic url
95
- let url = this . get_full_url ( true ) ;
96
- url = `${ url } /recognize` ;
97
- url = this . add_options_to_url ( url , options , required_url_parameters ) ;
47
+ let full_url = get_full_url ( this . recognize_base_url , this . server , this . port )
48
+ let url = add_options_to_url ( full_url , this . options , options , required_url_parameters ) ;
98
49
99
50
return new Promise ( ( resolve , reject ) => {
100
51
recognition_endpoints . recognize_face_request ( image_path , url , this . key )
@@ -112,7 +63,8 @@ class RecognitionService {
112
63
* @returns {Object }
113
64
*/
114
65
getFaceCollection ( ) {
115
- let url = this . get_full_url ( ) ;
66
+ const { get_full_url, add_options_to_url } = common_functions ;
67
+ let url = get_full_url ( this . base_url , this . server , this . port )
116
68
let key = this . key ;
117
69
let that = this ;
118
70
@@ -150,7 +102,7 @@ class RecognitionService {
150
102
151
103
// add parameters to basic url
152
104
url = `${ url } ?subject=${ subject } `
153
- url = that . add_options_to_url ( url , options , required_url_parameters ) ;
105
+ url = add_options_to_url ( url , that . options , options , required_url_parameters ) ;
154
106
155
107
return new Promise ( ( resolve , reject ) => {
156
108
if ( isUrl ) {
@@ -192,7 +144,7 @@ class RecognitionService {
192
144
} ;
193
145
// add parameters to basic url
194
146
url = `${ url } /${ image_id } /verify` ;
195
- url = that . add_options_to_url ( url , options , required_url_parameters ) ;
147
+ url = add_options_to_url ( url , that . options , options , required_url_parameters ) ;
196
148
197
149
return new Promise ( ( resolve , reject ) => {
198
150
recognition_endpoints . verify_face_request ( image_path , url , key )
0 commit comments