-
Notifications
You must be signed in to change notification settings - Fork 2
/
DefaultNovaPoshtaGateway.cs
220 lines (175 loc) · 6.98 KB
/
DefaultNovaPoshtaGateway.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//Copyright 2020 Alexey Prokhorov
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
using Baroque.NovaPoshta.Client.Domain;
using Baroque.NovaPoshta.Client.Http;
using Baroque.NovaPoshta.Client.Serialization;
using System;
namespace Baroque.NovaPoshta.Client
{
/// <summary>
/// Represents after serialization methods signature.
/// </summary>
/// <param name="request">Request reference</param>
/// <param name="serialized">Serialized request</param>
public delegate void RequestSerialized(object request, ref string serialized);
/// <summary>
/// Represents response arrived methods signature.
/// </summary>
/// <param name="data">Serialized response</param>
public delegate void ResponseArrived(ref string data);
/// <summary>
/// Represents response deserialized method signature
/// </summary>
/// <param name="deserialized">Deserialized response object</param>
/// <param name="response">Serialized response</param>
public delegate void ResponseDeserialized(object deserialized, ref string response);
/// <summary>
/// Represents default 'Nova Poshta' service gateway
/// </summary>
public class DefaultNovaPoshtaGateway : INovaPoshtaGateway
{
#region Fields
private string _fullUri;
#endregion
#region Properties
/// <summary>
/// Gets or sets individual api key
/// </summary>
public string ApiKey { get; set; }
/// <summary>
/// Gets 'Nova Poshta' service address
/// </summary>
public string Url { get; private set; } = "https://api.novaposhta.ua/";
/// <summary>
/// Gets Nova Poshta Api version
/// </summary>
public string Version { get; private set; } = "v2.0";
/// <summary>
/// Gets full http request address
/// </summary>
public Uri FullUri
{
get
{
return new Uri(_fullUri);
}
}
/// <summary>
/// Gets serialization helper which will use for connection
/// </summary>
public ISerializationHelper SerializationHelper { get; private set; } = new JsonSerializationHelper();
/// <summary>
/// Gets HTTP request helper, which allow to send request to service
/// </summary>
public IHttpRequestHelper HttpRequestHelper { get; private set; } = new HttpRequestHelper();
#endregion
#region Constructors
public DefaultNovaPoshtaGateway()
{
Initialize();
}
/// <summary>
/// Default nova poshta gateway
/// </summary>
/// <param name="apiKey">Your unique Nova Poshta service api key</param>
public DefaultNovaPoshtaGateway(string apiKey)
{
ApiKey = apiKey;
Initialize();
}
#endregion
#region Events
public event RequestSerialized RequestSerialized;
public event ResponseArrived ResponseArrived;
public event ResponseDeserialized ResponseDeserialized;
#endregion
#region Utilites
/// <summary>
/// Initialize 'Nova Poshta' gateway
/// </summary>
protected void Initialize()
{
_fullUri = $"{Url}{Version}/{SerializationHelper.Type.ToLower()}/";
}
#endregion
#region Methods
/// <summary>
/// Set api version property
/// </summary>
/// <param name="version">Version</param>
public void SetVersion(string version)
{
Version = version;
}
/// <summary>
/// Set serialization helper
/// </summary>
/// <param name="serializationHelper">Serialization helper</param>
public void SetSerializationHelper(ISerializationHelper serializationHelper)
{
SerializationHelper = serializationHelper;
}
/// <summary>
/// Create HTTP request to 'Nova Poshta' gateway
/// </summary>
/// <typeparam name="TResponse">Response type</typeparam>
/// <typeparam name="TRequest">Request type</typeparam>
/// <param name="request">Request instance</param>
/// <returns>Deserialized response</returns>
public TResponse CreateRequest<TRequest, TResponse>(IRequestEnvelope<TRequest> request)
where TRequest : class, new()
where TResponse : class, new()
{
if (request == null)
throw new ArgumentNullException(nameof(request));
//apply api key to request
request.ApiKey = this.ApiKey;
//serialize request
var serialized = SerializationHelper.Serialize(request);
//run methods processing serialized request
if (RequestSerialized != null)
RequestSerialized(request, ref serialized);
//create HTTP request
var responseData = CreateRequest(request.OverridedMethodUrl, serialized);
//run method processing arrived response data
if (ResponseArrived != null)
ResponseArrived(ref responseData);
var deserialized = SerializationHelper.Deserialize<TResponse>(responseData);
//run methods processing response deserialization result
if (ResponseDeserialized != null)
ResponseDeserialized(deserialized, ref responseData);
return deserialized;
}
/// <summary>
/// Create HTTP request to 'Nova Poshta' gateway
/// </summary>
/// <param name="data">Data to send, usually serialized at JSON or XML format.</param>
/// <returns>Gateway response. Serialized at XML or JSON format</returns>
public string CreateRequest(string overridedUri, string data)
{
//get request bytes
var requestData = HttpRequestHelper.Encoding.GetBytes(data);
//create http request
var httpRequest = new HttpRequest()
{
ContentType = SerializationHelper.ContentType,
Data = requestData,
Method = HttpMethod.POST,
Uri = string.IsNullOrEmpty(overridedUri) ? FullUri : new Uri(overridedUri)
};
//create http request
var responseBytes = HttpRequestHelper.CreateRequest(httpRequest);
var responseData = HttpRequestHelper.Encoding.GetString(responseBytes);
return responseData;
}
#endregion
}
}