Skip to content

Instantly share code, notes, and snippets.

@krhoyt krhoyt/async.js
Created Apr 6, 2019

Embed
What would you like to do?
IBM Cloud Function to handle Watson classification of image files placed into Cloud Object Storage.
async function main( params ) {
if( params.status === 'added' ) {
const authorization = await rp( {
url: 'https://iam.ng.bluemix.net/oidc/token',
method: 'POST',
form: {
apikey: params.COS_API_KEY,
response_type: 'cloud_iam',
grant_type: 'urn:ibm:params:oauth:grant-type:apikey'
},
json: true
} );
console.log( authorization );
const contents = await rp( {
url: `https://${params.endpoint}/${params.bucket}/${params.key}`,
method: 'GET',
headers: {
Authorization: `${authorization.token_type} ${authorization.access_token}`,
'ibm-service-instance-id': params.COS_SERVICE_INSTANCE
},
encoding: null
} );
console.log( contents );
const classification = await rp( {
url: 'https://gateway.watsonplatform.net/visual-recognition/api/v3/classify',
method: 'POST',
auth: {
user: params.WATSON_USERNAME,
pass: params.WATSON_PASSWORD
},
formData: {
images_file: {
value: contents,
options: {
filename: params.key
}
}
},
qs: {
version: '2018-03-19'
},
json: true
} );
console.log( classification );
classification.bucket = params.bucket;
classification.key = params.key;
classification.endpoint = params.endpoint;
const doc = await rp( {
url: `https://${params.CLOUDANT_ACCOUNT}.cloudant.com/${params.CLOUDANT_DATABASE}`,
method: 'POST',
auth: {
user: params.CLOUDANT_USERNAME,
pass: params.CLOUDANT_PASSWORD
},
json: true,
body: classification
} );
console.log( doc );
return doc;
}
}
const rp = require( 'request-promise' );
function main( params ) {
if( params.status === 'added' ) {
return rp( {
url: 'https://iam.ng.bluemix.net/oidc/token',
method: 'POST',
form: {
apikey: params.COS_API_KEY,
response_type: 'cloud_iam',
grant_type: 'urn:ibm:params:oauth:grant-type:apikey'
},
json: true
} )
.then( ( authorization ) => {
return rp( {
url: `https://${params.endpoint}/${params.bucket}/${params.key}`,
method: 'GET',
headers: {
Authorization: `${authorization.token_type} ${authorization.access_token}`,
'ibm-service-instance-id': params.COS_SERVICE_INSTANCE
},
encoding: null
} );
} )
.then( ( contents ) => {
return rp( {
url: 'https://gateway.watsonplatform.net/visual-recognition/api/v3/classify',
method: 'POST',
auth: {
user: params.WATSON_USERNAME,
pass: params.WATSON_PASSWORD
},
formData: {
images_file: {
value: contents,
options: {
filename: params.key
}
}
},
qs: {
version: '2018-03-19'
},
json: true
} );
} )
.then( ( classification ) => {
classification.bucket = params.bucket;
classification.key = params.key;
classification.endpoint = params.endpoint;
return rp( {
url: `https://${params.CLOUDANT_ACCOUNT}.cloudant.com/${params.CLOUDANT_DATABASE}`,
method: 'POST',
auth: {
user: params.CLOUDANT_USERNAME,
pass: params.CLOUDANT_PASSWORD
},
json: true,
body: classification
} )
} )
.then( ( doc ) => {
console.log( doc );
return doc;
} );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.