From c40302aa933d2851d5bf8215deff04863b3d8a38 Mon Sep 17 00:00:00 2001 From: evgeniywas Date: Sat, 7 Oct 2023 12:35:40 +0100 Subject: [PATCH] RC-9: create property service --- src/app/services/index.js | 12 +++++- src/app/services/propertyService.js | 57 +++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 src/app/services/propertyService.js diff --git a/src/app/services/index.js b/src/app/services/index.js index 085d6b3..9cfaffc 100644 --- a/src/app/services/index.js +++ b/src/app/services/index.js @@ -1,5 +1,7 @@ import AuthService from './authService'; import FirebaseService from './firebaseService'; +import PropertyService from './propertyService'; + // TODO: change to firebase secrets or to use firebase functions const firebaseConfig = { apiKey: 'AIzaSyBqMGmOF0-DkYDpnsmZwpf5S8w5cL3fBb8', @@ -12,7 +14,15 @@ const firebaseConfig = { measurementId: 'G-JW7J8ZQ9FJ', }; +// TopHap +const propertyConfig = { + dataBaseURL: process.env.REACT_APP_PROPERTY_DATA_BASE_URL, + widgetBaseURL: process.env.REACT_APP_PROPERTY_WIDGET_BASE_URL, + apiKey: process.env.REACT_APP_PROPERTY_API_KEY, +}; + const firebase = new FirebaseService(firebaseConfig); const authService = new AuthService(); +const propertyService = new PropertyService(propertyConfig); -export { authService, firebase }; +export { authService, firebase, propertyService }; diff --git a/src/app/services/propertyService.js b/src/app/services/propertyService.js new file mode 100644 index 0000000..ad6637b --- /dev/null +++ b/src/app/services/propertyService.js @@ -0,0 +1,57 @@ +import axios from 'axios'; + +const widgets = { + absorptionRate: 'absorption-rate', + crime: 'crime', + hazards: 'hazards', + mapPreview: 'map-preview', + marketTrends: 'market-trends', + noise: 'noise', + population: 'population', + propertyTypes: 'property-types', + rentEstimate: 'rent-estimate', + thEstimate: 'th-estimate', + turnover: 'turnover', + walkability: 'walkability', + zipCodeMap: 'zip-code-map', +}; + +export default class PropertyService { + #dataApi; + + #widgetApi; + + constructor(config) { + const { dataBaseURL, widgetBaseURL, apiKey } = config; + this.#dataApi = axios.create({ + baseURL: dataBaseURL, + headers: { 'X-API-Key': apiKey }, + }); + this.#widgetApi = axios.create({ + baseURL: widgetBaseURL, + params: { + sid: apiKey, + }, + }); + } + + fetchProperty(params) { + return this.#dataApi.get('/property', { params }); + } + + search(body) { + return this.#dataApi.post('/search', body); + } + + fetchComparables(params) { + return this.#dataApi.get('/comparables', { params }); + } + + fetchWidgetByPropertyId(widget, propertyId, params) { + return this.#widgetApi.get(`${widget}/${propertyId}`, { params }); + } + + fetchWidgetByAddress(widget, params) { + return this.#widgetApi.get(`${widget}/address`, { params }); + } +}