|
| 1 | +using Dtmcli.DtmImp; |
| 2 | +using DtmCommon; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Net.Http; |
| 6 | +using System.Text.Json.Serialization; |
| 7 | +using System.Threading; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using static Dtmcli.Constant; |
| 10 | + |
| 11 | +namespace Dtmcli |
| 12 | +{ |
| 13 | + public sealed class Xa : TransBase |
| 14 | + { |
| 15 | + private readonly IDtmClient _dtmClient; |
| 16 | + |
| 17 | + internal Xa(IDtmClient dtmHttpClient, string gid) |
| 18 | + { |
| 19 | + this._dtmClient = dtmHttpClient; |
| 20 | + this.Gid = gid; |
| 21 | + this.TransType = DtmCommon.Constant.TYPE_XA; |
| 22 | + this.BranchIDGen = new BranchIDGen(); |
| 23 | + } |
| 24 | + |
| 25 | + internal Xa(IDtmClient dtmHttpClient) |
| 26 | + { |
| 27 | + this._dtmClient = dtmHttpClient; |
| 28 | + } |
| 29 | + |
| 30 | + public async Task<string> CallBranch(object body, string url, CancellationToken cancellationToken = default) |
| 31 | + { |
| 32 | + using var response = await _dtmClient.TransRequestBranch( |
| 33 | + this, |
| 34 | + HttpMethod.Post, |
| 35 | + body, |
| 36 | + this.BranchIDGen.NewSubBranchID(), |
| 37 | + Constant.Request.BRANCH_ACTION, |
| 38 | + url, |
| 39 | + cancellationToken).ConfigureAwait(false); |
| 40 | + |
| 41 | + Exception ex = await Utils.RespAsErrorCompatible(response); |
| 42 | + if (null != ex) |
| 43 | + throw ex; |
| 44 | + |
| 45 | + return await response.Content.ReadAsStringAsync(); |
| 46 | + } |
| 47 | + |
| 48 | + [JsonIgnore] |
| 49 | + public string Phase2Url { get; set; } |
| 50 | + |
| 51 | + |
| 52 | +#if NET5_0_OR_GREATER |
| 53 | + public static Xa FromQuery(IDtmClient dtmClient, Microsoft.AspNetCore.Http.IQueryCollection query) |
| 54 | + { |
| 55 | + if (query.TryGetValue(Request.GID, out var gid) == false || string.IsNullOrEmpty(gid)) |
| 56 | + throw new ArgumentNullException(Request.GID); |
| 57 | + |
| 58 | + if (query.TryGetValue(Request.TRANS_TYPE, out var transType) == false || string.IsNullOrEmpty(transType)) |
| 59 | + throw new ArgumentNullException(Request.TRANS_TYPE); |
| 60 | + |
| 61 | + if (query.TryGetValue(Request.OP, out var op) == false || string.IsNullOrEmpty(op)) |
| 62 | + throw new ArgumentNullException(Request.OP); |
| 63 | + |
| 64 | + if (query.TryGetValue(Request.BRANCH_ID, out var branchID) == false || string.IsNullOrEmpty(branchID)) |
| 65 | + throw new ArgumentNullException(Request.BRANCH_ID); |
| 66 | + |
| 67 | + query.TryGetValue(Request.DTM, out var dtm); |
| 68 | + query.TryGetValue(Request.PHASE2_URL, out var phase2Url); |
| 69 | + |
| 70 | + return new(dtmClient) |
| 71 | + { |
| 72 | + Gid = gid, |
| 73 | + Dtm = dtm, |
| 74 | + Op = op, |
| 75 | + TransType = transType, |
| 76 | + Phase2Url = phase2Url, |
| 77 | + BranchIDGen = new BranchIDGen(branchID), |
| 78 | + }; |
| 79 | + } |
| 80 | +#else |
| 81 | + public static Xa FromQuery(IDtmClient dtmClient, IDictionary<string, string> query) |
| 82 | + { |
| 83 | + if (!query.TryGetValue(Request.GID, out var gid) == false || string.IsNullOrEmpty(gid)) |
| 84 | + throw new ArgumentNullException(Request.GID); |
| 85 | + |
| 86 | + if (query.TryGetValue(Request.TRANS_TYPE, out var transType) == false || string.IsNullOrEmpty(transType)) |
| 87 | + throw new ArgumentNullException(Request.TRANS_TYPE); |
| 88 | + |
| 89 | + if (query.TryGetValue(Request.OP, out var op) == false || string.IsNullOrEmpty(op)) |
| 90 | + throw new ArgumentNullException(Request.OP); |
| 91 | + |
| 92 | + if (query.TryGetValue(Request.BRANCH_ID, out var branchID) == false || string.IsNullOrEmpty(branchID)) |
| 93 | + throw new ArgumentNullException(Request.BRANCH_ID); |
| 94 | + |
| 95 | + query.TryGetValue(Request.DTM, out var dtm); |
| 96 | + query.TryGetValue(Request.PHASE2_URL, out var phase2Url); |
| 97 | + |
| 98 | + return new(dtmClient) |
| 99 | + { |
| 100 | + Gid = gid, |
| 101 | + Dtm = dtm, |
| 102 | + Op = op, |
| 103 | + TransType = transType, |
| 104 | + Phase2Url = phase2Url, |
| 105 | + BranchIDGen = new BranchIDGen(branchID), |
| 106 | + }; |
| 107 | + } |
| 108 | +#endif |
| 109 | + } |
| 110 | +} |
0 commit comments