Skip to content

Commit

Permalink
Convert httpClient calls to @esri/arcgis-rest-request with ArcGISIden…
Browse files Browse the repository at this point in the history
…tityManager for authentication
  • Loading branch information
Rick Saccoccia committed Oct 28, 2024
1 parent 022067f commit e2da608
Show file tree
Hide file tree
Showing 5 changed files with 293 additions and 99 deletions.
6 changes: 3 additions & 3 deletions plugins/arcgis/service/src/FeatureLayerProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { LayerInfo } from "./LayerInfo";
import { ObservationBinner } from "./ObservationBinner";
import { ObservationBins } from "./ObservationBins";
import { ObservationsSender } from "./ObservationsSender";

import { ArcGISIdentityManager } from "@esri/arcgis-rest-request";
/**
* Processes new, updated, and deleted observations and sends the changes to a specific arc feature layer.
*/
Expand Down Expand Up @@ -42,10 +42,10 @@ export class FeatureLayerProcessor {
* @param config Contains certain parameters that can be configured.
* @param console Used to log messages to the console.
*/
constructor(layerInfo: LayerInfo, config: ArcGISPluginConfig, console: Console) {
constructor(layerInfo: LayerInfo, config: ArcGISPluginConfig, identityManager: ArcGISIdentityManager, console: Console) {
this.layerInfo = layerInfo;
this.lastTimeStamp = 0;
this.featureQuerier = new FeatureQuerier(layerInfo, config, console);
this.featureQuerier = new FeatureQuerier(layerInfo, config, identityManager,console);
this._binner = new ObservationBinner(layerInfo, this.featureQuerier, config);
this.sender = new ObservationsSender(layerInfo, config, console);
}
Expand Down
82 changes: 50 additions & 32 deletions plugins/arcgis/service/src/FeatureQuerier.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
import { ArcGISPluginConfig } from "./ArcGISPluginConfig";
import { HttpClient } from "./HttpClient";
import { LayerInfo } from "./LayerInfo";
import { QueryObjectResult } from "./QueryObjectResult";
import { ArcGISIdentityManager, request } from "@esri/arcgis-rest-request";

/**
* Performs various queries on observations for a specific arc feature layer.
*/
export class FeatureQuerier {

/**
* Used to query the arc server to figure out if an observation exists.
*/
private _httpClient: HttpClient;

/**
* The query url to find out if an observations exists on the server.
*/
private _url: string;
private _url: URL;

/**
* Used to log to console.
Expand All @@ -28,15 +23,23 @@ export class FeatureQuerier {
*/
private _config: ArcGISPluginConfig;

/**
* An instance of `ArcGISIdentityManager` used to manage authentication and identity for ArcGIS services.
* This private member handles the authentication process, ensuring that requests to ArcGIS services
* are properly authenticated using the credentials provided.
*/
private _identityManager: ArcGISIdentityManager;

/**
* Constructor.
* @param layerInfo The layer info.
* @param config The plugins configuration.
* @param console Used to log to the console.
*/
constructor(layerInfo: LayerInfo, config: ArcGISPluginConfig, console: Console) {
this._httpClient = new HttpClient(console, layerInfo.token);
this._url = layerInfo.url + '/query?where=';
constructor(layerInfo: LayerInfo, config: ArcGISPluginConfig, identityManager: ArcGISIdentityManager, console: Console) {
this._identityManager = identityManager;
this._url = new URL(layerInfo.url);
this._url.pathname += '/query';
this._console = console;
this._config = config;
}
Expand All @@ -48,19 +51,23 @@ export class FeatureQuerier {
* @param fields fields to query, all fields if not provided
* @param geometry query the geometry, default is true
*/
queryObservation(observationId: string, response: (result: QueryObjectResult) => void, fields?: string[], geometry?: boolean) {
let queryUrl = this._url + this._config.observationIdField
async queryObservation(observationId: string, response: (result: QueryObjectResult) => void, fields?: string[], geometry?: boolean) {
const queryUrl = new URL(this._url)
if (this._config.eventIdField == null) {
queryUrl += ' LIKE \'' + observationId + this._config.idSeparator + '%\''
queryUrl.searchParams.set('where', `${this._config.observationIdField} LIKE '${observationId}${this._config.idSeparator}%'`);
} else {
queryUrl += '=\'' + observationId + '\''
queryUrl.searchParams.set('where', `${this._config.observationIdField} = ${observationId}`);
}
queryUrl += this.outFields(fields) + this.returnGeometry(geometry)
this._httpClient.sendGetHandleResponse(queryUrl, (chunk) => {
this._console.info('ArcGIS response for ' + queryUrl + ' ' + chunk)
const result = JSON.parse(chunk) as QueryObjectResult
response(result)
queryUrl.searchParams.set('outFields', this.outFields(fields))
queryUrl.searchParams.set('returnGeometry', this.returnGeometry(geometry))

const queryResponse = await request(queryUrl.toString(), {
authentication: this._identityManager
});

this._console.info('ArcGIS response for ' + queryUrl + ' ' + queryResponse)
const result = JSON.parse(queryResponse) as QueryObjectResult
response(result);
}

/**
Expand All @@ -69,27 +76,38 @@ export class FeatureQuerier {
* @param fields fields to query, all fields if not provided
* @param geometry query the geometry, default is true
*/
queryObservations(response: (result: QueryObjectResult) => void, fields?: string[], geometry?: boolean) {
let queryUrl = this._url + this._config.observationIdField + ' IS NOT NULL' + this.outFields(fields) + this.returnGeometry(geometry)
this._httpClient.sendGetHandleResponse(queryUrl, (chunk) => {
this._console.info('ArcGIS response for ' + queryUrl + ' ' + chunk)
const result = JSON.parse(chunk) as QueryObjectResult
response(result)
async queryObservations(response: (result: QueryObjectResult) => void, fields?: string[], geometry?: boolean) {
const queryUrl = new URL(this._url)
queryUrl.searchParams.set('where', `${this._config.observationIdField} IS NOT NULL`);
queryUrl.searchParams.set('outFields', this.outFields(fields));
queryUrl.searchParams.set('returnGeometry', this.returnGeometry(geometry));
const queryResponse = await request(queryUrl.toString(), {
authentication: this._identityManager
});

this._console.info('ArcGIS response for ' + queryUrl + ' ' + queryResponse)
const result = JSON.parse(queryResponse) as QueryObjectResult
response(result)
}

/**
* Queries for distinct non null observation field values
* @param response Function called once query is complete.
* @param field field to query
*/
queryDistinct(response: (result: QueryObjectResult) => void, field: string) {
let queryUrl = this._url + field + ' IS NOT NULL&returnDistinctValues=true' + this.outFields([field]) + this.returnGeometry(false)
this._httpClient.sendGetHandleResponse(queryUrl, (chunk) => {
this._console.info('ArcGIS response for ' + queryUrl + ' ' + chunk)
const result = JSON.parse(chunk) as QueryObjectResult
response(result)
});
async queryDistinct(response: (result: QueryObjectResult) => void, field: string) {
const queryUrl = new URL(this._url);
queryUrl.searchParams.set('where', `${field} IS NOT NULL`);
queryUrl.searchParams.set('returnDistinctValues', 'true');
queryUrl.searchParams.set('outFields', this.outFields([field]));
queryUrl.searchParams.set('returnGeometry', this.returnGeometry(false));
const queryResponse = await request(queryUrl.toString(), {
authentication: this._identityManager

});
this._console.info('ArcGIS response for ' + queryUrl + ' ' + queryResponse)
const result = JSON.parse(queryResponse) as QueryObjectResult
response(result)
}

/**
Expand Down
Loading

0 comments on commit e2da608

Please sign in to comment.