1414 * permissions and limitations under the License.
1515 */
1616import { recognition_endpoints } from '../endpoints/recognition_endpoints.js' ;
17+ import { common_functions } from '../functions/index.js' ;
1718
1819class RecognitionService {
1920 constructor ( server , port , options , key ) {
2021 this . server = server ;
2122 this . port = port ;
2223 this . options = options ;
2324 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" ;
7627 }
7728
7829 /**
@@ -81,6 +32,7 @@ class RecognitionService {
8132 * @returns {Promise }
8233 */
8334 recognize ( image_path , options ) {
35+ const { get_full_url, add_options_to_url } = common_functions ;
8436 // add extra parameter(s) name with true value if it is referenced in API documentation for particular endpoint
8537 // add_options_to_url() adds this parameter to url if user passes some value as option otherwise function ignores this parameter
8638 let required_url_parameters = {
@@ -92,9 +44,8 @@ class RecognitionService {
9244 } ;
9345
9446 // 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 ) ;
9849
9950 return new Promise ( ( resolve , reject ) => {
10051 recognition_endpoints . recognize_face_request ( image_path , url , this . key )
@@ -112,7 +63,8 @@ class RecognitionService {
11263 * @returns {Object }
11364 */
11465 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 )
11668 let key = this . key ;
11769 let that = this ;
11870
@@ -150,7 +102,7 @@ class RecognitionService {
150102
151103 // add parameters to basic url
152104 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 ) ;
154106
155107 return new Promise ( ( resolve , reject ) => {
156108 if ( isUrl ) {
@@ -192,7 +144,7 @@ class RecognitionService {
192144 } ;
193145 // add parameters to basic url
194146 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 ) ;
196148
197149 return new Promise ( ( resolve , reject ) => {
198150 recognition_endpoints . verify_face_request ( image_path , url , key )
0 commit comments