基线 SVN r240

SVN-Revision: r240
This commit is contained in:
cyf
2025-02-06 06:46:06 +00:00
commit ab56a9bcf7
5317 changed files with 902274 additions and 0 deletions
@@ -0,0 +1,67 @@
using Lskj.Push.Push;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace Lskj.Push.Common
{
public class APIRequestException:Exception
{
private ResponseWrapper responseRequest;
public APIRequestException(ResponseWrapper responseRequest)
: base(responseRequest.exceptionString)
{
this.responseRequest = responseRequest;
}
public HttpStatusCode Status
{
get
{
return this.responseRequest.responseCode;
}
}
public long MsgId
{
get
{
return responseRequest.jpushError.msg_id;
}
}
public int ErrorCode
{
get
{
return responseRequest.jpushError.error.code;
}
}
public String ErrorMessage
{
get
{
return responseRequest.jpushError.error.message;
}
}
private JpushError ErrorObject()
{
return responseRequest.jpushError;
}
public int RateLimitQuota()
{
return responseRequest.rateLimitQuota;
}
public int RateLimitRemaining()
{
return responseRequest.rateLimitRemaining;
}
public int RateLimitReset()
{
return responseRequest.rateLimitReset;
}
}
}
@@ -0,0 +1,107 @@
using Lskj.Push.Push.Mode;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lskj.Push.Common
{
public class AudienceConverter : JsonConverter
{
/// <summary>
/// Platform whether this instance can convert the specified object type.
/// </summary>
/// <param name="objectType">Type of the object.</param>
/// <returns>
/// <c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.
/// </returns>
public override bool CanConvert(Type objectType)
{
if (objectType == typeof(Audience))
return true;
return false;
}
/// <summary>
/// Writes the JSON representation of the object.
/// </summary>
/// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
/// <param name="value">The value.</param>
/// <param name="serializer">The calling serializer.</param>
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
Audience audience = value as Audience;
if (audience == null)
{
return;
}
audience.Check();
if (audience.isAll())
{
writer.WriteValue(audience.allAudience);
//writer.WriteValue("alll");
}
else
{
var json = JsonConvert.SerializeObject(audience.dictionary);
writer.WriteRawValue(json);
}
}
/// <summary>
/// Reads the JSON representation of the object.
/// </summary>
/// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
/// <param name="objectType">Type of the object.</param>
/// <param name="existingValue">The existing property value of the JSON that is being converted.</param>
/// <param name="serializer">The calling serializer.</param>
/// <returns>The object value.</returns>
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
Audience audience = Audience.all();
if (reader.TokenType == JsonToken.Null)
{
return null;
}
else if (reader.TokenType == JsonToken.String)
{
audience.allAudience = reader.Value.ToString();
}
else if (reader.TokenType == JsonToken.StartObject)
{
audience.allAudience = null;
Dictionary<string, HashSet<string>> dictionary=new Dictionary<string,HashSet<string>>();
string key="key";
HashSet<string> value=null;
while (reader.Read())
{
Debug.WriteLine("Type:{0},Path:{1}", reader.TokenType, reader.Path);
switch (reader.TokenType)
{
case JsonToken.StartObject:
break;
case JsonToken.PropertyName:
key = reader.Value.ToString();
break;
case JsonToken.StartArray:
value = new HashSet<string>();
break;
case JsonToken.String:
value.Add(reader.Value.ToString());
break;
case JsonToken.EndArray:
{
dictionary.Add(key,value);
}
break;
case JsonToken.EndObject:
return audience;
}
}
audience.dictionary = dictionary;
}
return audience;
}
}
}
@@ -0,0 +1,144 @@
using Lskj.Push.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Diagnostics;
using Newtonsoft.Json;
using Lskj.Push.Common.Resp;
namespace Lskj.Push.Common
{
class BaseHttpClient
{
private const String CHARSET = "UTF-8";
private const String RATE_LIMIT_QUOTA = "X-Rate-Limit-Limit";
private const String RATE_LIMIT_Remaining = "X-Rate-Limit-Remaining";
private const String RATE_LIMIT_Reset = "X-Rate-Limit-Reset";
protected const int RESPONSE_OK = 200;
//设置连接超时时间
private const int DEFAULT_CONNECTION_TIMEOUT = (20 * 1000); // milliseconds
//设置读取超时时间
private const int DEFAULT_SOCKET_TIMEOUT = (30 * 1000); // milliseconds
public ResponseWrapper sendPost(String url, String auth, String reqParams)
{
return this.sendRequest( "POST", url, auth, reqParams);
}
public ResponseWrapper sendDelete(String url, String auth, String reqParams)
{
return this.sendRequest("DELETE", url, auth, reqParams);
}
public ResponseWrapper sendGet(String url, String auth, String reqParams)
{
return this.sendRequest("GET", url, auth, reqParams);
}
/**
*
* method "POST" or "GET"
* url
* auth 可选
*/
public ResponseWrapper sendRequest(String method, String url, String auth,String reqParams)
{
Console.WriteLine("Send request - " + method.ToString() + " " + url + " "+ DateTime.Now);
if (null != reqParams)
{
Console.WriteLine("Request Content - " + reqParams +" "+ DateTime.Now);
}
ResponseWrapper result = new ResponseWrapper();
HttpWebRequest myReq = null;
HttpWebResponse response = null;
try
{
myReq = (HttpWebRequest)WebRequest.Create(url);
myReq.Method = method;
myReq.ContentType = "application/json";
if ( !String.IsNullOrEmpty(auth) )
{
myReq.Headers.Add("Authorization", "Basic " + auth);
}
if (method == "POST")
{
byte[] bs = UTF8Encoding.UTF8.GetBytes(reqParams);
myReq.ContentLength = bs.Length;
using (Stream reqStream = myReq.GetRequestStream())
{
reqStream.Write(bs, 0, bs.Length);
reqStream.Close();
}
}
response = (HttpWebResponse)myReq.GetResponse();
HttpStatusCode statusCode = response.StatusCode;
result.responseCode = statusCode;
if (Equals(response.StatusCode, HttpStatusCode.OK))
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8))
{
result.responseContent = reader.ReadToEnd();
}
String limitQuota = response.GetResponseHeader(RATE_LIMIT_QUOTA);
String limitRemaining = response.GetResponseHeader(RATE_LIMIT_Remaining);
String limitReset = response.GetResponseHeader(RATE_LIMIT_Reset);
result.setRateLimit(limitQuota, limitRemaining, limitReset);
Console.WriteLine("Succeed to get response - 200 OK" +" "+ DateTime.Now);
Console.WriteLine("Response Content - {0}", result.responseContent +" "+ DateTime.Now);
}
}
catch (WebException e)
{
if (e.Status == WebExceptionStatus.ProtocolError)
{
HttpStatusCode errorCode = ((HttpWebResponse)e.Response).StatusCode;
string statusDescription = ((HttpWebResponse)e.Response).StatusDescription;
using (StreamReader sr = new StreamReader(((HttpWebResponse)e.Response).GetResponseStream(), System.Text.Encoding.UTF8))
{
result.responseContent = sr.ReadToEnd();
}
result.responseCode = errorCode;
result.exceptionString = e.Message;
String limitQuota = ((HttpWebResponse)e.Response).GetResponseHeader(RATE_LIMIT_QUOTA);
String limitRemaining = ((HttpWebResponse)e.Response).GetResponseHeader(RATE_LIMIT_Remaining);
String limitReset = ((HttpWebResponse)e.Response).GetResponseHeader(RATE_LIMIT_Reset);
result.setRateLimit(limitQuota, limitRemaining, limitReset);
Debug.Print(e.Message);
result.setErrorObject();
Console.WriteLine(string.Format("fail to get response - {0}", errorCode) + " "+ DateTime.Now);
Console.WriteLine(string.Format("Response Content - {0}", result.responseContent) + " "+ DateTime.Now);
throw new APIRequestException(result);
}
else
{//
throw new APIConnectionException(e.Message);
}
}
//这里不再抓取非http的异常,如果异常抛出交给开发者自行处理
//catch (System.Exception ex)
//{
// String errorMsg = ex.Message;
// Debug.Print(errorMsg);
//}
finally
{
if (response != null)
{
response.Close();
}
if(myReq != null)
{
myReq.Abort();
}
}
return result;
}
}
}
+51
View File
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lskj.Push.Common
{
public abstract class BaseResult
{
public const int ERROR_CODE_NONE = -1;
public const int ERROR_CODE_OK = 0;
public const String ERROR_MESSAGE_NONE = "None error message.";
public const int RESPONSE_OK = 200;
private ResponseWrapper responseResult;
public ResponseWrapper ResponseResult
{
get { return responseResult; }
set { responseResult = value; }
}
public abstract bool isResultOK();
// public override String getErrorMessage();
public int getRateLimitQuota() {
if (null != responseResult) {
return responseResult.rateLimitQuota;
}
return 0;
}
public int getRateLimitRemaining() {
if (null != responseResult) {
return responseResult.rateLimitRemaining;
}
return 0;
}
public int getRateLimitReset() {
if (null != responseResult) {
return responseResult.rateLimitReset;
}
return 0;
}
}
}
+19
View File
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Reflection;
namespace Lskj.Push.Common
{
public enum DeviceType
{
[Description("android")] android,
[Description("ios")] ios,
[Description("winphone")] winphone
}
}
@@ -0,0 +1,112 @@
using Lskj.Push.Push.Mode;
using Lskj.Push.Util;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lskj.Push.Common
{
public class PlatformConverter:JsonConverter
{
/// <summary>
/// Platform whether this instance can convert the specified object type.
/// </summary>
/// <param name="objectType">Type of the object.</param>
/// <returns>
/// <c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.
/// </returns>
public override bool CanConvert(Type objectType)
{
if (objectType == typeof(Platform))
return true;
return false;
}
/// <summary>
/// Writes the JSON representation of the object.
/// </summary>
/// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
/// <param name="value">The value.</param>
/// <param name="serializer">The calling serializer.</param>
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
Platform platform = value as Platform;
if (platform == null)
{
return;
}
platform.Check();
if (platform.isAll())
{
writer.WriteValue(platform.allPlatform);
}
else
{
writer.WriteStartArray();
foreach (var item in platform.deviceTypes)
{
writer.WriteValue(item);
}
writer.WriteEndArray();
}
}
/// <summary>
/// Reads the JSON representation of the object.
/// </summary>
/// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
/// <param name="objectType">Type of the object.</param>
/// <param name="existingValue">The existing property value of the JSON that is being converted.</param>
/// <param name="serializer">The calling serializer.</param>
/// <returns>The object value.</returns>
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
Platform platform = Platform.all();
if (reader.TokenType == JsonToken.Null)
{
return null;
}
else if(reader.TokenType==JsonToken.StartArray)
{
platform.allPlatform = null;
platform.deviceTypes = ReadArray(reader);
}
else if (reader.TokenType==JsonToken.String)
{
platform.allPlatform = reader.Value.ToString();
}
else
{
return null;
}
return platform;
}
private HashSet<string> ReadArray(JsonReader reader)
{
HashSet<string> list = new HashSet<string>();
while (reader.Read())
{
switch (reader.TokenType)
{
case JsonToken.String:
list.Add(Convert.ToString(reader.Value, CultureInfo.InvariantCulture));
break;
case JsonToken.EndArray:
return list;
case JsonToken.Comment:
// skip
break;
default:
return null;
}
}
return null;
}
}
}
@@ -0,0 +1,81 @@
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization.Json;
using System.Net;
using Lskj.Push.Util;
using System.Diagnostics;
using Lskj.Push.Push;
using Newtonsoft.Json;
namespace Lskj.Push.Common
{
public class ResponseWrapper
{
private const int RESPONSE_CODE_NONE = -1;
//private static Gson _gson = new Gson();
public JpushError jpushError;
public HttpStatusCode responseCode = HttpStatusCode.BadRequest;
private String _responseContent;
public String responseContent
{
get
{
return _responseContent;
}
set
{
_responseContent = value;
}
}
public void setErrorObject()
{
if(!string.IsNullOrEmpty(_responseContent))
{
jpushError = JsonConvert.DeserializeObject<JpushError>(_responseContent);
}
}
public int rateLimitQuota;
public int rateLimitRemaining;
public int rateLimitReset;
public bool isServerResponse()
{
return responseCode == HttpStatusCode.OK;
}
public String exceptionString;
public ResponseWrapper() {
}
public void setRateLimit(String quota, String remaining, String reset) {
if (null == quota) return;
try
{
if (quota != "" && StringUtil.IsInt(quota))
{
rateLimitQuota = int.Parse(quota);
}
if (remaining!="" && StringUtil.IsInt(remaining))
{
rateLimitRemaining = int.Parse(remaining);
}
if (reset!="" && StringUtil.IsInt(reset))
{
rateLimitReset = int.Parse(reset);
}
Console.WriteLine(string.Format("JPush API Rate Limiting params - quota:{0}, remaining:{1}, reset:{2} ", quota, remaining, reset) +" "+ DateTime.Now);
}
catch(Exception e)
{
Debug.Print(e.Message);
}
}
}
}
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lskj.Push.Common
{
public class ServiceHelper
{
private const int MAX_BADGE_NUMBER = 99999;
private const int MIN = 100000;
private const int MAX = int.MaxValue;
public static int generateSendno()
{
Random random = new Random();
return random.Next((MAX - MIN) + 1) + MIN;
}
public static bool isValidIntBadge(int intBadge)
{
if (intBadge >= 0 && intBadge <= MAX_BADGE_NUMBER)
{
return true;
}
return false;
}
}
}
+18
View File
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lskj.Push.Common
{
public enum TimeUnit
{
HOUR,
DAY,
MONTH
}
}
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lskj.Push.Common.Resp
{
public class APIConnectionException:Exception
{
public APIConnectionException(String message):base(message)
{
}
}
}
@@ -0,0 +1,25 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lskj.Push.Common.Resp
{
public class BooleanResult : DefaultResult
{
public bool result;
new public static BooleanResult fromResponse(ResponseWrapper responseWrapper)
{
BooleanResult tagListResult = new BooleanResult();
if (responseWrapper.isServerResponse())
{
tagListResult = JsonConvert.DeserializeObject<BooleanResult>(responseWrapper.responseContent);
}
tagListResult.ResponseResult = responseWrapper;
return tagListResult;
}
}
}
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace Lskj.Push.Common.Resp
{
public class DefaultResult:BaseResult
{
public static DefaultResult fromResponse(ResponseWrapper responseWrapper)
{
DefaultResult result = null;
if (responseWrapper.isServerResponse())
{
result = new DefaultResult();
}
result.ResponseResult=responseWrapper;
return result;
}
public override bool isResultOK()
{
if (Equals(ResponseResult.responseCode, HttpStatusCode.OK))
{
return true;
}
return false;
}
}
}