Connection.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net;
  5. using System.Threading.Tasks;
  6. using BO.AppServer.Metadata.Dto;
  7. using BO.AppServer.Metadata.Enums;
  8. using BO.Connector;
  9. using Quadarax.Foundation.Core.Data.Interface.Entity.Dto;
  10. using Quadarax.Foundation.Core.Logging;
  11. namespace Connector.PS
  12. {
  13. public class Connection : AbstractConnection, IConnectionPS
  14. {
  15. #region *** Private fields ***
  16. protected override string ApiName => "PS";
  17. private string _psIdentifier;
  18. private IPAddress _ip;
  19. private int _poolSize;
  20. private long? _registrationId;
  21. #endregion
  22. #region *** Constructor ***
  23. public Connection(Uri uriApiBase, TimeSpan timeout,string psIdentifier,IPAddress ipAddress, int poolSize, ILogHandler log = null, bool isDumpContentEnabled = false) : base(uriApiBase, timeout, log, isDumpContentEnabled)
  24. {
  25. if (string.IsNullOrEmpty(psIdentifier))
  26. throw new ArgumentNullException(nameof(psIdentifier));
  27. _psIdentifier = psIdentifier;
  28. _ip = ipAddress ?? throw new ArgumentNullException(nameof(ipAddress));
  29. _poolSize = poolSize;
  30. }
  31. #endregion
  32. #region *** Public operations ***
  33. public async Task<IEnumerable<DocumentRDto>> GetDocumentsAsync(DocumentsScopeEnums scope)
  34. {
  35. if (!(scope == DocumentsScopeEnums.Pending || scope == DocumentsScopeEnums.Working))
  36. throw new ArgumentOutOfRangeException(
  37. $"Document list scope value must be '{DocumentsScopeEnums.Working}' or '{DocumentsScopeEnums.Pending}' int this context.");
  38. //{ticket}/ps/{registrationId}/new
  39. var result = scope == DocumentsScopeEnums.Pending
  40. ? await CallRequestAsync<ResultsValueDto<DocumentRDto>>($"{Ticket}/queue/{_registrationId}/pending",
  41. RestCallTypeEnum.Get)
  42. : await CallRequestAsync<ResultsValueDto<DocumentRDto>>($"{Ticket}/queue/{_registrationId}/working",
  43. RestCallTypeEnum.Get);
  44. return result.Values;
  45. }
  46. public async Task<DocumentStatusEnum> GetDocumentStateAsync(long documentId)
  47. {
  48. //{ticket}/queue/{registrationId}/document/{documentId}/status
  49. var result = await CallRequestAsync<ResultValueDto<PingDto>>($"{Ticket}/queue/{_registrationId}/document/{documentId}/status", RestCallTypeEnum.Get);
  50. return Enum.Parse<DocumentStatusEnum>(result.Value.Status);
  51. }
  52. public async Task<DocumentRDto> GetDocumentInfoAsync(long documentId)
  53. {
  54. //{ticket}/queue/{registrationId}/document/{documentId}/info
  55. var result = await CallRequestAsync<ResultValueDto<DocumentRDto>>($"{Ticket}/queue/{_registrationId}/document/{documentId}/info", RestCallTypeEnum.Get);
  56. return result.Value;
  57. }
  58. public async Task<DocumentStatusEnum> SetDocumentStateAsync(long documentId, DocumentStatusEnum status)
  59. {
  60. if (!(status == DocumentStatusEnum.New || status == DocumentStatusEnum.Processing || status == DocumentStatusEnum.DoneOk || status == DocumentStatusEnum.DoneFail))
  61. throw new ArgumentOutOfRangeException(
  62. $"Document status value must be '{DocumentStatusEnum.New}' or '{DocumentStatusEnum.Processing}' or '{DocumentStatusEnum.DoneOk}' or '{DocumentStatusEnum.DoneFail}' in this context.");
  63. var result = await CallRequestAsync<ResultValueDto<PingDto>>($"{Ticket}/queue/{_registrationId}/document/{documentId}/status/{status}", RestCallTypeEnum.Put);
  64. return Enum.Parse<DocumentStatusEnum>(result.Value.Status);
  65. }
  66. public async Task<Stream> GetDocumentContentAsync(long documentId, long artifactId)
  67. {
  68. var result = await CallGetStreamAsync($"{Ticket}/queue/{_registrationId}/document/{documentId}/artifact/{artifactId}");
  69. return result;
  70. }
  71. public async Task<ArtifactRDto> SetDocumentContentAsync(long documentId, string fileName, string mimeType, bool overwriteIfExists, Stream contentStream)
  72. {
  73. CheckIsOpen();
  74. var result = await CallPostStreamMultipartAsync<ResultValueDto<ArtifactRDto>>($"{Ticket}/queue/{_registrationId}/document/{documentId}",fileName,contentStream,mimeType);
  75. return result.Value;
  76. }
  77. #endregion
  78. #region *** Private operations and overrides ***
  79. protected override void OnOpening()
  80. {
  81. base.OnOpening();
  82. var result = Task.Run(() => CallRequestAsync<ResultValueDto<RegistrationRDto>>($"{Ticket}/register", RestCallTypeEnum.Get, GetRegistration()));
  83. if (!result.Result.IsSuccess)
  84. throw result.Result.ToAggregateException();
  85. Log(LogSeverityEnum.Info,$"Process server '{_psIdentifier}' [{result.Result.Value.Id}] is registered.");
  86. }
  87. private RegistrationCDto GetRegistration()
  88. {
  89. return new RegistrationCDto()
  90. {
  91. LocationIp = _ip.ToString(),
  92. Psinstance = _psIdentifier,
  93. PoolSize = _poolSize,
  94. Id = _registrationId.GetValueOrDefault(0)
  95. };
  96. }
  97. #endregion
  98. }
  99. }