Connection.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Threading.Tasks;
  5. using BO.AppServer.Metadata.Dto;
  6. using BO.AppServer.Metadata.Enums;
  7. using BO.Connector;
  8. using Quadarax.Foundation.Core.Data.Interface.Entity.Dto;
  9. using Quadarax.Foundation.Core.Logging;
  10. namespace Connector.PS
  11. {
  12. public class Connection : AbstractConnection
  13. {
  14. #region *** Private fields ***
  15. protected override string ApiName => "PS";
  16. private string _psIdentifier;
  17. #endregion
  18. #region *** Constructor ***
  19. public Connection(Uri uriApiBase, TimeSpan timeout,string psIdentifier, ILogHandler log = null, bool isDumpContentEnabled = false) : base(uriApiBase, timeout, log, isDumpContentEnabled)
  20. {
  21. if (string.IsNullOrEmpty(psIdentifier))
  22. throw new ArgumentNullException(nameof(psIdentifier));
  23. _psIdentifier = psIdentifier;
  24. }
  25. #endregion
  26. #region *** Public operations ***
  27. public async Task<IEnumerable<DocumentRDto>> GetDocumentsAsync(DocumentsScopeEnums scope)
  28. {
  29. if (!(scope == DocumentsScopeEnums.Pending || scope == DocumentsScopeEnums.Working))
  30. throw new ArgumentOutOfRangeException(
  31. $"Document list scope value must be '{DocumentsScopeEnums.Working}' or '{DocumentsScopeEnums.Pending}' int this context.");
  32. var result = await CallRequestAsync<ResultsValueDto<DocumentRDto>>($"{Ticket}/{_psIdentifier}/documents/{scope}", RestCallTypeEnum.Get);
  33. return result.Values;
  34. }
  35. public async Task<DocumentStatusEnum> GetDocumentStateAsync(long documentId)
  36. {
  37. var result = await CallRequestAsync<ResultValueDto<PingDto>>($"{Ticket}/{_psIdentifier}/document/{documentId}", RestCallTypeEnum.Get);
  38. return Enum.Parse<DocumentStatusEnum>(result.Value.Status);
  39. }
  40. public async Task<DocumentStatusEnum> SetDocumentStateAsync(long documentId, DocumentStatusEnum status)
  41. {
  42. var result = await CallRequestAsync<ResultValueDto<PingDto>>($"{Ticket}/{_psIdentifier}/document/{documentId}", RestCallTypeEnum.Put,new PingDto(){Status = status.ToString()});
  43. return Enum.Parse<DocumentStatusEnum>(result.Value.Status);
  44. }
  45. public async Task<Stream> GetDocumentContentAsync(long artifactId)
  46. {
  47. var result = await CallGetStreamAsync($"{Ticket}/{_psIdentifier}/artifact/{artifactId}");
  48. return result;
  49. }
  50. #endregion
  51. #region *** Private operations and overrides ***
  52. protected override void OnOpening()
  53. {
  54. base.OnOpening();
  55. var result = Task.Run(() => CallRequestAsync<ResultPlain>($"{Ticket}/register", RestCallTypeEnum.Post, new PingDto(){Status = _psIdentifier}));
  56. if (!result.Result.IsSuccess)
  57. throw result.Result.ToAggregateException();
  58. Log(LogSeverityEnum.Info,$"Process server '{_psIdentifier}' is registered.");
  59. }
  60. #endregion
  61. }
  62. }