// // LogServiceClientBuilders.cs // // Author: // MiNG // // Copyright (c) 2018 Alibaba Cloud // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. using System; using System.Collections.Generic; using Aliyun.Api.LogService.Domain.Log; using Aliyun.Api.LogService.Utils; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace Aliyun.Api.LogService.Infrastructure.Protocol.Http { public static class LogHeaderExtensions { private static T? ParseNullable(this String source, Func parse) where T : struct { if (source.IsEmpty()) { return null; } return parse(source); } private static T ParseNotNull(this String source, Func parse) { return parse(source); } #region PullLogs /// /// 获取下一条数据的cursor。 /// /// PullLogs 的响应消息。 /// 下一条数据的cursor。 public static String GetLogCursor(this IResponse response) => response.Headers.GetValueOrDefault(LogHeaders.Cursor); /// /// 获取当前返回数量。 /// /// PullLogs 的响应消息。 /// 当前返回数量。 /// Header的值不是整数形式。 public static Int64 GetLogCount(this IResponse response) => response.Headers.GetValueOrDefault(LogHeaders.Count) .ParseNotNull(Int64.Parse); /// /// 获取报文压缩类型(可空)。 /// /// PullLogs 的响应消息。 /// 报文压缩类型(可空)。 /// Header的值在当前SDK版本不支持。 /// public static CompressType? GetLogCompressType(this IResponse response) => response.Headers.GetValueOrDefault(LogHeaders.CompressType) .ParseNullable(x => (CompressType) Enum.Parse(typeof(CompressType), x, true)); /// /// 获取响应消息中 Body 的原始大小。 /// /// PullLogs 的响应消息。 /// 响应消息中 Body 的原始大小。 /// 当Header的值不是整数形式。 public static Int64 GetLogBodyRawSize(this IResponse response) => response.Headers.GetValueOrDefault(LogHeaders.BodyRawSize) .ParseNotNull(Int64.Parse); #endregion #region GetLogs /// /// 获取查询结果的状态。 /// /// GetLogs 的响应消息。 /// 获取查询结果的状态。 /// Header的值在当前SDK版本不支持。 /// public static LogProgressState GetLogProgress(this IResponse response) => response.Headers.GetValueOrDefault(LogHeaders.Progress) .ParseNotNull(x => (LogProgressState) Enum.Parse(typeof(LogProgressState), x, true)); /// /// 获取当前返回数量。 /// /// GetLogs 的响应消息。 /// 当前返回数量。 /// Header的值不是整数形式。 public static Int64 GetLogCount(this IResponse response) => response.Headers.GetValueOrDefault(LogHeaders.Count) .ParseNotNull(Int64.Parse); /// /// (TODO: 暂无文档) /// /// GetLogs 的响应消息。 /// /// Header的值不是整数形式。 public static Int64 GetLogProcessedRows(this IResponse response) => response.Headers.GetValueOrDefault(LogHeaders.ProcessedRows) .ParseNotNull(Int64.Parse); /// /// (TODO: 暂无文档) /// /// GetLogs 的响应消息。 /// /// Header的值不是整数形式。 public static Int64 GetLogElapsedMillisecond(this IResponse response) => response.Headers.GetValueOrDefault(LogHeaders.ElapsedMillisecond) .ParseNotNull(Int64.Parse); /// /// (TODO: 暂无文档) /// /// GetLogs 的响应消息。 /// public static LogQueryInfo GetLogQueryInfo(this IResponse response) => response.Headers.GetValueOrDefault(LogHeaders.QueryInfo) .ParseNotNull(JsonConvert.DeserializeObject); /// /// (TODO: 暂无文档) /// /// GetLogs 的响应消息。 /// /// Header的值不是JSON对象形式。 public static dynamic GetLogQueryInfoAsDynamic(this IResponse response) => response.Headers.GetValueOrDefault(LogHeaders.QueryInfo) .ParseNotNull(x => { if (x.IsEmpty()) { return new JObject(); } try { return JObject.Parse(x); } catch (JsonReaderException e) { // Prevent underlying type expose explicitly. throw new FormatException(e.Message, e); } }); /// /// (TODO: 暂无文档) /// /// GetLogs 的响应消息。 /// public static Boolean GetLogHasSql(this IResponse response) => response.Headers.GetValueOrDefault(LogHeaders.HasSql) .ParseNotNull(Boolean.Parse); /// /// (TODO: 暂无文档) /// /// GetLogs 的响应消息。 /// public static String GetLogAggQuery(this IResponse response) => response.Headers.GetValueOrDefault(LogHeaders.AggQuery); /// /// (TODO: 暂无文档) /// /// GetLogs 的响应消息。 /// public static String GetLogWhereQuery(this IResponse response) => response.Headers.GetValueOrDefault(LogHeaders.WhereQuery); #endregion #region GetHistograms /// /// 获取当前返回数量。 /// /// GetLogHistograms 的响应消息。 /// 当前返回数量。 /// Header的值不是整数形式。 public static Int64 GetLogCount(this IResponse response) => response.Headers.GetValueOrDefault(LogHeaders.Count) .ParseNotNull(Int64.Parse); /// /// 获取查询结果的状态。 /// /// GetLogHistograms 的响应消息。 /// 获取查询结果的状态。 /// Header的值在当前SDK版本不支持。 /// public static LogProgressState GetLogProgress(this IResponse response) => response.Headers.GetValueOrDefault(LogHeaders.Progress) .ParseNotNull(x => (LogProgressState) Enum.Parse(typeof(LogProgressState), x, true)); #endregion } }