Class QueryOData
Provides an HTTP-based client for executing OData queries and converting the responses to usable shapes.
public class QueryOData : IDisposable
- Inheritance
-
QueryOData
- Implements
- Inherited Members
- Extension Methods
Constructors
QueryOData(string)
Initializes a new instance of the QueryOData class with the specified base URL.
public QueryOData(string baseUrl)
Parameters
baseUrlstringThe base URL for the OData service. Must be a valid absolute HTTP or HTTPS URI.
Remarks
This constructor sets up an HttpClient with default credentials and a timeout of 600 seconds.
Exceptions
- ArgumentNullException
Thrown if
baseUrlis null.- ArgumentException
Thrown if
baseUrlis not a valid absolute HTTP or HTTPS URI.
QueryOData(string, HttpClient)
Initializes a new instance of the QueryOData class with the specified base URL and HTTP client.
public QueryOData(string baseUrl, HttpClient httpClient)
Parameters
baseUrlstringThe base URL for the OData service. Must be a valid absolute HTTP or HTTPS URI.
httpClientHttpClientThe HTTP client used to send requests. Cannot be null.
Exceptions
- ArgumentNullException
Thrown if
baseUrlorhttpClientis null.- ArgumentException
Thrown if
baseUrlis not a valid absolute HTTP or HTTPS URI.
Properties
BaseUrl
Gets the base URL used for oData requests.
public string BaseUrl { get; }
Property Value
CookieContainer
Gets the CookieContainer used by the internal handler when the instance manages its own client. This allows callers to propagate cookies from an incoming HTTP request.
public CookieContainer? CookieContainer { get; }
Property Value
Credentials
If provided, credentials used by the internal handler. If an external HttpClient is provided in the ctor, those credentials won't be applied to that external client.
public NetworkCredential? Credentials { get; set; }
Property Value
Methods
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
GetColumns(IQuery, JsonArray)
Retrieves the column names from the specified query and JSON array.
public static string[] GetColumns(IQuery parameter, JsonArray array)
Parameters
parameterIQueryThe query parameter containing the selection criteria. If the
Selectproperty is null or whitespace, all columns from the first JSON object in the array are returned.arrayJsonArrayThe JSON array from which to extract column names. The array must contain at least one JSON object.
Returns
- string[]
An array of strings representing the column names. If
parameterhas a non-emptySelectproperty, the specified columns are returned; otherwise, all columns from the first JSON object are returned.
GetMetadataFromBaseAsync(CancellationToken)
Retrieves the service metadata using the instance BaseUrl.
public Task<ReturnValue<Edmx>> GetMetadataFromBaseAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenToken used to cancel the retrieval operation.
Returns
- Task<ReturnValue<Edmx>>
A ReturnValue<T> containing the metadata document when successful.
GetMetadataFromJsonAsync(JsonNode, CancellationToken)
Retrieves the service metadata by extracting the metadata URL from the provided JSON payload.
public Task<ReturnValue<Edmx>> GetMetadataFromJsonAsync(JsonNode jsonResult, CancellationToken cancellationToken = default)
Parameters
jsonResultJsonNodeThe JSON payload returned by an OData service.
cancellationTokenCancellationTokenToken used to cancel the retrieval operation.
Returns
- Task<ReturnValue<Edmx>>
A ReturnValue<T> containing the metadata document when successful.
Exceptions
- ArgumentNullException
Thrown when
jsonResultis null.
GetMetadataFromUrlAsync(string, CancellationToken)
Retrieves the service metadata using the provided metadata document URL.
public Task<ReturnValue<Edmx>> GetMetadataFromUrlAsync(string metadataUrl, CancellationToken cancellationToken = default)
Parameters
metadataUrlstringAbsolute or relative URL pointing to the metadata document.
cancellationTokenCancellationTokenToken used to cancel the retrieval operation.
Returns
- Task<ReturnValue<Edmx>>
A ReturnValue<T> containing the metadata document when successful.
Exceptions
- ArgumentException
Thrown when
metadataUrlis null or whitespace.
QueryToDataReader(IQuery, int, int?, CancellationToken)
Executes the specified query and returns an IDataReader that streams rows as they are downloaded.
public Task<ReturnValue<IDataReader>> QueryToDataReader(IQuery parameter, int skip = 0, int? maxPerRequest = null, CancellationToken cancellationToken = default)
Parameters
parameterIQueryQuery definition describing the requested dataset. Cannot be null.
skipintNumber of rows to skip before starting to stream results.
maxPerRequestint?Optional maximum number of rows to fetch per HTTP request.
cancellationTokenCancellationTokenToken used to cancel the asynchronous operation.
Returns
- Task<ReturnValue<IDataReader>>
A ReturnValue<T> containing the streaming reader when successful; otherwise an error.
Remarks
The returned reader downloads additional pages in the background until the data source stops returning results or the configured limit is reached.
QueryToJSon(IQuery, int, int?, CancellationToken)
Executes a query and converts the result to JSON, transparently downloading subsequent pages when required.
public Task<ReturnValue<(JsonArray? Datas, Dictionary<string, string>? Metadatas)>> QueryToJSon(IQuery parameter, int skip = 0, int? maxPerRequest = null, CancellationToken cancellationToken = default)
Parameters
parameterIQueryThe query parameter that defines the data retrieval criteria.
skipintThe number of records to skip in the query result. Must be non-negative.
maxPerRequestint?Optional maximum number of records to request per HTTP call.
cancellationTokenCancellationTokenToken used to cancel the asynchronous processing pipeline.
Returns
- Task<ReturnValue<(JsonArray Datas, Dictionary<string, string> Metadatas)>>
A task that represents the asynchronous operation. The task result contains a tuple with a JsonArray of data and a dictionary of metadata. Returns an error if the query or conversion fails.
Remarks
This method performs one or more HTTP requests until the requested number of items is retrieved or the service no longer returns results. Returned payloads are aggregated into a single JSON array while preserving metadata from the first successful response.
SimpleQuery(IQuery, int, CancellationToken)
Executes a query using the internal HTTP client and returns the raw response.
public Task<HttpResponseMessage> SimpleQuery(IQuery parameter, int skip = 0, CancellationToken cancellationToken = default)
Parameters
parameterIQueryParameters used to build the query URL.
skipintNumber of records to skip in addition to Skip.
cancellationTokenCancellationTokenToken used to cancel the HTTP request.
Returns
- Task<HttpResponseMessage>
The HTTP response message. The caller owns the returned response and is responsible for disposing it. The method never returns null; it throws on transport failure.
SimpleQuery(IQuery, HttpRequestMessage?, int, CancellationToken)
Executes a query while copying headers and cookies from an existing HTTP request to preserve the context.
public Task<HttpResponseMessage> SimpleQuery(IQuery parameter, HttpRequestMessage? sourceRequest = null, int skip = 0, CancellationToken cancellationToken = default)
Parameters
parameterIQueryParameters used to build the query URL.
sourceRequestHttpRequestMessageOptional request providing HTTP headers to forward.
skipintNumber of records to skip in addition to Skip.
cancellationTokenCancellationTokenToken used to cancel the HTTP request.
Returns
- Task<HttpResponseMessage>
The HTTP response message. The caller owns the returned response and is responsible for disposing it. The method never returns null; it throws on transport failure.