Add blob trigger function + http client
This commit is contained in:
Родитель
ba027895c1
Коммит
0b73b7dc62
|
@ -30,9 +30,6 @@ async function storeValues(context: Context, blob: StorageResponse): Promise<boo
|
|||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
export async function run(context: Context, req: HttpRequest) {
|
||||
|
||||
// Data can be sent as an url to a blob storage or directly as form-data
|
||||
|
|
|
@ -8,5 +8,5 @@
|
|||
"connection": "AzureWebJobsStorage"
|
||||
}
|
||||
],
|
||||
"disabled": true
|
||||
"disabled": false
|
||||
}
|
|
@ -1,13 +1,29 @@
|
|||
import {Context} from '@azure/functions';
|
||||
import {HttpClient} from '../common/http-client';
|
||||
|
||||
var hostname = process.env["WEBSITE_HOSTNAME"];
|
||||
// The purpose of this function is to automatically trigger the official endpoint ("api"-function)
|
||||
// once a new document is uploaded to the data container in the storage. If you want to disable
|
||||
// this automatic call, just disable the function
|
||||
|
||||
const hostname: string = process.env["WEBSITE_HOSTNAME"];
|
||||
const storageAccount: string = process.env["AzureWebJobsStorage"].split(";")[1].replace("AccountName=", "");
|
||||
const httpClient = HttpClient.getInstance();
|
||||
|
||||
export async function run(context: Context, docblob: Buffer) {
|
||||
|
||||
const docPath = context.bindingData.blobTrigger;
|
||||
const docName = docPath.split("/")[1];
|
||||
const docBlob = context.bindings.docblob;
|
||||
|
||||
context.log('document-processor triggered by storage blob');
|
||||
|
||||
var prefix = "";
|
||||
if(hostname.indexOf("localhost") == 0) {
|
||||
prefix = "http://"
|
||||
} else {
|
||||
prefix = "https://"
|
||||
}
|
||||
|
||||
const docPath = context.bindingData.blobTrigger;
|
||||
var url = prefix + hostname + "/api/storevalues?source=https://" + storageAccount + ".blob.core.windows.net/" + docPath;
|
||||
|
||||
context.log("Calling url " + url);
|
||||
httpClient.post(url, {});
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,63 @@
|
|||
"use strict";
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __importStar = (this && this.__importStar) || function (mod) {
|
||||
if (mod && mod.__esModule) return mod;
|
||||
var result = {};
|
||||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
||||
result["default"] = mod;
|
||||
return result;
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const request = __importStar(require("superagent"));
|
||||
const url = __importStar(require("url"));
|
||||
class HttpClient {
|
||||
constructor() {
|
||||
if (HttpClient.instance) {
|
||||
throw new Error('Error: Instantiation failed: Use HttpClient.getInstance() instead of new.');
|
||||
}
|
||||
HttpClient.instance = this;
|
||||
}
|
||||
static getInstance() {
|
||||
return HttpClient.instance;
|
||||
}
|
||||
/**
|
||||
* @param {string} endpoint
|
||||
* @returns {Promise<any>}
|
||||
*/
|
||||
get(endpoint) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
var urlObj = {
|
||||
host: endpoint,
|
||||
};
|
||||
const response = yield request.get(url.format(urlObj));
|
||||
return response;
|
||||
});
|
||||
}
|
||||
/**
|
||||
* @param {string} endpoint
|
||||
* @param {any} payload
|
||||
* @returns {Promise<any>}
|
||||
*/
|
||||
post(endpoint, payload) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
var urlObj = {
|
||||
host: endpoint,
|
||||
};
|
||||
const response = request
|
||||
.post(url.format(urlObj))
|
||||
.send(payload)
|
||||
.set('Accept', 'application/json');
|
||||
return response;
|
||||
});
|
||||
}
|
||||
}
|
||||
HttpClient.instance = new HttpClient();
|
||||
exports.HttpClient = HttpClient;
|
||||
//# sourceMappingURL=index.js.map
|
|
@ -0,0 +1 @@
|
|||
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,oDAAsC;AACtC,yCAA2B;AAE3B,MAAa,UAAU;IAQnB;QACI,IAAI,UAAU,CAAC,QAAQ,EAAE;YACrB,MAAM,IAAI,KAAK,CAAC,2EAA2E,CAAC,CAAC;SAChG;QACD,UAAU,CAAC,QAAQ,GAAG,IAAI,CAAC;IAC/B,CAAC;IAXM,MAAM,CAAC,WAAW;QACrB,OAAO,UAAU,CAAC,QAAQ,CAAC;IAC/B,CAAC;IAWD;;;OAGG;IACU,GAAG,CAAC,QAAgB;;YAE7B,IAAI,MAAM,GAAG;gBACT,IAAI,EAAE,QAAQ;aACjB,CAAC;YAEF,MAAM,QAAQ,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC;YACvD,OAAO,QAAQ,CAAC;QAEpB,CAAC;KAAA;IAED;;;;OAIG;IACU,IAAI,CAAC,QAAgB,EAAE,OAAY;;YAE5C,IAAI,MAAM,GAAG;gBACT,IAAI,EAAE,QAAQ;aACjB,CAAC;YAEF,MAAM,QAAQ,GAAG,OAAO;iBACnB,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;iBACxB,IAAI,CAAC,OAAO,CAAC;iBACb,GAAG,CAAC,QAAQ,EAAE,kBAAkB,CAAC,CAAA;YAEtC,OAAO,QAAQ,CAAC;QAEpB,CAAC;KAAA;;AA1Cc,mBAAQ,GAAe,IAAI,UAAU,EAAE,CAAC;AAN3D,gCAkDC"}
|
|
@ -0,0 +1,54 @@
|
|||
import * as request from "superagent";
|
||||
import * as url from 'url';
|
||||
|
||||
export class HttpClient {
|
||||
|
||||
public static getInstance() {
|
||||
return HttpClient.instance;
|
||||
}
|
||||
|
||||
private static instance: HttpClient = new HttpClient();
|
||||
|
||||
constructor() {
|
||||
if (HttpClient.instance) {
|
||||
throw new Error('Error: Instantiation failed: Use HttpClient.getInstance() instead of new.');
|
||||
}
|
||||
HttpClient.instance = this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} endpoint
|
||||
* @returns {Promise<any>}
|
||||
*/
|
||||
public async get(endpoint: string): Promise<any> {
|
||||
|
||||
var urlObj = {
|
||||
host: endpoint,
|
||||
};
|
||||
|
||||
const response = await request.get(url.format(urlObj));
|
||||
return response;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} endpoint
|
||||
* @param {any} payload
|
||||
* @returns {Promise<any>}
|
||||
*/
|
||||
public async post(endpoint: string, payload: any): Promise<any> {
|
||||
|
||||
var urlObj = {
|
||||
host: endpoint,
|
||||
};
|
||||
|
||||
const response = request
|
||||
.post(url.format(urlObj))
|
||||
.send(payload)
|
||||
.set('Accept', 'application/json')
|
||||
|
||||
return response;
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -546,12 +546,22 @@
|
|||
"delayed-stream": "~1.0.0"
|
||||
}
|
||||
},
|
||||
"component-emitter": {
|
||||
"version": "1.3.0",
|
||||
"resolved": "https://registry.npmjs.org/component-emitter/-/component-emitter-1.3.0.tgz",
|
||||
"integrity": "sha512-Rd3se6QB+sO1TwqZjscQrurpEPIfO0/yYnSin6Q/rD3mOutHvUrCAhJub3r90uNb+SESBuE0QYoB90YdfatsRg=="
|
||||
},
|
||||
"concat-map": {
|
||||
"version": "0.0.1",
|
||||
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
|
||||
"integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=",
|
||||
"dev": true
|
||||
},
|
||||
"cookiejar": {
|
||||
"version": "2.1.2",
|
||||
"resolved": "https://registry.npmjs.org/cookiejar/-/cookiejar-2.1.2.tgz",
|
||||
"integrity": "sha512-Mw+adcfzPxcPeI+0WlvRrr/3lGVO0bD75SxX6811cxSh1Wbxx7xZBGK1eVtDf6si8rg2lhnUjsVLMFMfbRIuwA=="
|
||||
},
|
||||
"core-util-is": {
|
||||
"version": "1.0.2",
|
||||
"resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz",
|
||||
|
@ -749,6 +759,11 @@
|
|||
"resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz",
|
||||
"integrity": "sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw=="
|
||||
},
|
||||
"fast-safe-stringify": {
|
||||
"version": "2.0.7",
|
||||
"resolved": "https://registry.npmjs.org/fast-safe-stringify/-/fast-safe-stringify-2.0.7.tgz",
|
||||
"integrity": "sha512-Utm6CdzT+6xsDk2m8S6uL8VHxNwI6Jub+e9NYTcAms28T84pTa25GJQV9j0CY0N1rM8hK4x6grpF2BQf+2qwVA=="
|
||||
},
|
||||
"fill-range": {
|
||||
"version": "7.0.1",
|
||||
"resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz",
|
||||
|
@ -800,6 +815,11 @@
|
|||
"mime-types": "^2.1.12"
|
||||
}
|
||||
},
|
||||
"formidable": {
|
||||
"version": "1.2.2",
|
||||
"resolved": "https://registry.npmjs.org/formidable/-/formidable-1.2.2.tgz",
|
||||
"integrity": "sha512-V8gLm+41I/8kguQ4/o1D3RIHRmhYFG4pnNyonvua+40rqcEmT4+V71yaZ3B457xbbgCsCfjSPi65u/W6vK1U5Q=="
|
||||
},
|
||||
"fs.realpath": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz",
|
||||
|
@ -1163,6 +1183,16 @@
|
|||
"safe-buffer": "^5.1.2"
|
||||
}
|
||||
},
|
||||
"methods": {
|
||||
"version": "1.1.2",
|
||||
"resolved": "https://registry.npmjs.org/methods/-/methods-1.1.2.tgz",
|
||||
"integrity": "sha1-VSmk1nZUE07cxSZmVoNbD4Ua/O4="
|
||||
},
|
||||
"mime": {
|
||||
"version": "2.4.4",
|
||||
"resolved": "https://registry.npmjs.org/mime/-/mime-2.4.4.tgz",
|
||||
"integrity": "sha512-LRxmNwziLPT828z+4YkNzloCFC2YM4wrB99k+AV5ZbEyfGNWfG8SO1FUXLmLDBSo89NrJZ4DIWeLjy1CHGhMGA=="
|
||||
},
|
||||
"mime-db": {
|
||||
"version": "1.43.0",
|
||||
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.43.0.tgz",
|
||||
|
@ -1240,8 +1270,7 @@
|
|||
"ms": {
|
||||
"version": "2.1.1",
|
||||
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.1.tgz",
|
||||
"integrity": "sha512-tgp+dl5cGk28utYktBsrFqA7HKgrhgPsg6Z/EfhWI4gl1Hwq8B/GmY/0oXZ6nF8hDVesS/FpnYaD/kOWhYQvyg==",
|
||||
"dev": true
|
||||
"integrity": "sha512-tgp+dl5cGk28utYktBsrFqA7HKgrhgPsg6Z/EfhWI4gl1Hwq8B/GmY/0oXZ6nF8hDVesS/FpnYaD/kOWhYQvyg=="
|
||||
},
|
||||
"ms-rest": {
|
||||
"version": "2.5.3",
|
||||
|
@ -1699,6 +1728,64 @@
|
|||
"integrity": "sha1-PFMZQukIwml8DsNEhYwobHygpgo=",
|
||||
"dev": true
|
||||
},
|
||||
"superagent": {
|
||||
"version": "5.2.2",
|
||||
"resolved": "https://registry.npmjs.org/superagent/-/superagent-5.2.2.tgz",
|
||||
"integrity": "sha512-pMWBUnIllK4ZTw7p/UaobiQPwAO5w/1NRRTDpV0FTVNmECztsxKspj3ZWEordVEaqpZtmOQJJna4yTLyC/q7PQ==",
|
||||
"requires": {
|
||||
"component-emitter": "^1.3.0",
|
||||
"cookiejar": "^2.1.2",
|
||||
"debug": "^4.1.1",
|
||||
"fast-safe-stringify": "^2.0.7",
|
||||
"form-data": "^3.0.0",
|
||||
"formidable": "^1.2.1",
|
||||
"methods": "^1.1.2",
|
||||
"mime": "^2.4.4",
|
||||
"qs": "^6.9.1",
|
||||
"readable-stream": "^3.4.0",
|
||||
"semver": "^6.3.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"debug": {
|
||||
"version": "4.1.1",
|
||||
"resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz",
|
||||
"integrity": "sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==",
|
||||
"requires": {
|
||||
"ms": "^2.1.1"
|
||||
}
|
||||
},
|
||||
"form-data": {
|
||||
"version": "3.0.0",
|
||||
"resolved": "https://registry.npmjs.org/form-data/-/form-data-3.0.0.tgz",
|
||||
"integrity": "sha512-CKMFDglpbMi6PyN+brwB9Q/GOw0eAnsrEZDgcsH5Krhz5Od/haKHAX0NmQfha2zPPz0JpWzA7GJHGSnvCRLWsg==",
|
||||
"requires": {
|
||||
"asynckit": "^0.4.0",
|
||||
"combined-stream": "^1.0.8",
|
||||
"mime-types": "^2.1.12"
|
||||
}
|
||||
},
|
||||
"qs": {
|
||||
"version": "6.9.1",
|
||||
"resolved": "https://registry.npmjs.org/qs/-/qs-6.9.1.tgz",
|
||||
"integrity": "sha512-Cxm7/SS/y/Z3MHWSxXb8lIFqgqBowP5JMlTUFyJN88y0SGQhVmZnqFK/PeuMX9LzUyWsqqhNxIyg0jlzq946yA=="
|
||||
},
|
||||
"readable-stream": {
|
||||
"version": "3.6.0",
|
||||
"resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.0.tgz",
|
||||
"integrity": "sha512-BViHy7LKeTz4oNnkcLJ+lVSL6vpiFeX6/d3oSH8zCW7UxP2onchk+vTGB143xuFjHS3deTgkKoXXymXqymiIdA==",
|
||||
"requires": {
|
||||
"inherits": "^2.0.3",
|
||||
"string_decoder": "^1.1.1",
|
||||
"util-deprecate": "^1.0.1"
|
||||
}
|
||||
},
|
||||
"semver": {
|
||||
"version": "6.3.0",
|
||||
"resolved": "https://registry.npmjs.org/semver/-/semver-6.3.0.tgz",
|
||||
"integrity": "sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw=="
|
||||
}
|
||||
}
|
||||
},
|
||||
"supports-color": {
|
||||
"version": "6.0.0",
|
||||
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-6.0.0.tgz",
|
||||
|
|
|
@ -18,7 +18,8 @@
|
|||
"ms-rest-azure": "^2.5.9",
|
||||
"node-fetch": "^2.3.0",
|
||||
"stream-to-buf": "^1.0.0",
|
||||
"csvtojson": "^2.0.10"
|
||||
"csvtojson": "^2.0.10",
|
||||
"superagent": "^5.2.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@azure/functions": "^1.2.0",
|
||||
|
|
Загрузка…
Ссылка в новой задаче