11import { ArcGISPluginConfig } from "./ArcGISPluginConfig" ;
2- import { HttpClient } from "./HttpClient" ;
32import { LayerInfo } from "./LayerInfo" ;
43import { QueryObjectResult } from "./QueryObjectResult" ;
4+ import { ArcGISIdentityManager , request } from "@esri/arcgis-rest-request" ;
55
66/**
77 * Performs various queries on observations for a specific arc feature layer.
88 */
99export class FeatureQuerier {
1010
11- /**
12- * Used to query the arc server to figure out if an observation exists.
13- */
14- private _httpClient : HttpClient ;
15-
1611 /**
1712 * The query url to find out if an observations exists on the server.
1813 */
19- private _url : string ;
14+ private _url : URL ;
2015
2116 /**
2217 * Used to log to console.
@@ -28,15 +23,23 @@ export class FeatureQuerier {
2823 */
2924 private _config : ArcGISPluginConfig ;
3025
26+ /**
27+ * An instance of `ArcGISIdentityManager` used to manage authentication and identity for ArcGIS services.
28+ * This private member handles the authentication process, ensuring that requests to ArcGIS services
29+ * are properly authenticated using the credentials provided.
30+ */
31+ private _identityManager : ArcGISIdentityManager ;
32+
3133 /**
3234 * Constructor.
3335 * @param layerInfo The layer info.
3436 * @param config The plugins configuration.
3537 * @param console Used to log to the console.
3638 */
37- constructor ( layerInfo : LayerInfo , config : ArcGISPluginConfig , console : Console ) {
38- this . _httpClient = new HttpClient ( console , layerInfo . token ) ;
39- this . _url = layerInfo . url + '/query?where=' ;
39+ constructor ( layerInfo : LayerInfo , config : ArcGISPluginConfig , identityManager : ArcGISIdentityManager , console : Console ) {
40+ this . _identityManager = identityManager ;
41+ this . _url = new URL ( layerInfo . url ) ;
42+ this . _url . pathname += '/query' ;
4043 this . _console = console ;
4144 this . _config = config ;
4245 }
@@ -48,19 +51,23 @@ export class FeatureQuerier {
4851 * @param fields fields to query, all fields if not provided
4952 * @param geometry query the geometry, default is true
5053 */
51- queryObservation ( observationId : string , response : ( result : QueryObjectResult ) => void , fields ?: string [ ] , geometry ?: boolean ) {
52- let queryUrl = this . _url + this . _config . observationIdField
54+ async queryObservation ( observationId : string , response : ( result : QueryObjectResult ) => void , fields ?: string [ ] , geometry ?: boolean ) {
55+ const queryUrl = new URL ( this . _url )
5356 if ( this . _config . eventIdField == null ) {
54- queryUrl += ' LIKE \'' + observationId + this . _config . idSeparator + '%\''
57+ queryUrl . searchParams . set ( 'where' , ` ${ this . _config . observationIdField } LIKE ' ${ observationId } ${ this . _config . idSeparator } %'` ) ;
5558 } else {
56- queryUrl += '=\'' + observationId + '\''
59+ queryUrl . searchParams . set ( 'where' , ` ${ this . _config . observationIdField } = ${ observationId } ` ) ;
5760 }
58- queryUrl += this . outFields ( fields ) + this . returnGeometry ( geometry )
59- this . _httpClient . sendGetHandleResponse ( queryUrl , ( chunk ) => {
60- this . _console . info ( 'ArcGIS response for ' + queryUrl + ' ' + chunk )
61- const result = JSON . parse ( chunk ) as QueryObjectResult
62- response ( result )
61+ queryUrl . searchParams . set ( 'outFields' , this . outFields ( fields ) )
62+ queryUrl . searchParams . set ( 'returnGeometry' , this . returnGeometry ( geometry ) )
63+
64+ const queryResponse = await request ( queryUrl . toString ( ) , {
65+ authentication : this . _identityManager
6366 } ) ;
67+
68+ this . _console . info ( 'ArcGIS response for ' + queryUrl + ' ' + queryResponse )
69+ const result = JSON . parse ( queryResponse ) as QueryObjectResult
70+ response ( result ) ;
6471 }
6572
6673 /**
@@ -69,27 +76,38 @@ export class FeatureQuerier {
6976 * @param fields fields to query, all fields if not provided
7077 * @param geometry query the geometry, default is true
7178 */
72- queryObservations ( response : ( result : QueryObjectResult ) => void , fields ?: string [ ] , geometry ?: boolean ) {
73- let queryUrl = this . _url + this . _config . observationIdField + ' IS NOT NULL' + this . outFields ( fields ) + this . returnGeometry ( geometry )
74- this . _httpClient . sendGetHandleResponse ( queryUrl , ( chunk ) => {
75- this . _console . info ( 'ArcGIS response for ' + queryUrl + ' ' + chunk )
76- const result = JSON . parse ( chunk ) as QueryObjectResult
77- response ( result )
79+ async queryObservations ( response : ( result : QueryObjectResult ) => void , fields ?: string [ ] , geometry ?: boolean ) {
80+ const queryUrl = new URL ( this . _url )
81+ queryUrl . searchParams . set ( 'where' , `${ this . _config . observationIdField } IS NOT NULL` ) ;
82+ queryUrl . searchParams . set ( 'outFields' , this . outFields ( fields ) ) ;
83+ queryUrl . searchParams . set ( 'returnGeometry' , this . returnGeometry ( geometry ) ) ;
84+ const queryResponse = await request ( queryUrl . toString ( ) , {
85+ authentication : this . _identityManager
7886 } ) ;
87+
88+ this . _console . info ( 'ArcGIS response for ' + queryUrl + ' ' + queryResponse )
89+ const result = JSON . parse ( queryResponse ) as QueryObjectResult
90+ response ( result )
7991 }
8092
8193 /**
8294 * Queries for distinct non null observation field values
8395 * @param response Function called once query is complete.
8496 * @param field field to query
8597 */
86- queryDistinct ( response : ( result : QueryObjectResult ) => void , field : string ) {
87- let queryUrl = this . _url + field + ' IS NOT NULL&returnDistinctValues=true' + this . outFields ( [ field ] ) + this . returnGeometry ( false )
88- this . _httpClient . sendGetHandleResponse ( queryUrl , ( chunk ) => {
89- this . _console . info ( 'ArcGIS response for ' + queryUrl + ' ' + chunk )
90- const result = JSON . parse ( chunk ) as QueryObjectResult
91- response ( result )
92- } ) ;
98+ async queryDistinct ( response : ( result : QueryObjectResult ) => void , field : string ) {
99+ const queryUrl = new URL ( this . _url ) ;
100+ queryUrl . searchParams . set ( 'where' , `${ field } IS NOT NULL` ) ;
101+ queryUrl . searchParams . set ( 'returnDistinctValues' , 'true' ) ;
102+ queryUrl . searchParams . set ( 'outFields' , this . outFields ( [ field ] ) ) ;
103+ queryUrl . searchParams . set ( 'returnGeometry' , this . returnGeometry ( false ) ) ;
104+ const queryResponse = await request ( queryUrl . toString ( ) , {
105+ authentication : this . _identityManager
106+
107+ } ) ;
108+ this . _console . info ( 'ArcGIS response for ' + queryUrl + ' ' + queryResponse )
109+ const result = JSON . parse ( queryResponse ) as QueryObjectResult
110+ response ( result )
93111 }
94112
95113 /**
0 commit comments