Skip to content

Commit

Permalink
Extended IMessageStreamConfigurator.cs with optional headers and crea…
Browse files Browse the repository at this point in the history
…ted nuget publish script version-1.1.0
  • Loading branch information
numinnex committed Jul 27, 2023
1 parent 68cd13a commit ba077ce
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 24 deletions.
43 changes: 30 additions & 13 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,36 @@ on:
branches: [ "master" ]

jobs:
build:

build-and-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 7.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: Test
run: dotnet test --no-build --verbosity normal

nuget:
needs: build-and-test
runs-on: ubuntu-latest
if: ${{ needs.build-and-test.result == 'success' }}
steps:
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 7.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: Test
run: dotnet test --no-build --verbosity normal
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 7.0.x
- name: Restore dependencies
run: dotnet restore
- name: Publish
env:
NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}
run: chmod +x scripts/pack.sh && scripts/pack.sh
shell: bash

4 changes: 2 additions & 2 deletions Iggy_SDK.sln.DotSettings.user
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
<TestId>xUnit::8514D555-35CF-4538-97D3-DC42A182B694::net7.0::Iggy_SDK_Tests.MessageStreamTests.MessageStreamUnitTests</TestId>
</TestAncestor>
&lt;/SessionState&gt;</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=c6e7cdb2_002Da359_002D47ac_002D93bd_002De335f8b8d26f/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="CreateTopicAsync_Returns201Created_WhenSuccessfullyCreated #2" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=c6e7cdb2_002Da359_002D47ac_002D93bd_002De335f8b8d26f/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="CreateTopicAsync_Returns201Created_WhenSuccessfullyCreated #2" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
&lt;TestAncestor&gt;
&lt;TestId&gt;xUnit::8514D555-35CF-4538-97D3-DC42A182B694::net7.0::Iggy_SDK_Tests.MessageStreamTests.HttpMessageStream&lt;/TestId&gt;
&lt;TestId&gt;xUnit::8514D555-35CF-4538-97D3-DC42A182B694::net7.0::Iggy_SDK_Tests.MapperTests.BinaryMapper&lt;/TestId&gt;
&lt;TestId&gt;xUnit::8514D555-35CF-4538-97D3-DC42A182B694::net7.0::Iggy_SDK_Tests.UtilityTests.ToSnakeCaseMessagePolicy&lt;/TestId&gt;
&lt;TestId&gt;xUnit::8514D555-35CF-4538-97D3-DC42A182B694::net7.0::Iggy_SDK_Tests.ContractTests.TcpContract.TcpContracts_MessageSendRequest_HasCorrectBytes&lt;/TestId&gt;
&lt;/TestAncestor&gt;
&lt;/SessionState&gt;</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=fdae582f_002D649c_002D46a6_002D8ad5_002D876f70bf8246/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="GetTopicByIdAsync_Returns200Ok_WhenFound" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=fdae582f_002D649c_002D46a6_002D8ad5_002D876f70bf8246/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="GetTopicByIdAsync_Returns200Ok_WhenFound" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
&lt;TestAncestor&gt;
&lt;TestId&gt;xUnit::8514D555-35CF-4538-97D3-DC42A182B694::net7.0::Iggy_SDK_Tests.MessageStreamTests.MessageStreamUnitTests&lt;/TestId&gt;
&lt;TestId&gt;xUnit::8514D555-35CF-4538-97D3-DC42A182B694::net7.0::Iggy_SDK_Tests.ContractTests.TcpContractTests.GetMessages_ReturnsCorrectByteArray&lt;/TestId&gt;
Expand Down
5 changes: 5 additions & 0 deletions Iggy_SDK/Configuration/IMessageStreamConfigurator.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Net.Http.Headers;
using Iggy_SDK.Contracts.Http;
using Iggy_SDK.Enums;

namespace Iggy_SDK.Configuration;
Expand All @@ -6,4 +8,7 @@ public interface IMessageStreamConfigurator
{
public string BaseAdress { get; set; }
public Protocol Protocol { get; set; }
public IEnumerable<HttpRequestHeaderContract>? Headers { get; set; }
public int ReceiveBufferSize { get; set; }
public int SendBufferSize { get; set; }
}
5 changes: 5 additions & 0 deletions Iggy_SDK/Configuration/MessageStreamConfigurator.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Net.Http.Headers;
using Iggy_SDK.Contracts.Http;
using Iggy_SDK.Enums;

