From e94a27823cc01a1eaabc5115e3abb72e12e993e7 Mon Sep 17 00:00:00 2001 From: hzhang Date: Thu, 13 Mar 2025 11:00:11 +0000 Subject: [PATCH] refactor: web hook --- Alchegos.Core.csproj | 6 +- nswag.json => nswag.gitea.json | 0 nswag.youtrack.json | 100 ++++++++++++++++++ .../Gitea/GiteaApiClient.Extension.cs | 3 +- src/Services/RabbitMQ/IRabbitSubscriber.cs | 6 ++ src/Services/RabbitMQ/RabbitSubscriber.cs | 35 ++++++ .../Youtrack/YoutrackApiClient.Extension.cs | 16 +++ src/Services/Youtrack/YoutrackApiOptions.cs | 7 ++ 8 files changed, 169 insertions(+), 4 deletions(-) rename nswag.json => nswag.gitea.json (100%) create mode 100644 nswag.youtrack.json create mode 100644 src/Services/RabbitMQ/IRabbitSubscriber.cs create mode 100644 src/Services/RabbitMQ/RabbitSubscriber.cs create mode 100644 src/Services/Youtrack/YoutrackApiClient.Extension.cs create mode 100644 src/Services/Youtrack/YoutrackApiOptions.cs diff --git a/Alchegos.Core.csproj b/Alchegos.Core.csproj index 504f0e3..3ca0ff6 100644 --- a/Alchegos.Core.csproj +++ b/Alchegos.Core.csproj @@ -13,9 +13,11 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + - - + + + diff --git a/nswag.json b/nswag.gitea.json similarity index 100% rename from nswag.json rename to nswag.gitea.json diff --git a/nswag.youtrack.json b/nswag.youtrack.json new file mode 100644 index 0000000..35541f0 --- /dev/null +++ b/nswag.youtrack.json @@ -0,0 +1,100 @@ +{ + "runtime": "Net80", + "defaultVariables": null, + "documentGenerator": { + "fromDocument": { + "url": "http://localhost:18891/api/openapi.json", + "output": null, + "newLineBehavior": "Auto" + } + }, + "codeGenerators": { + "openApiToCSharpClient": { + "clientBaseClass": null, + "configurationClass": null, + "generateClientClasses": true, + "suppressClientClassesOutput": false, + "generateClientInterfaces": false, + "suppressClientInterfacesOutput": false, + "clientBaseInterface": null, + "injectHttpClient": true, + "disposeHttpClient": true, + "protectedMethods": [], + "generateExceptionClasses": true, + "exceptionClass": "ApiException", + "wrapDtoExceptions": true, + "useHttpClientCreationMethod": false, + "httpClientType": "System.Net.Http.HttpClient", + "useHttpRequestMessageCreationMethod": false, + "useBaseUrl": true, + "generateBaseUrlProperty": true, + "generateSyncMethods": false, + "generatePrepareRequestAndProcessResponseAsAsyncMethods": false, + "exposeJsonSerializerSettings": false, + "clientClassAccessModifier": "public", + "typeAccessModifier": "public", + "propertySetterAccessModifier": "", + "generateNativeRecords": false, + "generateContractsOutput": false, + "contractsNamespace": null, + "contractsOutputFilePath": null, + "parameterDateTimeFormat": "s", + "parameterDateFormat": "yyyy-MM-dd", + "generateUpdateJsonSerializerSettingsMethod": true, + "useRequestAndResponseSerializationSettings": false, + "serializeTypeInformation": false, + "queryNullValue": "", + "className": "YoutrackApiClient", + "operationGenerationMode": "MultipleClientsFromOperationId", + "additionalNamespaceUsages": [], + "additionalContractNamespaceUsages": [], + "generateOptionalParameters": false, + "generateJsonMethods": false, + "enforceFlagEnums": false, + "parameterArrayType": "System.Collections.Generic.IEnumerable", + "parameterDictionaryType": "System.Collections.Generic.IDictionary", + "responseArrayType": "System.Collections.Generic.ICollection", + "responseDictionaryType": "System.Collections.Generic.IDictionary", + "wrapResponses": false, + "wrapResponseMethods": [], + "generateResponseClasses": true, + "responseClass": "SwaggerResponse", + "namespace": "Alchegos.Core.Services.Youtrack", + "requiredPropertiesMustBeDefined": true, + "dateType": "System.DateTimeOffset", + "jsonConverters": null, + "anyType": "object", + "dateTimeType": "System.DateTimeOffset", + "timeType": "System.TimeSpan", + "timeSpanType": "System.TimeSpan", + "arrayType": "System.Collections.Generic.ICollection", + "arrayInstanceType": "System.Collections.ObjectModel.Collection", + "dictionaryType": "System.Collections.Generic.IDictionary", + "dictionaryInstanceType": "System.Collections.Generic.Dictionary", + "arrayBaseType": "System.Collections.ObjectModel.Collection", + "dictionaryBaseType": "System.Collections.Generic.Dictionary", + "classStyle": "Poco", + "jsonLibrary": "NewtonsoftJson", + "generateDefaultValues": true, + "generateDataAnnotations": true, + "excludedTypeNames": [], + "excludedParameterNames": [], + "handleReferences": false, + "generateImmutableArrayProperties": false, + "generateImmutableDictionaryProperties": false, + "jsonSerializerSettingsTransformationMethod": null, + "inlineNamedArrays": false, + "inlineNamedDictionaries": false, + "inlineNamedTuples": true, + "inlineNamedAny": false, + "generateDtoTypes": true, + "generateOptionalPropertiesAsNullable": false, + "generateNullableReferenceTypes": false, + "templateDirectory": null, + "serviceHost": null, + "serviceSchemes": null, + "output": "src/Services/Youtrack/YoutrackApiClient.cs", + "newLineBehavior": "Auto" + } + } +} \ No newline at end of file diff --git a/src/Services/Gitea/GiteaApiClient.Extension.cs b/src/Services/Gitea/GiteaApiClient.Extension.cs index ecb3a47..0a51f48 100644 --- a/src/Services/Gitea/GiteaApiClient.Extension.cs +++ b/src/Services/Gitea/GiteaApiClient.Extension.cs @@ -1,11 +1,10 @@ -using System.Net; using Microsoft.Extensions.Options; namespace Alchegos.Core.Services.Gitea; public partial class GiteaApiClient { - private GiteaApiOptions Options; + private GiteaApiOptions Options { get; set; } public GiteaApiClient(HttpClient client, IOptions options) { diff --git a/src/Services/RabbitMQ/IRabbitSubscriber.cs b/src/Services/RabbitMQ/IRabbitSubscriber.cs new file mode 100644 index 0000000..b9c7b45 --- /dev/null +++ b/src/Services/RabbitMQ/IRabbitSubscriber.cs @@ -0,0 +1,6 @@ +namespace Alchegos.Core.Services.RabbitMQ; + +public interface IRabbitSubscriber +{ + Task SubscribeAsync(string exchange, string queueName, IEnumerable routingKeys, Func messageHandler); +} \ No newline at end of file diff --git a/src/Services/RabbitMQ/RabbitSubscriber.cs b/src/Services/RabbitMQ/RabbitSubscriber.cs new file mode 100644 index 0000000..53f2dd2 --- /dev/null +++ b/src/Services/RabbitMQ/RabbitSubscriber.cs @@ -0,0 +1,35 @@ +using System.Text; +using RabbitMQ.Client; +using RabbitMQ.Client.Events; + +namespace Alchegos.Core.Services.RabbitMQ; + +public class RabbitSubscriber : IRabbitSubscriber +{ + public RabbitSubscriber(IRabbitService rabbitService) + { + RabbitService = rabbitService; + } + + private IRabbitService RabbitService { get; set; } + + public async Task SubscribeAsync(string exchange, string queueName, IEnumerable routingKeys, Func messageHandler) + { + IChannel channel = await RabbitService.GetChannelAsync(); + AsyncEventingBasicConsumer consumer = new AsyncEventingBasicConsumer(channel); + consumer.ReceivedAsync += async (sender, args) => + { + try + { + ReadOnlyMemory body = args.Body; + var message = Encoding.UTF8.GetString(body.ToArray()); + await messageHandler(message); + await channel.BasicAckAsync(args.DeliveryTag, false); + } + catch (Exception ex) + { + await channel.BasicNackAsync(args.DeliveryTag, false, true); + } + }; + } +} \ No newline at end of file diff --git a/src/Services/Youtrack/YoutrackApiClient.Extension.cs b/src/Services/Youtrack/YoutrackApiClient.Extension.cs new file mode 100644 index 0000000..3f7fcfd --- /dev/null +++ b/src/Services/Youtrack/YoutrackApiClient.Extension.cs @@ -0,0 +1,16 @@ +using Microsoft.Extensions.Options; + +namespace Alchegos.Core.Services.Youtrack; + +public partial class YoutrackApiClient +{ + private YoutrackApiOptions Options { get; set; } + + public YoutrackApiClient(HttpClient client, IOptions options) + { + _httpClient = client; + Options = options.Value; + BaseUrl = Options.BaseUrl; + _httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {Options.Token}"); + } +} diff --git a/src/Services/Youtrack/YoutrackApiOptions.cs b/src/Services/Youtrack/YoutrackApiOptions.cs new file mode 100644 index 0000000..9d79718 --- /dev/null +++ b/src/Services/Youtrack/YoutrackApiOptions.cs @@ -0,0 +1,7 @@ +namespace Alchegos.Core.Services.Youtrack; + +public class YoutrackApiOptions +{ + public string BaseUrl { get; set; } + public string Token { get; set; } +} \ No newline at end of file