当前位置: 首页 > article >正文

WPF实战项目十九(客户端):修改RestSharp的引用

 修改HttpRestClient,更新RestSharp到110.2.0,因为106版本和110版本的代码不一样,所以需要修改下代码

 

using Newtonsoft.Json;
using RestSharp;
using System;
using System.Threading.Tasks;
using WPFProjectShared;

namespace WPFProject.Service
{
    public class HttpRestClient
    {
        public readonly string apiUrl;
        //private readonly RestClient client;

        public HttpRestClient(string apiUrl)
        {
            this.apiUrl = apiUrl;
            //client = new RestClient();
        }

        public async Task<WebApiResponse> ExecuteAsync(BaseRequest baseRequest)
        {
            var request = new RestRequest(string.Empty, baseRequest.Method);
            request.AddHeader("Content-Type", baseRequest.ContenType.ToString());

            if (baseRequest.Parameter != null)
                request.AddParameter("param", JsonConvert.SerializeObject(baseRequest.Parameter), ParameterType.RequestBody);

            var options = new RestClientOptions();
            options.BaseUrl = new Uri(apiUrl + baseRequest.Route);
            var client = new RestClient(options);
            var response = await client.ExecuteAsync(request);

            JsonSerializerSettings jsSetting = new JsonSerializerSettings();
            jsSetting.NullValueHandling = NullValueHandling.Ignore;

            if (response.StatusCode == System.Net.HttpStatusCode.OK)
                return JsonConvert.DeserializeObject<WebApiResponse>(response.Content, jsSetting);
            else
                return new WebApiResponse()
                {
                    Status = false,
                    Result = null,
                    Message = response.ErrorMessage
                };

        }

        public async Task<WebApiResponse<T>> ExecuteAsync<T>(BaseRequest baseRequest)
        {
            var request = new RestRequest(string.Empty, baseRequest.Method);
            request.AddHeader("Content-Type", baseRequest.ContenType);
            if (baseRequest.Parameter != null)
                request.AddParameter("application/json", JsonConvert.SerializeObject(baseRequest.Parameter), ParameterType.RequestBody);

            var options = new RestClientOptions();
            options.BaseUrl = new Uri(apiUrl + baseRequest.Route);
            var client = new RestClient(options);
            var response = await client.ExecuteAsync(request);

            JsonSerializerSettings jsSetting = new JsonSerializerSettings();
            jsSetting.NullValueHandling = NullValueHandling.Ignore;

            if (response.StatusCode == System.Net.HttpStatusCode.OK)
                return JsonConvert.DeserializeObject<WebApiResponse<T>>(response.Content, jsSetting);

            else
                return new WebApiResponse<T>()
                {
                    Status = false,
                    Message = response.ErrorMessage
                };
        }
    }
}

Baseservice.cs也要相应的修改

using DryIoc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WPFProjectAPI.Context.UnitOfWork;
using WPFProjectShared;
using WPFProjectShared.Parameters;

namespace WPFProject.Service
{
    public class BaseService<TEntity> : IBaseService<TEntity> where TEntity : class
    {
        private readonly HttpRestClient client;
        private readonly string serviceName;

        public BaseService(HttpRestClient client, string serviceName)
        {
            this.client = client;
            this.serviceName = serviceName;
        }

        public async Task<WebApiResponse<TEntity>> AddAsync(TEntity entity)
        {
            BaseRequest request = new BaseRequest();
            request.Method = RestSharp.Method.Post;
            request.Route = $"api/{serviceName}/Add";
            request.Parameter = entity;
            return await client.ExecuteAsync<TEntity>(request);
        }

        public async Task<WebApiResponse> DeleteAsync(int id)
        {
            BaseRequest request = new BaseRequest();
            request.Method = RestSharp.Method.Delete;
            request.Route = $"api/{serviceName}/Delete?id={id}";
            return await client.ExecuteAsync(request);
        }

        public async Task<WebApiResponse<PagedList<TEntity>>> GetAllPageListAsync(QueryParameter parameter)
        {
            BaseRequest request = new BaseRequest();
            request.Method = RestSharp.Method.Get;
            request.Route = $"api/{serviceName}/GetAllPageList?pageIndex={parameter.PageIndex}" + $"&pageSize={parameter.PageSize}" + $"&search={parameter.Search}";
            return await client.ExecuteAsync<PagedList<TEntity>>(request);
        }

        public async Task<WebApiResponse<TEntity>> GetFirstOfDefaultAsync(int id)
        {
            BaseRequest request = new BaseRequest();
            request.Method = RestSharp.Method.Get;
            request.Route = $"api/{serviceName}/Get?Id={id}";
            return await client.ExecuteAsync<TEntity>(request);
        }

        public async Task<WebApiResponse<TEntity>> UpdateAsync(TEntity entity)
        {
            BaseRequest request = new BaseRequest();
            request.Method = RestSharp.Method.Post;
            request.Route = $"api/{serviceName}/Update";
            request.Parameter = entity;
            return await client.ExecuteAsync<TEntity>(request);
        }
    }
}


http://www.kler.cn/a/155745.html

相关文章:

  • 【数据结构与算法】第12课—数据结构之归并排序
  • 随时随地编码:香橙派Zero3上安装Code Server远程开发指南
  • 申论1_概括、分析
  • 什么岗位需要学习 OpenGL ES ?说说 3.X 的新特性
  • 矢量拟合(1)Sanathanan–Koerner算法
  • gpu-V100显卡相关知识
  • 【Docker】容器数据持久化及容器互联
  • ThinkPHP的方法接收json数据问题
  • 【数据挖掘】国科大刘莹老师数据挖掘课程作业 —— 第三次作业
  • Vue3中teleport如何使用
  • 详解Spring对Mybatis等持久化框架的整合
  • LeetCode - 100. 相同的树 (C语言,二叉树,配图,简单)
  • 代理模式介绍(静态代理、jdk动态代理、cglib代理)
  • 栈和队列的OJ题——14.用栈实现队列
  • Azure Machine Learning - Azure AI 搜索中的索引器
  • 【限时免费】20天拿下华为OD笔试之【哈希集合】2023B-明明的随机数【欧弟算法】全网注释最详细分类最全的华为OD真题题解
  • Qt/QML编程学习之心得:如何添加资源文件到QML工程(十一)
  • 【书信,推荐信】【推荐节日活动】
  • QML中常见布局方法
  • 最小生成树(简单讲解,通俗易懂)
  • 笔迹检验(四):笔迹检验的程序和方法
  • PyQt6 QComboBox下拉组合框控件
  • STM32串口接收不定长数据(接收中断+超时判断)
  • C++ Easyx 三子棋
  • PostgreSQL中常用的几种连接池总结及更新
  • 阻止事件默认行为