namespace Iggy_SDK.Configuration;
Expand All @@ -6,4 +8,7 @@ public sealed class MessageStreamConfigurator : IMessageStreamConfigurator
{
public string BaseAdress { get; set; }

Check warning on line 9 in Iggy_SDK/Configuration/MessageStreamConfigurator.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Non-nullable property 'BaseAdress' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 9 in Iggy_SDK/Configuration/MessageStreamConfigurator.cs

View workflow job for this annotation

GitHub Actions / nuget

Non-nullable property 'BaseAdress' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public Protocol Protocol { get; set; } = Protocol.Http;
public IEnumerable<HttpRequestHeaderContract>? Headers { get; set; } = null;
public int ReceiveBufferSize { get; set; } = 8192;
public int SendBufferSize { get; set; } = 8192;
}
7 changes: 7 additions & 0 deletions Iggy_SDK/Contracts/Http/HttpRequestHeaderContract.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Iggy_SDK.Contracts.Http;

public sealed class HttpRequestHeaderContract
{
public required string Name { get; init; }
public required IEnumerable<string> Values { get; init; }
}
23 changes: 17 additions & 6 deletions Iggy_SDK/Factory/MessageStreamFactory.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.ComponentModel;
using System.Net;
using System.Net.Http.Headers;
using System.Net.Quic;
using System.Net.Security;
using System.Net.Sockets;
Expand All @@ -22,29 +23,39 @@ public static IMessageStream CreateMessageStream(Action<IMessageStreamConfigurat

return config.Protocol switch
{
Protocol.Http => CreateHttpMessageStream(config.BaseAdress),
Protocol.Tcp => CreateTcpMessageStream(config.BaseAdress),
Protocol.Http => CreateHttpMessageStream(config),
Protocol.Tcp => CreateTcpMessageStream(config),
_ => throw new InvalidEnumArgumentException()
};
}



private static TcpMessageStream CreateTcpMessageStream(string configBaseAdress)
private static TcpMessageStream CreateTcpMessageStream(IMessageStreamConfigurator options)
{
var urlPortSplitter = configBaseAdress.Split(":");
var urlPortSplitter = options.BaseAdress.Split(":");
if (urlPortSplitter.Length > 2)
{
throw new InvalidBaseAdressException();
}
var client = new TcpClient(urlPortSplitter[0], int.Parse(urlPortSplitter[1]));
client.SendBufferSize = options.SendBufferSize;
options.ReceiveBufferSize = options.ReceiveBufferSize;
return new TcpMessageStream(client);
}

private static HttpMessageStream CreateHttpMessageStream(string baseAdress)
private static HttpMessageStream CreateHttpMessageStream(IMessageStreamConfigurator options)
{
var client = new HttpClient();
client.BaseAddress = new Uri(baseAdress);
client.BaseAddress = new Uri(options.BaseAdress);
if (options.Headers is not null)
{
foreach (var header in options.Headers)
{
client.DefaultRequestHeaders.Add(header.Name, header.Values);
}
}

return new HttpMessageStream(client);
}
}
1 change: 1 addition & 0 deletions Iggy_SDK/Iggy_SDK.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<PackageId>Iggy</PackageId>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
6 changes: 3 additions & 3 deletions Iggy_SDK/MessageStream/Implementations/HttpMessageStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,10 @@ public Task LeaveConsumerGroupAsync(LeaveConsumerGroupRequest request)

private async Task HandleResponseAsync(HttpResponseMessage response)
{
if (response.StatusCode is HttpStatusCode.BadRequest or HttpStatusCode.NotFound)
if ((int)response.StatusCode > 300 && (int)response.StatusCode < 500)
{
var err = await response.Content.ReadFromJsonAsync<ErrorModel>(_toSnakeCaseOptions);
throw new InvalidResponseException(err.Reason);
var err = await response.Content.ReadAsStringAsync();
throw new InvalidResponseException(err);
}
if (response.StatusCode == HttpStatusCode.InternalServerError)
{
Expand Down
40 changes: 40 additions & 0 deletions scripts/pack.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/bash

# Function to extract the version number from the commit message using regular expressions
function extract_version {
local commit_message="$1"

if [[ $commit_message =~ version-([0-9]+\.[0-9]+\.[0-9]+) ]]; then
version=${BASH_REMATCH[1]}
echo "$version"
else
echo "Version not found in the commit message."
exit 1
fi
}

commit_message=$(git log -1 --pretty=format:"%s")

version=$(extract_version "$commit_message")

echo "Extracted version: $version"

echo "Executing after success scripts on branch $GITHUB_REF_NAME"
echo "Triggering Nuget package build"

cd Iggy_SDK
dotnet pack -c release /p:PackageVersion=$version --no-restore -o .

echo "Uploading Iggy package to Nuget using branch $GITHUB_REF_NAME"

case "$GITHUB_REF_NAME" in
"master")
dotnet nuget push *.nupkg -k $NUGET_API_KEY -s https://api.nuget.org/v3/index.json
echo "Published package succesfully!"
;;
*)
echo "Skipping NuGet package push as the branch is not master."
;;
esac


0 comments on commit ba077ce

Please sign in to comment.