/// <summary> /// Executes a Drive request. /// </summary> /// <typeparam name="T">The requested data type.</typeparam> /// <param name="request">The request.</param> /// <returns>A response.</returns> public DriveResponse <T> Execute <T>(IDriveRequest <T> request) where T : IDriveData, new() { Log.Debug("Execute."); DriveResponse <T> result = null; RequestErrorAction errorAction; do { try { result = request.Execute(this); break; } catch (InteractionException exception) { Log.TraceException("Execute. Exception", exception); errorAction = RaiseRequestError(exception, RequestErrorAction.Alert); Log.Trace("Execute. NextAction: \"{0}\"", errorAction); if (errorAction == RequestErrorAction.Alert) { throw; } } } while (errorAction == RequestErrorAction.Retry); return(result); }
public async Task <TResponse> Handle(IDriveRequest request, CancellationToken cancellationToken, RequestHandlerDelegate <TResponse> next) { var storageOptions = _serviceProvider.GetRequiredService <StorageOptions>(); Directory.CreateDirectory(storageOptions.StorageFolder); return(await next()); }
/// <summary> /// Gets body objects of a REST request. /// </summary> /// <typeparam name="T">The type of a retrieved data object.</typeparam> /// <param name="request">The Google Drive request.</param> /// <returns>REST request parameters.</returns> internal static IEnumerable <object> GetBodyObjects <T>(IDriveRequest <T> request) where T : IDriveData, new() { var attrType = typeof(RestBodyAttribute); var properties = request.GetType().GetProperties() .Where(p => p.IsDefined(attrType, false)) .Select(p => new { Property = p, Attr = p.GetCustomAttributes(attrType, false).OfType <RestBodyAttribute>().FirstOrDefault() }) .Where(x => x.Attr != null); return(from propertyDef in properties let value = propertyDef.Property.GetValue(request, null) where value != null select value); }
/// <summary> /// Gets parameters for a REST request from a Google Drive request. /// </summary> /// <typeparam name="T">The type of a retrieved data object.</typeparam> /// <param name="request">The Google Drive request.</param> /// <returns>REST request parameters.</returns> internal static IEnumerable <Parameter> GetParameters <T>(IDriveRequest <T> request) where T : IDriveData, new() { var attrType = typeof(RestParameterAttribute); var properties = request.GetType().GetProperties() .Where(p => p.IsDefined(attrType, false)) .Select(p => new { Property = p, Attr = p.GetCustomAttributes(attrType, false).OfType <RestParameterAttribute>().FirstOrDefault() }) .Where(x => x.Attr != null); foreach (var property in properties) { string strValue; if (property.Property.PropertyType.IsEnum) { var eVal = property.Property.GetValue(request, null) as Enum; strValue = eVal != null?eVal.GetStringValue(eVal.ToString()) : null; } else { var objVal = property.Property.GetValue(request, null); strValue = objVal != null ? (objVal is string?(string)objVal : objVal.ToString()) : null; } if (!string.IsNullOrEmpty(strValue)) { var item = new Parameter { Name = property.Attr.ParameterName, Type = property.Attr.ParameterType, Value = strValue }; yield return(item); } } }
/// <summary> /// IDriveRequest extension method that takes in a handler for the response. /// </summary> /// <param name="request">Request to send out</param> /// <param name="responseHandler">Handler for the response</param> /// <returns></returns> public static void SendGet(this IDriveRequest request, ResponseHandler responseHandler) { SendGet <Drive>(request, responseHandler); }
/// <summary> /// Creates a REST request with Drive API version parameter. /// </summary> /// <typeparam name="T">The type of a retrieved data object.</typeparam> /// <param name="resource">The request location.</param> /// <param name="method">The request method.</param> /// <param name="driveRequest">The Drive reqiest.</param> /// <param name="hasBody">The whether if drive request has body.</param> /// <returns>A REST request.</returns> internal static IRestRequest CreateRestRequest <T>(string resource, Method method, IDriveRequest <T> driveRequest, bool hasBody = false) where T : IDriveData, new() { object bodyObject; if (hasBody) { bodyObject = RestParametersHelper.GetBodyObjects(driveRequest).FirstOrDefault(); if (bodyObject == null) { throw new InvalidOperationException(LocalStrings.RequestBodyNullReferenceErrorMessage); } } else { bodyObject = null; } var result = CreateRestRequest(resource, method, RestParametersHelper.GetParameters(driveRequest)); if (hasBody) { result.AddBody(bodyObject); } return(result); }