Skip to content

Commit 77cf777

Browse files
authored
use carto sql api v3 (#148)
* updated config with carto v3 vars * updated searchRentStabilized action creator: - use new carto sql api v3 endpoint - use request headers with api key * updated rentStabilizedBblSql to use new carto table name * updated VerifyRentStabilized: - fixed check for this.match in updateMessage() * updated tenants rights query fetch to use new api & table * fixed test for rentStabilizedBblSql
1 parent f063ae8 commit 77cf777

File tree

6 files changed

+64
-24
lines changed

6 files changed

+64
-24
lines changed

app/src/action_creators/rentStabilizedActions.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import "cross-fetch/polyfill";
22

3-
import { cartoAccount } from "../constants/config";
3+
import { cartoAPIv3BaseURL, cartoApiKey } from "../constants/config";
44
import { rentStabilizedBblSql } from "../utils/sql";
55
import * as types from "../constants/actionTypes";
66

@@ -23,11 +23,21 @@ export const rentStabilizedReset = () => ({
2323
});
2424

2525
export const fetchRentStabilized = (bbl) => (dispatch) => {
26+
const headers = new Headers();
27+
headers.append("Authorization", `Bearer ${cartoApiKey}`);
28+
29+
const requestOptions = {
30+
method: "GET",
31+
headers: headers,
32+
redirect: "follow",
33+
};
34+
2635
dispatch(rentStabilizedRequest());
2736
return fetch(
28-
`https://${cartoAccount}.cartodb.com/api/v2/sql?q=${window.encodeURIComponent(
37+
`${cartoAPIv3BaseURL}/v3/sql/carto_dw/query?q=${window.encodeURIComponent(
2938
rentStabilizedBblSql(bbl)
30-
)}`
39+
)}`,
40+
requestOptions
3141
)
3242
.then((res) => {
3343
if (res.ok) {

app/src/action_creators/tenantsRightsGroupsActions.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import "cross-fetch/polyfill";
22
import * as types from "../constants/actionTypes";
3-
import { cartoAccount } from "../constants/config";
3+
import { cartoApiKey, cartoAPIv3BaseURL } from "../constants/config";
44
import { tenantsRightsGroupsSql } from "../utils/sql";
55

66
export const tenantsRightsGroupsRequest = () => ({
@@ -22,11 +22,21 @@ export const tenantsRightsGroupsReset = () => ({
2222
});
2323

2424
export const fetchTenantsRightsGroups = ({ lon, lat }) => (dispatch) => {
25+
const headers = new Headers();
26+
headers.append("Authorization", `Bearer ${cartoApiKey}`);
27+
28+
const requestOptions = {
29+
method: "GET",
30+
headers: headers,
31+
redirect: "follow",
32+
};
33+
2534
dispatch(tenantsRightsGroupsRequest());
2635
return fetch(
27-
`https://${cartoAccount}.cartodb.com/api/v2/sql?q=${window.encodeURIComponent(
36+
`${cartoAPIv3BaseURL}/v3/sql/carto_dw/query?q=${window.encodeURIComponent(
2837
tenantsRightsGroupsSql({ lon, lat })
29-
)}`
38+
)}`,
39+
requestOptions
3040
)
3141
.then((res) => {
3242
if (res.ok) {

app/src/components/verifyRentStabilized.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ export class VerifyRentStabilized extends Component {
2323
}
2424

2525
updateMessage() {
26-
if (this.match && this.match.total_rows > 0) {
26+
if (
27+
this.match &&
28+
Array.isArray(this.match.rows) &&
29+
this.match.rows.length
30+
) {
2731
this.msgYes.classList.remove("hidden");
2832
this.msgNo.classList.add("hidden");
2933
logAddressRS(this.match.rows[0].bbl);

app/src/constants/config.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,13 @@
22
export const cartoAccount = "chenrick";
33
export const rentStabilizedTable = "mappluto_likely_rs_2020_v8";
44
export const geosearchApiVersion = "v2";
5+
6+
export const cartoAPIv3BaseURL = "https://gcp-us-east1.api.carto.com";
7+
export const cartoV3RentStabilizedTableName =
8+
"shared.mappluto_likely_rs_2020_v8";
9+
export const cartoV3TenantsRightsServiceAreasTable =
10+
"shared.nyc_tenants_rights_service_areas";
11+
12+
// NOTE: this API key is restricted for use on the domain https://amirentstabilized.com
13+
export const cartoApiKey =
14+
"eyJhbGciOiJIUzI1NiJ9.eyJhIjoiYWNfd3cwaDFiczgiLCJqdGkiOiIyOTVmZGRkNiJ9.w6GC-oesQEl5HerTbTlZWxFb8-c8iX7F0NAG9pCunu0";

app/src/utils/sql.js

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
1-
import { rentStabilizedTable } from "../constants/config";
1+
import {
2+
rentStabilizedTable,
3+
cartoV3RentStabilizedTableName,
4+
cartoV3TenantsRightsServiceAreasTable,
5+
} from "../constants/config";
26

37
export const rentStabilizedBblSql = (bbl) =>
4-
`SELECT bbl FROM ${rentStabilizedTable} WHERE bbl = ${bbl}`;
8+
`SELECT bbl FROM ${cartoV3RentStabilizedTableName} WHERE bbl = ${bbl}`;
59

610
export const mapsApiSql = () =>
711
`SELECT the_geom_webmercator FROM ${rentStabilizedTable}`;
812

913
export const tenantsRightsGroupsSql = ({ lon, lat }) =>
10-
`SELECT
11-
name,
12-
full_address,
13-
email,
14-
phone,
15-
description,
16-
service_area,
17-
website_url
18-
FROM nyc_tenants_rights_service_areas
14+
`SELECT
15+
name,
16+
full_address,
17+
email,
18+
phone,
19+
description,
20+
service_area,
21+
website_url
22+
FROM ${cartoV3TenantsRightsServiceAreasTable}
1923
WHERE ST_Contains(
20-
the_geom,
21-
ST_GeomFromText(
22-
'POINT(' || ${lon} || ' ' || ${lat} || ')',
23-
4326
24+
geom,
25+
ST_GeogFromText(
26+
'POINT(' || ${lon} || ' ' || ${lat} || ')'
2427
)
2528
)`;

app/src/utils/sql.spec.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import * as sql from "./sql";
2-
import { rentStabilizedTable } from "../constants/config";
2+
import {
3+
rentStabilizedTable,
4+
cartoV3RentStabilizedTableName,
5+
} from "../constants/config";
36

47
describe("utils/sql", () => {
58
test("rentStabilizedBblSql", () => {
69
const result = sql.rentStabilizedBblSql("999999999");
710
expect(result).toBe(
8-
`SELECT bbl FROM ${rentStabilizedTable} WHERE bbl = 999999999`
11+
`SELECT bbl FROM ${cartoV3RentStabilizedTableName} WHERE bbl = 999999999`
912
);
1013
});
1114

0 commit comments

Comments
 (0)