-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
139 lines (114 loc) · 3.03 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const { ZILLOW_LISTING_API, ZILLOW_PROPERTY_API } = require('./consts');
const { doRequest } = require('./utils/http');
class ZillowAPI {
/**
*
* @param {apiKey} apiKey - Scrape-It.Cloud API Key
* @throws {Error}
*
*/
constructor(apiKey = null) {
if (!apiKey) {
throw new Error('API Key is not provided');
}
this.apiKey = apiKey;
}
handleErrors(result, statusCode) {
if (result.status === 'error' && result.message) {
throw new Error(result.message)
}
if (statusCode === 403) {
throw new Error('You don\'t have enough API credits to perform this request');
}
if (statusCode === 401) {
throw new Error('Invalid API Key');
}
if (statusCode === 429) {
throw new Error('You reached concurrency limit');
}
if (result.errors && result.errors.length) {
const error = new Error(`Validation error`)
error.validationErrors = result.errors
throw error
}
if (!result.requestMetadata) {
throw new Error('Invalid response');
}
if (result.requestMetadata.status === 'error') {
throw new Error('Invalid response');
}
}
/**
*
* @param {params} params - Scrape-It.Cloud Zillow API Params
* @throws {Error}
* @returns {object} Scrape-It.Cloud Zillow API Response
*
*/
async search(params) {
const searchParams = new URLSearchParams()
Object.entries(params).forEach(([key, value]) => {
if (Array.isArray(value)) {
value.forEach(value => searchParams.append(key, value.toString()))
} else if (typeof value === 'object') {
Object.keys(value).forEach(objKey => {
searchParams.append(`${key}[${objKey}]`, value[objKey])
})
} else {
searchParams.append(key, value.toString())
}
});
const { responseBody, statusCode } = await doRequest(
`${ZILLOW_LISTING_API}?${searchParams.toString()}`,
{
'x-api-key': this.apiKey,
},
{
...params,
source: 'nodejs_sdk'
}
);
let result = {};
try {
result = JSON.parse(responseBody);
} catch (e) {
result = responseBody;
}
this.handleErrors(result, statusCode)
if (result.requestMetadata.status === 'ok') {
return result;
}
return result;
}
/**
*
* @param {params} params - Scrape-It.Cloud Zillow API Params
* @throws {Error}
* @returns {object} Scrape-It.Cloud Zillow API Response
*
*/
async property(params) {
const { responseBody, statusCode } = await doRequest(
`${ZILLOW_PROPERTY_API}?${new URLSearchParams(params).toString()}`,
{
'x-api-key': this.apiKey,
},
{
...params,
source: 'nodejs_sdk'
}
);
let result = {};
try {
result = JSON.parse(responseBody);
} catch (e) {
result = responseBody;
}
this.handleErrors(result, statusCode)
if (result.requestMetadata.status === 'ok') {
return result;
}
return result;
}
}
module.exports = ZillowAPI;