Skip to content

Commit

Permalink
Add project files.
Browse files Browse the repository at this point in the history
  • Loading branch information
iAlexeyProkhorov committed Mar 19, 2020
1 parent 8c59885 commit 68fbbe5
Show file tree
Hide file tree
Showing 75 changed files with 5,172 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Baroque.NovaPoshta.Client.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<Version>1.0.7</Version>
<Description>Library represents full client for 'Nova Poshta' API</Description>
<RepositoryType>Git</RepositoryType>
<PackageReleaseNotes>Contains full methods collection for 'Address', 'AddressGeneral', 'Common' models.</PackageReleaseNotes>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>

<ItemGroup>
<Folder Include="Domain\Countrparty\" />
</ItemGroup>

</Project>
25 changes: 25 additions & 0 deletions Baroque.NovaPoshta.Client.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.705
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Baroque.NovaPoshta.Client", "Baroque.NovaPoshta.Client.csproj", "{DC921B24-615E-4DC0-B54D-2C9F8B930D05}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{DC921B24-615E-4DC0-B54D-2C9F8B930D05}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DC921B24-615E-4DC0-B54D-2C9F8B930D05}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DC921B24-615E-4DC0-B54D-2C9F8B930D05}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DC921B24-615E-4DC0-B54D-2C9F8B930D05}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {24183619-64C7-451E-A455-65C267C2EC8A}
EndGlobalSection
EndGlobal
190 changes: 190 additions & 0 deletions DefaultNovaPoshtaGateway.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
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

/// <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 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()
{

}

/// <summary>
/// Default nova poshta gateway
/// </summary>
/// <param name="apiKey">Your unique Nova Poshta service api key</param>
public DefaultNovaPoshtaGateway(string apiKey)
{
this.ApiKey = apiKey;
}

#endregion

#region Events

public event RequestSerialized RequestSerialized;
public event ResponseArrived ResponseArrived;
public event ResponseDeserialized ResponseDeserialized;

#endregion

#region Methods

/// <summary>
/// Gets full http request address
/// </summary>
public Uri FullUri
{
get
{
return new Uri($"{Url}{Version}/{SerializationHelper.Type.ToLower()}/");
}
}

/// <summary>
/// Set api version property
/// </summary>
/// <param name="version">Version</param>
public void SetVersion(string version)
{
this.Version = version;
}

/// <summary>
/// Set Nova Poshta API url
/// </summary>
/// <param name="url">Url</param>
public void SetUrl(string url)
{
this.Url = url;
}

/// <summary>
/// Set serialization helper
/// </summary>
/// <param name="serializationHelper">Serialization helper</param>
public void SetSerializationHelper(ISerializationHelper serializationHelper)
{
this.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()
{
//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(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 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 = this.FullUri
};

//create http request
var responseBytes = HttpRequestHelper.CreateRequest(httpRequest);
var responseData = HttpRequestHelper.Encoding.GetString(responseBytes);

return responseData;
}

#endregion
}
}
10 changes: 10 additions & 0 deletions Domain/Address/AreasGetRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Baroque.NovaPoshta.Client.Domain.Address
{
/// <summary>
/// Get Ukraine areas collections. Represents request for 'getAreas' method of 'Address' model.
/// Documentation: https://devcenter.novaposhta.ua/docs/services/556d7ccaa0fe4f08e8f7ce43/operations/556d9130a0fe4f08e8f7ce48
/// </summary>
public class AreasGetRequest
{
}
}
41 changes: 41 additions & 0 deletions Domain/Address/AreasGetResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Runtime.Serialization;

namespace Baroque.NovaPoshta.Client.Domain.Address
{
/// <summary>
/// Contains list of Ukraine areas. Represents 'getAreas' method response of 'Address' model.
/// Documentation: https://devcenter.novaposhta.ua/docs/services/556d7ccaa0fe4f08e8f7ce43/operations/556d9130a0fe4f08e8f7ce48
/// </summary>
[DataContract]
public class AreasGetResponse : BaseResponseEnvelope<AreasGetResponse.Area>
{
/// <summary>
/// Represents area
/// </summary>
[DataContract]
public class Area : BaseRefItem
{
/// <summary>
/// Gets or sets area name
/// </summary>
[DataMember]
public string Description { get; set; }

/// <summary>
/// Gets or sets area city cented guid code
/// </summary>
[DataMember]
public Guid AreasCenter { get; set; }

/// <summary>
/// Represents area
/// </summary>
/// <returns>String</returns>
public override string ToString()
{
return Description;
}
}
}
}
24 changes: 24 additions & 0 deletions Domain/Address/CitiesGetRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Runtime.Serialization;

namespace Baroque.NovaPoshta.Client.Domain.Address
{
/// <summary>
/// Represents cities search request implemented via 'getCities' method in 'Address' model.
/// Documentation: https://devcenter.novaposhta.ua/docs/services/556d7ccaa0fe4f08e8f7ce43/operations/556d885da0fe4f08e8f7ce46
/// </summary>
[DataContract]
public class CitiesGetRequest
{
/// <summary>
/// Gets or sets city unique key
/// </summary>
public Guid? Ref { get; set; }

/// <summary>
/// Gets or sets city unique key
/// </summary>
[DataMember]
public string FindByString { get; set; }
}
}
Loading

0 comments on commit 68fbbe5

Please sign in to comment.