1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
-
- using Newtonsoft.Json.Linq;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Net.Http;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- using System.Security.Cryptography;
- using System.Net.Http.Json;
- using System.Text.Json;
- using System.IO;
- using TEAMModelOS.SDK.Extension;
- using System.Net;
- using TEAMModelOS.SDK.Models;
- namespace TEAMModelOS.SDK.DI
- {
-
- public class HttpTrigger
- {
- private readonly HttpClient _httpClient;
- public HttpTrigger(HttpClient httpClient)
- {
- _httpClient = httpClient;
- }
- // <summary>
- /// 请求信息
- /// </summary>
- /// <param name="robotUrl">釘釘Robot發送Url</param>
- /// <param name="secret">加簽密鑰</param>
- /// <param name="msg">發送訊息</param>
- /// <returns></returns>
- public async Task<(int status, string json)> RequestHttpTrigger(object data, string location, string url)
- {
- var keys = HttpTriggerUrl.HttpTrigger地址.GetDescriptionText().Split(',');
- string domain = "";
- if (location.Equals("China-Dep"))
- {
- domain = keys[1];
- }
- else if (location.Equals("China-Test"))
- {
- domain = keys[1];
- }
- else if (location.Equals("China"))
- {
- domain = keys[2];
- }
- else if (location.Equals("Global-Dep"))
- {
- domain = keys[3];
- }
- else if (location.Equals("Global-Test"))
- {
- domain = keys[3];
- }
- else if (location.Equals("Global"))
- {
- domain = keys[4];
- }
- string link = domain.Contains("localhost") ? $"http://{domain}/api/{url}" : $"https://{domain}/api/{url}";
- HttpContent httpContent = new StringContent(data.ToJsonString());
- HttpResponseMessage responseMessage = await _httpClient.PostAsync(link, httpContent);
- if (responseMessage.StatusCode == HttpStatusCode.OK)
- {
- string Content = await responseMessage.Content.ReadAsStringAsync();
- if (string.IsNullOrWhiteSpace(Content))
- {
- return (200, null) ;
- }
- else {
- Content.ToObject<JsonElement>().TryGetProperty("data", out JsonElement content);
- return (200, $"{content}");
- }
- }
- else
- {
- string Content = await responseMessage.Content?.ReadAsStringAsync();
- return (500, Content);
- }
- }
- public Task RequestHttpTrigger(Portrait portrait, object location, string v)
- {
- throw new NotImplementedException();
- }
- }
- public enum HttpTriggerUrl
- {
- [Description("localhost:7071,teammodelosfunction-test.chinacloudsites.cn,teammodelosfunction.chinacloudsites.cn,localhost:7071,teammodelfunction.azurewebsites.net")]
- HttpTrigger地址,
- }
- }
|