* AVCommandRunner.cs: chore: 格式化代码

* AVUser.cs:
* AVCloud.cs:
* IAVCommandRunner.cs:
* AVFileController.cs:
oneRain 2019-08-28 16:32:13 +08:00
parent a951d8e6c5
commit 6a42f1f74a
5 changed files with 42 additions and 86 deletions

View File

@ -38,7 +38,7 @@ namespace LeanCloud.Storage.Internal
public Task<Tuple<HttpStatusCode, IDictionary<string, object>>> RunCommandAsync(AVCommand command,
IProgress<AVUploadProgressEventArgs> uploadProgress = null,
IProgress<AVDownloadProgressEventArgs> downloadProgress = null,
CancellationToken cancellationToken = default(CancellationToken))
CancellationToken cancellationToken = default)
{
return PrepareCommand(command).ContinueWith(commandTask =>
{

View File

@ -17,8 +17,8 @@ namespace LeanCloud.Storage.Internal
/// <param name="cancellationToken">The cancellation token for the request.</param>
/// <returns></returns>
Task<Tuple<HttpStatusCode, IDictionary<string, object>>> RunCommandAsync(AVCommand command,
IProgress<AVUploadProgressEventArgs> uploadProgress = null,
IProgress<AVDownloadProgressEventArgs> downloadProgress = null,
CancellationToken cancellationToken = default(CancellationToken));
IProgress<AVUploadProgressEventArgs> uploadProgress = null,
IProgress<AVDownloadProgressEventArgs> downloadProgress = null,
CancellationToken cancellationToken = default);
}
}

View File

