C# Client Library
A C# Client Library for the AnalyzeRe REST API
Loading...
Searching...
No Matches
Utilities.cs
Go to the documentation of this file.
1using System;
2using System.IO;
3using System.Text;
4
6
8{
10 public static class Utilities
11 {
13 internal const int DEFAULT_BUFFER_SIZE = 4096; // 4K bytes
14
18 public static bool EnableDebugLogging
19 {
20 get => _enableDebugLogging;
21 set
22 {
23 _enableDebugLogging = value;
24#if !DEBUG
25 throw new InvalidOperationException("Currently, this feature cannot be used " +
26 "as it makes use of Debug.WriteLine, which is compiled out in Release mode. " +
27 "To make use of this you will need to reference the Debug version of " +
28 "this library, which should be available on MyGet at: " +
29 "https://www.myget.org/feed/analyzere/package/nuget/AnalyzeRe.Client");
30#endif
31 }
32 }
33 private static bool _enableDebugLogging;
34
35#region Public Methods
40 public static MemoryStream GetStringStream(string input)
41 {
42 return new MemoryStream(Encoding.UTF8.GetBytes(input));
43 }
44
52 Parameters parameters = null)
53 {
55 parameters == null ? new AsyncParameters() : new AsyncParameters(parameters));
56 }
57
66 AsyncParameters parameters)
67 {
69 API.ValidateExistingResource(asResource);
71 try
72 {
74 return API.PollUntil(
75 () => dataEndpoint.status.Get(timeout: polling_timeout -
76 (int)(DateTime.UtcNow - startTime).TotalMilliseconds),
77 status =>
78 {
79 parameters.OnProgressUpdate?.Invoke(status);
80 return !status.status.IsBusy();
81 }, parameters.commit_polling_options);
82 }
83 // Catch NotWaitingException and re-raise as a timeout exception.
85 {
86 throw new RequestTimeoutException("The server took too long to commit the data, " +
87 $"the timeout was exceeded. ({polling_timeout / (1000d * 60d)} minutes)", ex,
88 ex.RestResponse);
89 }
90 }
91#endregion Public Methods
92
93#region Internal Helper Methods
95 internal static void CheckForCancellation(AsyncParameters parameters)
96 {
97 if (parameters.commit_polling_options?.CancellationToken?.IsCancellationRequested ?? false)
99 "The upload process has been cancelled by the user.");
100 }
101
103 internal static FileStream OpenReadOnlyFileStream(string filePath)
104 {
105 return new FileStream(filePath, FileMode.Open, FileAccess.Read,
106 FileShare.ReadWrite, DEFAULT_BUFFER_SIZE);
107 }
108#endregion Internal Helper Methods
109 }
110}
Describes an endpoint off of some types of resources from which an associated "large data file" can b...
Describes a collection of resources which can be listed.
API methods / requests made available to the user.
static void PollUntil(Func< bool > request, AnalyzeRe.PollingOptions pollingOptions=null)
Poll the specified request until it returns true.
Parameters to be used in a Large Upload operation.
Action< StatusResponse > OnProgressUpdate
Action to invoke every time a chunk is uploaded. Callback is supplied with a double indicating progre...
Parameters to be used in a Large Data Upload operation.
Definition Parameters.cs:7
PollingOptions commit_polling_options
Determines how the system polls the status of the data file while waiting for the data to be processe...
Definition Parameters.cs:47
Indicates the status of a data endpoint.
Large Data Upload Utilities.
Definition Utilities.cs:11
static StatusResponse PollUntilDataProcessed(LargeDataSubResource dataEndpoint, AsyncParameters parameters)
Polls the dataEndpoint until the state is no longer Processing.
Definition Utilities.cs:64
static StatusResponse PollUntilDataProcessed(LargeDataSubResource dataEndpoint, Parameters parameters=null)
Polls the dataEndpoint until the state is no longer Processing.
Definition Utilities.cs:50
static bool EnableDebugLogging
When in debug mode, allows detailed debug logging of the data upload process.
Definition Utilities.cs:19
static MemoryStream GetStringStream(string input)
Generate a byte stream from a string which can be used in the LargeStreamUpload methods.
Definition Utilities.cs:40
Thrown when a request requires additional time to complete, but it exceeds the time we are willing to...
CancellationToken? CancellationToken
A cancellation token (if available) supplied to the polling method to allow the polling task to be ca...
int MaxPollTotalTime
The maximum time (in milliseconds) to poll the request before raising a NotWaitingException....
Extends the TimeoutException class to contain the IRestResponse that timed out.
Interface for Base class used by all resources.