Skip to content

Commit

Permalink
refactor: replace got with ky
Browse files Browse the repository at this point in the history
  • Loading branch information
steveoh committed Jun 12, 2024
1 parent c310e23 commit 13d4863
Showing 1 changed file with 28 additions and 31 deletions.
59 changes: 28 additions & 31 deletions src/services/geocode.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,23 @@ import url from 'node:url';
import log from 'electron-log/main';
import { parse } from 'csv-parse';
import { stringify } from 'csv-stringify';
import got from 'got';
import ky from 'ky';
// import '../../tests/mocks/server';

const __filename = url.fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const SPACES = / +/;
const INVALID_CHARS = /[^a-zA-Z0-9]/g;

const client = ky.create({
prefixUrl: 'https://api.mapserv.utah.gov/api/v1/',
headers: {
'x-agrc-geocode-client': 'electron-api-client',
'x-agrc-geocode-client-version': app.getVersion(),
Referer: 'https://api-client.ugrc.utah.gov/',
},
});

const cleanseStreet = (data) => {
const replacement = ' ';

Expand Down Expand Up @@ -47,26 +56,19 @@ export const cancelGeocode = (status = 'cancelled') => {
cancelled = status;
};

const timeout = { request: 10000 };

export const checkApiKey = async (apiKey) => {
log.info(`Checking API key: ${apiKey}`);

let response;

try {
response = await got(`geocode/326 east south temple street/slc`, {
headers: {
'x-agrc-geocode-client': 'electron-api-client',
'x-agrc-geocode-client-version': app.getVersion(),
Referer: 'https://api-client.ugrc.utah.gov/',
},
searchParams: {
apiKey: apiKey,
},
prefixUrl: 'https://api.mapserv.utah.gov/api/v1/',
timeout,
}).json();
response = await client
.get(`geocode/326 east south temple street/slc`, {
searchParams: {
apiKey: apiKey,
},
})
.json();
} catch (error) {
if (error?.response?.body) {
log.error(`Error checking api key: ${error.response.body}`);
Expand Down Expand Up @@ -150,19 +152,14 @@ export const geocode = async (event, { filePath, fields, apiKey, wkid = 26912, s
};

try {
response = await got(`geocode/${street}/${zone}`, {
headers: {
'x-agrc-geocode-client': 'electron-api-client',
'x-agrc-geocode-client-version': app.getVersion(),
Referer: 'https://api-client.ugrc.utah.gov/',
},
searchParams: {
apiKey: apiKey,
spatialReference: wkid,
},
prefixUrl: 'https://api.mapserv.utah.gov/api/v1/',
timeout,
}).json();
response = await client
.get(`geocode/${street}/${zone}`, {
searchParams: {
apiKey: apiKey,
spatialReference: wkid,
},
})
.json();

lastRequest.response = {
status: response.status,
Expand All @@ -172,14 +169,14 @@ export const geocode = async (event, { filePath, fields, apiKey, wkid = 26912, s
log.error(`Error geocoding street [${street}] zone [${zone}]: ${error}`);

try {
response = JSON.parse(error.response.body);
response = await error.response.json();
} catch {
response = { error: error.message };
}

lastRequest.request.url = error?.request?.requestUrl.toString();
lastRequest.request.url = error?.request?.url.toString();
lastRequest.response = {
status: error.response?.statusCode ?? 'response is undefined', // response is undefined when got throws
status: error.response?.status ?? 'response is undefined', // response is undefined when got throws
body: response?.error ?? response?.message,
};

Expand Down

0 comments on commit 13d4863

Please sign in to comment.