Comparing version 0.0.1 to 0.1.0
69
index.js
@@ -5,25 +5,40 @@ "use strict"; | ||
const FormData = require("form-data"); | ||
// const fs = require("fs"); | ||
const {promisify} = require("util"); | ||
const { promisify } = require("util"); | ||
// const qs = require('qs'); | ||
const BASE_URL = "https://api.everypixel.com/v1"; | ||
const ENDPOINTS = { | ||
"keywords": "/keywords", | ||
"quality": "/quality", | ||
"quality_ugc": "/quality_ugc", | ||
"faces": "/faces", | ||
keywords: "/keywords", | ||
quality: "/quality", | ||
quality_ugc: "/quality_ugc", | ||
faces: "/faces", | ||
}; | ||
function EveryPixel (auth = {"username": "", "password": ""}) { | ||
function EveryPixel(auth = { username: "", password: "" }) { | ||
this.client = axios.create({ | ||
baseURL: BASE_URL, | ||
auth: auth || {}, | ||
}); | ||
// console.log(auth); | ||
return new Proxy(this, { | ||
get: function(target, key, receiver) { | ||
console.log(`getting ${key}!`); | ||
const client = Reflect.get(target, "client", receiver); | ||
this.client = axios.create({ | ||
"baseURL": BASE_URL, | ||
"auth": auth || {}, | ||
return async function(params) { | ||
if (params.url) { | ||
return await client.get(ENDPOINTS[key], { | ||
params: params, | ||
}); | ||
} else if (params.data) { | ||
const form = new FormData(); | ||
form.append("data", params.data); | ||
delete params.data; | ||
const headers = await getFormHeaders(form); | ||
return await client.post(ENDPOINTS[key], form, {headers: headers, params: params}); | ||
} | ||
}; | ||
}, | ||
}); | ||
@@ -33,30 +48,12 @@ } | ||
EveryPixel.prototype.keywords = async function(params) { | ||
if (params.url) { | ||
return (await this.client.get(ENDPOINTS.keywords, {"params": params})).data; | ||
} else if (params.data) { | ||
const form = new FormData(); | ||
form.append("data", params.data); | ||
const headers = await getFormHeaders(form); | ||
return await this.client.post(ENDPOINTS.keywords, form, {headers: headers}); | ||
} | ||
}; | ||
// Borrow from https://github.com/chux0519/axios-request for getting Content-Length | ||
async function getFormHeaders (form) { | ||
async function getFormHeaders(form) { | ||
const getLen = promisify(form.getLength).bind(form); | ||
const len = await getLen(); | ||
return { | ||
...form.getHeaders(), | ||
"Content-Length": len | ||
...form.getHeaders(), | ||
"Content-Length": len, | ||
}; | ||
} | ||
module.exports = EveryPixel; |
{ | ||
"name": "everypixel", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "JavaScript support for EveryPixel API", | ||
@@ -29,4 +29,3 @@ "main": "index.js", | ||
"axios": "^0.19.0", | ||
"form-data": "^2.4.0", | ||
"qs": "^6.7.0" | ||
"form-data": "^2.4.0" | ||
}, | ||
@@ -33,0 +32,0 @@ "devDependencies": { |
@@ -8,25 +8,59 @@ "use strict"; | ||
var api = new EveryPixel({ | ||
"username": "dR6hJL9SiROP5ptdqcg4sTFx", | ||
"password": "vVhatP2WMJoWQ52oHdWsSBnVDRNsZsx3LY0KwcMiIdwtCynY" | ||
"username": "<your-client-id>", | ||
"password": "<your-client-secret>" | ||
}); | ||
// test("set default handler to placeholder", async function (t) { | ||
// var ret = await api.keywords({"url": "http://image.everypixel.com/2014.12/67439828186edc79b9be81a4dedea8b03c09a12825b_b.jpg", "num_keywords": 10}); | ||
// console.log(ret); | ||
// t.end(); | ||
// }); | ||
test("Get 10 suggested keywords of online image", async function (t) { | ||
var ret = await api.keywords({"url": "http://image.everypixel.com/2014.12/67439828186edc79b9be81a4dedea8b03c09a12825b_b.jpg", "num_keywords": 10}); | ||
console.log(ret.data); | ||
t.end(); | ||
}); | ||
// var data = {"data": fs.createReadStream(file.path)}; | ||
test("set default handler to placeholder", async function (t) { | ||
test("Get 10 suggested keywords of local image file", async function (t) { | ||
var ret = await api.keywords({"data": fs.createReadStream(appRoot + "/t/cat.jpg"), "num_keywords": 10}); | ||
console.log(ret); | ||
console.log(ret.data); | ||
t.end(); | ||
}); | ||
// test("set default handler to placeholder", async function (t) { | ||
// var ret = await api.keywords({"file": appRoot + "/t/cat.jpg", "num_keywords": 10}); | ||
// console.log(ret); | ||
// t.end(); | ||
// }); | ||
test("Get quality score of online image", async function (t) { | ||
var ret = await api.quality({"url": "http://image.everypixel.com/2014.12/67439828186edc79b9be81a4dedea8b03c09a12825b_b.jpg"}); | ||
console.log(ret.data); | ||
t.end(); | ||
}); | ||
test("Get quality score of local image file", async function (t) { | ||
var ret = await api.quality({"data": fs.createReadStream(appRoot + "/t/cat.jpg")}); | ||
console.log(ret.data); | ||
t.end(); | ||
}); | ||
test("Get User-Generated Photo Scoring of online image", async function (t) { | ||
var ret = await api.quality_ugc({"url": "http://image.everypixel.com/2014.12/67439828186edc79b9be81a4dedea8b03c09a12825b_b.jpg"}); | ||
console.log(ret.data); | ||
t.end(); | ||
}); | ||
test("Get User-Generated Photo Scoring of local image file", async function (t) { | ||
var ret = await api.quality_ugc({"data": fs.createReadStream(appRoot + "/t/cat.jpg")}); | ||
console.log(ret.data); | ||
t.end(); | ||
}); | ||
test("Find faces of online image", async function (t) { | ||
var ret = await api.faces({"url": "https://labs.everypixel.com/api/static/i/estest_sample3.jpg"}); | ||
console.log(ret.data); | ||
console.log(ret.data.faces); | ||
t.end(); | ||
}); | ||
test("Find faces of local image file", async function (t) { | ||
var ret = await api.faces({"data": fs.createReadStream(appRoot + "/t/face.jpg")}); | ||
console.log(ret.data); | ||
console.log(ret.data.faces); | ||
t.end(); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
150279
2
97
- Removedqs@^6.7.0
- Removedcall-bind@1.0.7(transitive)
- Removeddefine-data-property@1.1.4(transitive)
- Removedes-define-property@1.0.0(transitive)
- Removedes-errors@1.3.0(transitive)
- Removedfunction-bind@1.1.2(transitive)
- Removedget-intrinsic@1.2.4(transitive)
- Removedgopd@1.0.1(transitive)
- Removedhas-property-descriptors@1.0.2(transitive)
- Removedhas-proto@1.0.3(transitive)
- Removedhas-symbols@1.0.3(transitive)
- Removedhasown@2.0.2(transitive)
- Removedobject-inspect@1.13.2(transitive)
- Removedqs@6.13.0(transitive)
- Removedset-function-length@1.2.2(transitive)
- Removedside-channel@1.0.6(transitive)