@@ -21,7 +21,6 @@ var requestFactory = require('../../lib/requestwrapper');
2121var  endpoints       =  require ( '../../lib/alchemy_endpoints.json' ) ; 
2222var  helper          =  require ( '../../lib/helper' ) ; 
2323var  isStream        =  require ( 'isstream' ) ; 
24- var  pick            =  require ( 'object.pick' ) ; 
2524var  omit            =  require ( 'object.omit' ) ; 
2625var  fs              =  require ( 'fs' ) ; 
2726
@@ -54,18 +53,33 @@ function createRequest(method) {
5453        ' needs to be specified' ) ) ; 
5554      return ; 
5655    } 
56+     // always return json 
57+     params . outputMode  =  'json' ; 
5758
5859    var  parameters  =  { 
5960      options : { 
6061        url : endpoints [ method ] [ format ] , 
61-         method : 'POST' , 
62-         json : true , 
63-         qs : extend ( { outputMode : 'json' } ,  params )  // change default output to json 
62+         method : 'POST' 
6463      } , 
6564      defaultOptions : this . _options 
6665    } ; 
6766
68-     return  requestFactory ( parameters ,  errorFormatter ( callback ) ) ; 
67+     if  ( ! params . image  ||  ! isStream ( params . image ) )  { 
68+       // url or base64 images are considered 'not-raw' 
69+       params . imagePostMode  =  'not-raw' ; 
70+       // send the parameters as form url-encoded 
71+       parameters . options . form  =  params ; 
72+       return  requestFactory ( parameters ,  errorFormatter ( callback ) ) ; 
73+     }  else  { 
74+       params . imagePostMode  =  'raw' ; 
75+       // send the parameters as query parameters 
76+       parameters . options . qs  =  omit ( params , [ 'image' ] ) ; 
77+       // add the content-length to the headers 
78+       parameters . options . headers  =  { 
79+         'Content-Length' : fs . statSync ( params . image . path ) . size 
80+       } ; 
81+       return  params . image . pipe ( requestFactory ( parameters ,  errorFormatter ( callback ) ) ) ; 
82+     } 
6983  } ; 
7084} 
7185
@@ -81,86 +95,16 @@ function AlchemyVision(options) {
8195/** 
8296 * Extracts images from a URL or html 
8397 */ 
84- AlchemyVision . prototype . getImageLinks  =  function ( _params ,  callback  )  { 
85-   var  params  =  _params  ||  { } ; 
86-   var  accepted_formats  =  Object . keys ( endpoints [ 'image_link' ] ) ; 
87-   var  format  =  helper . getFormat ( params ,  accepted_formats ) ; 
88- 
89-   if  ( format  ===  null )  { 
90-     callback ( new  Error ( 'Missing required parameters: '  +  accepted_formats . join ( ', ' )  +  ' needs to be specified' ) ) ; 
91-     return ; 
92-   } 
93- 
94-   var  parameters  =  { 
95-     options : { 
96-       url : endpoints [ 'image_link' ] [ format ] , 
97-       method : 'POST' , 
98-       json : true , 
99-       qs : extend ( { } ,  params , { outputMode : 'json' } )  // change default output to json 
100-     } , 
101-     defaultOptions : this . _options 
102-   } ; 
103- 
104-   return  requestFactory ( parameters ,  errorFormatter ( callback ) ) ; 
105- } ; 
98+ AlchemyVision . prototype . getImageLinks  =  createRequest ( 'image_link' ) ; 
10699
107100/** 
108101 * Tags image with keywords 
109102 */ 
110- AlchemyVision . prototype . getImageKeywords  =  function ( _params ,  callback )  { 
111-   var  params  =  extend ( { } ,  _params ) ; 
112-   var  accepted_formats  =  Object . keys ( endpoints [ 'image_keywords' ] ) ; 
113-   var  format  =  helper . getFormat ( params ,  accepted_formats ) ; 
114- 
115-   if  ( format  ===  null )  { 
116-     callback ( new  Error ( 'Missing required parameters: '  +  accepted_formats . join ( ', ' )  +  ' needs to be specified' ) ) ; 
117-     return ; 
118-   } 
119- 
120-   var  parameters  =  { 
121-     options : { 
122-       url : endpoints [ 'image_keywords' ] [ format ] , 
123-       method : 'POST' , 
124-       json : true , 
125-       qs : { outputMode : 'json' }  // change default output to json 
126-     } , 
127-     defaultOptions : this . _options 
128-   } ; 
129- 
130-   if  ( typeof ( params . image )  !==  'undefined' )  { 
131- 
132-     // check if image is stream or string 
133-     if  ( ( typeof ( params . image )  !==  'string' )  &&  ! isStream ( params . image ) )  { 
134-       callback ( new  Error ( 'Invalid arguments: image needs to be a stream or base64' ) ) ; 
135-       return ; 
136-     } 
137- 
138-     if  ( isStream ( params . image ) )  { 
139-       params . imagePostMode  =  'raw' ; 
140-       // handle raw images 
141-       parameters . options . body  =  params . image ; 
142- 
143-     }  else  { 
144-       params . imagePostMode  =  'not-raw' ; 
145-       parameters . options . formData  =  params ; 
146-     } 
147-   }  else  { 
148-     parameters . options . formData  =  params ; 
149-   } 
150- 
151-   return  requestFactory ( parameters ,  errorFormatter ( callback ) ) ; 
152- } ; 
103+ AlchemyVision . prototype . getImageKeywords  =  createRequest ( 'image_keywords' ) ; 
153104
154105/** 
155106 * Face detection and Recognition 
156107 */ 
157- AlchemyVision . prototype . recognizeFaces  =  function ( _params ,  callback )  { 
158-   var  params  =  _params  ||  { } ; 
159- 
160-   if  ( params . image  &&  typeof ( params . image )  !==  'string' ) 
161-     params . imagePostMode  =  'raw' ; 
162- 
163-   return  createRequest ( 'image_recognition' ) . call ( this ,  params ,  callback ) ; 
164- } ; 
108+ AlchemyVision . prototype . recognizeFaces  =  createRequest ( 'image_recognition' ) ; 
165109
166110module . exports  =  AlchemyVision ; 
0 commit comments