1+ /*
2+ * Copyright (c) 2020 the original author or authors
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * https://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,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13+ * or implied. See the License for the specific language governing
14+ * permissions and limitations under the License.
15+ */
16+
17+ import { detection_endpoints } from '../endpoints/detection_endpoints.js' ;
18+
19+ class DetectionService {
20+ constructor ( server , port , options , key ) {
21+ this . server = server ;
22+ this . port = port ;
23+ this . options = options ;
24+ this . key = key ;
25+ }
26+
27+ /**
28+ * Construct full url from given server and port number
29+ * @returns {String }
30+ */
31+ get_full_url ( ) {
32+ let destination = 'api/v1/detection/detect' ;
33+ let full_url = `${ this . server } :${ this . port } /${ destination } ` ;
34+
35+ return full_url ;
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 ;
76+ }
77+
78+ detect ( image_path , options ) {
79+ // add extra parameter(s) name with true value if it is referenced in API documentation for particular endpoint
80+ // add_options_to_url() adds this parameter to url if user passes some value as option otherwise function ignores this parameter
81+ let required_url_parameters = {
82+ limit : true ,
83+ det_prob_threshold : true ,
84+ prediction_count : true ,
85+ face_plugins : true ,
86+ status : true
87+ } ;
88+ // add parameters to basic url
89+ let url = this . add_options_to_url ( this . get_full_url ( ) , options , required_url_parameters ) ;
90+
91+ return new Promise ( ( resolve , reject ) => {
92+ detection_endpoints . detect_request ( image_path , url , this . key )
93+ . then ( response => {
94+ resolve ( response . data )
95+ } )
96+ . catch ( error => {
97+ reject ( error . response . data )
98+ } )
99+ } )
100+ }
101+ }
102+
103+ export { DetectionService }
0 commit comments