Untitled

mail@pastecode.io avatar
unknown
csharp
6 months ago
1.7 kB
1
Indexable
Never
[HttpPost]
public async Task<IDataResult<ResultStatus<SendDataResult>>> SendData([FromBody] SendData data)
{
    var apiData = _apiManager.Get();

    using (HttpClient httpClient = new HttpClient())
    {
        httpClient.BaseAddress = new Uri(apiData.Data.ApiAdress);

        var loginRes = await _login.Login(apiData.Data.UserName, apiData.Data.Password);

        if (loginRes != null && loginRes.objects != null)
        {
            httpClient.DefaultRequestHeaders.Add("AToken", JsonConvert.SerializeObject(new AToken { TicketId = loginRes.objects.TicketId.ToString() }));

            var content = new StringContent(
                JsonConvert.SerializeObject(data),
                Encoding.UTF8,
                "application/json");

            var response = await httpClient.PostAsync(StationType.SAIS.ToString() + "/SendData", content);
            response.EnsureSuccessStatusCode();

            var responseContent = await response.Content.ReadAsStringAsync();

            var desResponseContent = JsonConvert.DeserializeObject<ResultStatus<SendDataResult>>(responseContent)!;

            TempLog.Write(DateTime.Now + ": " + Messages.ApiSendDataSuccces);

            return new SuccessDataResult<ResultStatus<SendDataResult>>(desResponseContent, Messages.ApiSendDataSuccces);
        }
        else
        {
            TempLog.Write(DateTime.Now + ": LoginRes or LoginRes.objects is null");
            // Handle the case where loginRes or loginRes.objects is null
            return new ErrorDataResult<ResultStatus<SendDataResult>>(null, "LoginRes or LoginRes.objects is null");
        }
    }
Leave a Comment