@ -7,20 +7,17 @@ using System.Net;
using System.Collections.Generic;
using System.Linq;
namespace LeanCloud.Storage.Internal
{
namespace LeanCloud.Storage.Internal {
/// <summary>
/// AVF ile controller.
/// </summary>
public class AVFileController : IAVFileController
{
public class AVFileController : IAVFileController {
private readonly IAVCommandRunner commandRunner;
/// <summary>
/// Initializes a new instance of the <see cref="T:LeanCloud.Storage.Internal.AVFileController"/> class.
/// </summary>
/// <param name="commandRunner">Command runner.</param>
public AVFileController(IAVCommandRunner commandRunner)
{
public AVFileController(IAVCommandRunner commandRunner) {
this.commandRunner = commandRunner;
}
/// <summary>
@ -33,19 +30,16 @@ namespace LeanCloud.Storage.Internal
/// <param name="progress">Progress.</param>
/// <param name="cancellationToken">Cancellation token.</param>
public virtual Task<FileState> SaveAsync(FileState state,
Stream dataStream,
String sessionToken,
IProgress<AVUploadProgressEventArgs> progress,
CancellationToken cancellationToken = default(CancellationToken))
{
if (state.Url != null)
{
Stream dataStream,
string sessionToken,
IProgress<AVUploadProgressEventArgs> progress,
CancellationToken cancellationToken = default) {
if (state.Url != null) {
// !isDirty
return Task<FileState>.FromResult(state);
}
if (cancellationToken.IsCancellationRequested)
{
if (cancellationToken.IsCancellationRequested) {
var tcs = new TaskCompletionSource<FileState>();
tcs.TrySetCanceled();
return tcs.Task;
@ -60,30 +54,25 @@ namespace LeanCloud.Storage.Internal
return commandRunner.RunCommandAsync(command,
uploadProgress: progress,
cancellationToken: cancellationToken).OnSuccess(uploadTask =>
{
cancellationToken: cancellationToken).OnSuccess(uploadTask => {
var result = uploadTask.Result;
var jsonData = result.Item2;
cancellationToken.ThrowIfCancellationRequested();
return new FileState
{
return new FileState {
Name = jsonData["name"] as string,
Url = new Uri(jsonData["url"] as string, UriKind.Absolute),
MimeType = state.MimeType
};
}).ContinueWith(t =>
{
}).ContinueWith(t => {
// Rewind the stream on failure or cancellation (if possible)
if ((t.IsFaulted || t.IsCanceled) && dataStream.CanSeek)
{
if ((t.IsFaulted || t.IsCanceled) && dataStream.CanSeek) {
dataStream.Seek(oldPosition, SeekOrigin.Begin);
}
return t;
}).Unwrap();
}
public Task DeleteAsync(FileState state, string sessionToken, CancellationToken cancellationToken)
{
public Task DeleteAsync(FileState state, string sessionToken, CancellationToken cancellationToken) {
var command = new AVCommand("files/" + state.ObjectId,
method: "DELETE",
sessionToken: sessionToken,
@ -91,8 +80,7 @@ namespace LeanCloud.Storage.Internal
return commandRunner.RunCommandAsync(command, cancellationToken: cancellationToken);
}
internal static Task<Tuple<HttpStatusCode, IDictionary<string, object>>> GetFileToken(FileState fileState, CancellationToken cancellationToken)
{
internal static Task<Tuple<HttpStatusCode, IDictionary<string, object>>> GetFileToken(FileState fileState, CancellationToken cancellationToken) {
Task<Tuple<HttpStatusCode, IDictionary<string, object>>> rtn;
string currentSessionToken = AVUser.CurrentSessionToken;
string str = fileState.Name;
@ -107,43 +95,37 @@ namespace LeanCloud.Storage.Internal
return rtn;
}
public Task<FileState> GetAsync(string objectId, string sessionToken, CancellationToken cancellationToken)
{
public Task<FileState> GetAsync(string objectId, string sessionToken, CancellationToken cancellationToken) {
var command = new AVCommand("files/" + objectId,
method: "GET",
sessionToken: sessionToken,
data: null);
return commandRunner.RunCommandAsync(command, cancellationToken: cancellationToken).OnSuccess(_ =>
{
return commandRunner.RunCommandAsync(command, cancellationToken: cancellationToken).OnSuccess(_ => {
var result = _.Result;
var jsonData = result.Item2;
cancellationToken.ThrowIfCancellationRequested();
return new FileState
{
return new FileState {
ObjectId = jsonData["objectId"] as string,
Name = jsonData["name"] as string,
Url = new Uri(jsonData["url"] as string, UriKind.Absolute),
};
});
}
internal static string GetUniqueName(FileState fileState)
{
internal static string GetUniqueName(FileState fileState) {
string key = Random(12);
string extension = Path.GetExtension(fileState.Name);
key += extension;
fileState.CloudName = key;
return key;
}
internal static string Random(int length)
{
internal static string Random(int length) {
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
var random = new Random();
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[random.Next(s.Length)]).ToArray());
}
internal static double CalcProgress(double already, double total)
{
internal static double CalcProgress(double already, double total) {
var pv = (1.0 * already / total);
return Math.Round(pv, 3);
}

View File

@ -42,7 +42,7 @@ namespace LeanCloud {
/// <param name="sesstionToken"></param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The result of the cloud call.</returns>
public static Task<T> CallFunctionAsync<T>(String name, IDictionary<string, object> parameters = null, string sesstionToken = null, CancellationToken cancellationToken = default(CancellationToken))
public static Task<T> CallFunctionAsync<T>(string name, IDictionary<string, object> parameters = null, string sesstionToken = null, CancellationToken cancellationToken = default)
{
var sessionTokenTask = AVUser.TakeSessionToken(sesstionToken);
@ -64,7 +64,7 @@ namespace LeanCloud {
/// <param name="sesstionToken"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static Task<T> RPCFunctionAsync<T>(String name, IDictionary<string, object> parameters = null, string sesstionToken = null, CancellationToken cancellationToken = default(CancellationToken))
public static Task<T> RPCFunctionAsync<T>(string name, IDictionary<string, object> parameters = null, string sesstionToken = null, CancellationToken cancellationToken = default)
{
var sessionTokenTask = AVUser.TakeSessionToken(sesstionToken);
@ -108,20 +108,6 @@ namespace LeanCloud {
});
}
/// <summary>
/// 请求短信认证。
/// </summary>
/// <param name="mobilePhoneNumber">手机号。</param>
/// <param name="name">应用名称。</param>
/// <param name="op">进行的操作名称。</param>
/// <param name="ttl">验证码失效时间。</param>
/// <returns></returns>
public static Task<bool> RequestSMSCodeAsync(string mobilePhoneNumber, string name, string op, int ttl = 10)
{
return RequestSMSCodeAsync(mobilePhoneNumber, name, op, ttl, CancellationToken.None);
}
/// <summary>
/// 请求发送验证码。
/// </summary>
@ -131,7 +117,7 @@ namespace LeanCloud {
/// <param name="op">进行的操作名称。</param>
/// <param name="ttl">验证码失效时间。</param>
/// <param name="cancellationToken">Cancellation token。</param>
public static Task<bool> RequestSMSCodeAsync(string mobilePhoneNumber, string name, string op, int ttl = 10, CancellationToken cancellationToken = default(CancellationToken))
public static Task<bool> RequestSMSCodeAsync(string mobilePhoneNumber, string name, string op, int ttl = 10, CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(mobilePhoneNumber))
{
@ -202,8 +188,7 @@ namespace LeanCloud {
string template,
IDictionary<string, object> env,
string sign = "",
string validateToken = "",
CancellationToken cancellationToken = default(CancellationToken))
string validateToken = "")
{
if (string.IsNullOrEmpty(mobilePhoneNumber))
@ -318,7 +303,7 @@ namespace LeanCloud {
/// <param name="code">User's input of this captcha.</param>
/// <param name="cancellationToken">CancellationToken.</param>
/// <returns></returns>
public Task VerifyAsync(string code, CancellationToken cancellationToken = default(CancellationToken))
public Task VerifyAsync(string code)
{
return AVCloud.VerifyCaptchaAsync(code, Token);
}
@ -331,10 +316,10 @@ namespace LeanCloud {
/// <param name="height">captcha image height.</param>
/// <param name="cancellationToken">CancellationToken.</param>
/// <returns>an instance of Captcha.</returns>
public static Task<Captcha> RequestCaptchaAsync(int width = 85, int height = 30, CancellationToken cancellationToken = default(CancellationToken))
public static Task<Captcha> RequestCaptchaAsync(int width = 85, int height = 30, CancellationToken cancellationToken = default)
{
var path = String.Format("requestCaptcha?width={0}&height={1}", width, height);
var command = new AVCommand(path, method: "GET", sessionToken: null, data: null);
var command = new AVCommand(path, "GET", null, data: null);
return AVPlugins.Instance.CommandRunner.RunCommandAsync(command, cancellationToken: cancellationToken).OnSuccess(t =>
{
var decoded = AVDecoder.Instance.Decode(t.Result.Item2) as IDictionary<string, object>;
@ -353,14 +338,14 @@ namespace LeanCloud {
/// <param name="code">User's input of this captcha.</param>
/// <param name="cancellationToken">CancellationToken.</param>
/// <returns></returns>
public static Task<string> VerifyCaptchaAsync(string code, string token, CancellationToken cancellationToken = default(CancellationToken))
public static Task<string> VerifyCaptchaAsync(string code, string token, CancellationToken cancellationToken = default)
{
var data = new Dictionary<string, object>
{
{ "captcha_token", token },
{ "captcha_code", code },
};
var command = new AVCommand("verifyCaptcha", method: "POST", sessionToken: null, data: data);
var command = new AVCommand("verifyCaptcha", "POST", null, data: data);
return AVPlugins.Instance.CommandRunner.RunCommandAsync(command, cancellationToken: cancellationToken).ContinueWith(t =>
{
if (!t.Result.Item2.ContainsKey("validate_token"))
@ -374,7 +359,7 @@ namespace LeanCloud {
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static Task<IDictionary<string, object>> GetCustomParametersAsync(CancellationToken cancellationToken = default(CancellationToken))
public static Task<IDictionary<string, object>> GetCustomParametersAsync(CancellationToken cancellationToken = default)
{
var command = new AVCommand(string.Format("statistics/apps/{0}/sendPolicy", AVClient.CurrentConfiguration.ApplicationId),
method: "GET",
@ -397,7 +382,7 @@ namespace LeanCloud {
public string Signature { internal set; get; }
}
public static Task<RealtimeSignature> RequestRealtimeSignatureAsync(CancellationToken cancellationToken = default(CancellationToken))
public static Task<RealtimeSignature> RequestRealtimeSignatureAsync(CancellationToken cancellationToken = default)
{
return AVUser.GetCurrentUserAsync(cancellationToken).OnSuccess(t =>
{
@ -405,7 +390,7 @@ namespace LeanCloud {
}).Unwrap();
}
public static Task<RealtimeSignature> RequestRealtimeSignatureAsync(AVUser user, CancellationToken cancellationToken = default(CancellationToken))
public static Task<RealtimeSignature> RequestRealtimeSignatureAsync(AVUser user, CancellationToken cancellationToken = default)
{
var command = new AVCommand(string.Format("rtm/sign"),
method: "POST",
@ -518,7 +503,7 @@ namespace LeanCloud {
var user = t.Result;
var encodedParameters = Encode(parameters);
var command = new AVCommand(
string.Format("call/{0}", Uri.EscapeUriString(this.FunctionName)),
string.Format("call/{0}", Uri.EscapeUriString(FunctionName)),
method: "POST",
sessionToken: user != null ? user.SessionToken : null,
data: encodedParameters);

View File

@ -147,7 +147,7 @@ namespace LeanCloud
}
}
internal static Task<string> GetCurrentSessionTokenAsync(CancellationToken cancellationToken = default(CancellationToken))
internal static Task<string> GetCurrentSessionTokenAsync(CancellationToken cancellationToken = default)
{
return CurrentUserController.GetCurrentSessionTokenAsync(cancellationToken);
}
@ -936,24 +936,13 @@ namespace LeanCloud
return AVUser.LogInByMobilePhoneNumberAsync(mobilePhoneNumber, password, CancellationToken.None);
}
/// <summary>
/// 以手机号和验证码匹配登陆
/// </summary>
/// <param name="mobilePhoneNumber">手机号</param>
/// <param name="smsCode">短信验证码</param>
/// <returns></returns>
public static Task<AVUser> LogInBySmsCodeAsync(string mobilePhoneNumber, string smsCode)
{
return AVUser.LogInBySmsCodeAsync(mobilePhoneNumber, smsCode, CancellationToken.None);
}
/// <summary>
/// 用邮箱作和密码匹配登录
/// </summary>
/// <param name="email">邮箱</param>
/// <param name="password">密码</param>
/// <returns></returns>
public static Task<AVUser> LogInByEmailAsync(string email, string password, CancellationToken cancellationToken = default(CancellationToken))
public static Task<AVUser> LogInByEmailAsync(string email, string password, CancellationToken cancellationToken = default)
{
return UserController.LogInAsync(null, email, password, cancellationToken).OnSuccess(t => {
AVUser user = AVObject.FromState<AVUser>(t.Result, "_User");
@ -986,7 +975,7 @@ namespace LeanCloud
/// <param name="smsCode">短信验证码</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static Task<AVUser> LogInBySmsCodeAsync(string mobilePhoneNumber, string smsCode, CancellationToken cancellationToken = default(CancellationToken))
public static Task<AVUser> LogInBySmsCodeAsync(string mobilePhoneNumber, string smsCode, CancellationToken cancellationToken = default)
{
Dictionary<string, object> strs = new Dictionary<string, object>()
{
@ -1127,7 +1116,7 @@ namespace LeanCloud
/// <param name="smsCode">Sms code.</param>
public static Task<AVUser> LogInByMobilePhoneAsync(string mobilePhoneNumber, string smsCode)
{
return AVUser.LogInBySmsCodeAsync(mobilePhoneNumber, smsCode);
return LogInBySmsCodeAsync(mobilePhoneNumber, smsCode);
}
#endregion
#endregion