2121
2222using System . Text . Json ;
2323using Couchbase . AnalyticsClient . Internal ;
24+ using Couchbase . AnalyticsClient . Options ;
25+ using Couchbase . AnalyticsClient . Results ;
2426
2527namespace Couchbase . AnalyticsClient . Async ;
2628
2729/// <summary>
2830/// Represents a handle to a server-side asynchronous query.
29- /// Obtained from <see cref="Cluster.StartQueryAsync"/> or <see cref="Cluster.QueryHandleFromSerialized"/> .
31+ /// Obtained from <see cref="Cluster.StartQueryAsync"/>.
3032/// </summary>
3133public class QueryHandle
3234{
3335 private readonly IAnalyticsService _analyticsService ;
34- private readonly TimeSpan ? _requestTimeout ;
3536
3637 /// <summary>
37- /// The query handle string used to poll status and fetch results.
38- /// This is the path segment after <c>/api/v1/request/status/</c>.
38+ /// The query handle string used to poll for the result handle.
3939 /// </summary>
4040 public string Handle { get ; }
4141
@@ -44,75 +44,53 @@ public class QueryHandle
4444 /// </summary>
4545 public string RequestId { get ; }
4646
47- internal QueryHandle ( string handle , string requestId , IAnalyticsService analyticsService , TimeSpan ? requestTimeout = null )
47+ internal QueryHandle ( string handle , string requestId , IAnalyticsService analyticsService )
4848 {
4949 Handle = handle ?? throw new ArgumentNullException ( nameof ( handle ) ) ;
5050 RequestId = requestId ?? throw new ArgumentNullException ( nameof ( requestId ) ) ;
5151 _analyticsService = analyticsService ?? throw new ArgumentNullException ( nameof ( analyticsService ) ) ;
52- _requestTimeout = requestTimeout ;
5352 }
5453
5554 /// <summary>
56- /// Fetches the current status of the asynchronous query from the server.
55+ /// Fetches the result handle of the asynchronous query from the server.
5756 /// </summary>
57+ /// <param name="options">Options for fetching the result handle.</param>
5858 /// <param name="cancellationToken">A cancellation token.</param>
59- /// <returns>A <see cref="QueryStatus "/> representing the current state of the query .</returns>
60- public async Task < QueryStatus > FetchStatusAsync ( CancellationToken cancellationToken = default )
59+ /// <returns>A <see cref="QueryResultHandle "/> if results are ready, otherwise null .</returns>
60+ public Task < QueryResultHandle ? > FetchResultHandleAsync ( FetchResultHandleOptions ? options = null , CancellationToken cancellationToken = default )
6161 {
62- return await _analyticsService . FetchStatusAsync ( Handle , _requestTimeout , cancellationToken )
63- . ConfigureAwait ( false ) ;
62+ options ??= new FetchResultHandleOptions ( ) ;
63+ return _analyticsService . FetchResultHandleAsync ( this , options , cancellationToken ) ;
6464 }
6565
6666 /// <summary>
67- /// Discards the query results on the server. After this call, the results can no longer be fetched .
67+ /// Fetches the result handle of the asynchronous query from the server .
6868 /// </summary>
69- /// <param name="cancellationToken">A cancellation token.</param>
70- public async Task DiscardResultsAsync ( CancellationToken cancellationToken = default )
69+ public Task < QueryResultHandle ? > FetchResultHandleAsync ( Func < FetchResultHandleOptions , FetchResultHandleOptions > options , CancellationToken cancellationToken = default )
7170 {
72- await _analyticsService . DiscardResultsAsync ( Handle , _requestTimeout , cancellationToken )
73- . ConfigureAwait ( false ) ;
71+ var fetchOptions = new FetchResultHandleOptions ( ) ;
72+ fetchOptions = options . Invoke ( fetchOptions ) ;
73+ return FetchResultHandleAsync ( fetchOptions , cancellationToken ) ;
7474 }
7575
7676 /// <summary>
7777 /// Cancels the query on the server. If the query has already completed, this is a no-op.
7878 /// </summary>
79+ /// <param name="options">Options for cancellation.</param>
7980 /// <param name="cancellationToken">A cancellation token.</param>
80- public async Task CancelAsync ( CancellationToken cancellationToken = default )
81- {
82- await _analyticsService . CancelQueryAsync ( RequestId , _requestTimeout , cancellationToken )
83- . ConfigureAwait ( false ) ;
84- }
85-
86- /// <summary>
87- /// Serializes this <see cref="QueryHandle"/> to a JSON string so it can be persisted and
88- /// later reconstructed via <see cref="Cluster.QueryHandleFromSerialized"/>.
89- /// This method does not perform any network operations.
90- /// </summary>
91- /// <returns>A JSON string containing the handle and request ID.</returns>
92- public string Serialize ( )
81+ public Task CancelAsync ( CancelOptions ? options = null , CancellationToken cancellationToken = default )
9382 {
94- var data = new SerializedQueryHandle ( Handle , RequestId ) ;
95- return JsonSerializer . Serialize ( data ) ;
83+ options ?? = new CancelOptions ( ) ;
84+ return _analyticsService . CancelQueryAsync ( RequestId , options , cancellationToken ) ;
9685 }
9786
9887 /// <summary>
99- /// Deserializes a <see cref="QueryHandle"/> from a JSON string previously produced by <see cref="Serialize"/>.
100- /// This method does not perform any network operations.
88+ /// Cancels the query on the server. If the query has already completed, this is a no-op.
10189 /// </summary>
102- internal static QueryHandle Deserialize ( string serializedHandle , IAnalyticsService analyticsService , TimeSpan ? requestTimeout = null )
90+ public Task CancelAsync ( Func < CancelOptions , CancelOptions > options , CancellationToken cancellationToken = default )
10391 {
104- ArgumentNullException . ThrowIfNull ( serializedHandle ) ;
105-
106- var data = JsonSerializer . Deserialize < SerializedQueryHandle > ( serializedHandle )
107- ?? throw new ArgumentException ( "Invalid serialized handle format." , nameof ( serializedHandle ) ) ;
108-
109- if ( string . IsNullOrWhiteSpace ( data . Handle ) || string . IsNullOrWhiteSpace ( data . RequestId ) )
110- {
111- throw new ArgumentException ( "Serialized handle is missing required fields." , nameof ( serializedHandle ) ) ;
112- }
113-
114- return new QueryHandle ( data . Handle , data . RequestId , analyticsService , requestTimeout ) ;
92+ var cancelOptions = new CancelOptions ( ) ;
93+ cancelOptions = options . Invoke ( cancelOptions ) ;
94+ return CancelAsync ( cancelOptions , cancellationToken ) ;
11595 }
116-
117- private record SerializedQueryHandle ( string Handle , string RequestId ) ;
11896}
0 commit comments