• HTTP框架 - HttpMaster 核心基类上传


    场景

    在电子商务应用中,可能需要与多个供应商和物流服务提供商进行通信。这些服务提供商可能具有不同的 API 和身份验证要求。通过封装 HTTP 工具,可以统一管理与这些服务提供商的通信,处理价格查询、订单跟踪、库存查询等任务。如果供应商或物流服务提供商更改其 API,您只需更新 HTTP 工具的相关部分,而不必更改整个应用程序。

    意义

    抽象底层细节:第三方接口通常具有不同的协议和细节,例如 REST、SOAP、GraphQL 等,以及各种身份验证和授权方式。HTTP 工具的封装可将这些细节抽象,使开发人员无需关心底层的协议和身份验证方式。

    代码重用:封装的 HTTP 工具使开发人员能够封装通用的 HTTP 请求和响应处理逻辑,以便在应用程序的不同部分重复使用,减少代码冗余,降低错误发生的风险。

    错误处理:封装工具可以集中处理错误情况,例如网络问题、请求失败或无效响应。这提供了一种一致的方法来处理错误,而不必在应用程序的各个部分编写相同的错误处理代码。

    监控和记录:通过封装,可以轻松记录请求和响应,用于调试和性能监控。这有助于跟踪应用程序与第三方服务的交互,以及识别潜在的性能瓶颈。

    安全性:封装工具可以提供内置的安全性功能,如身份验证和数据加密。这有助于确保与第三方接口的通信是安全的。

    版本控制:如果第三方接口升级或更改,封装工具可以在一处进行修改,而不必在整个应用程序中进行大规模更改。这提高了应用程序的可维护性。

    并发处理:封装的工具通常提供异步请求支持,允许应用程序同时发出多个请求,而不会阻塞主线程。这在高并发场景下非常重要。

    核心基类代码 httpClient

    package com.wukong.http.core;
    
    import okhttp3.RequestBody;
    import okhttp3.Response;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.Map;
    import java.util.concurrent.Future;
    
    /**
     * httpclient
     * @Author: Herche Jane
     */
    public interface HttpClient {
    
        /**
         * Synchronous GET request.
         *
         * @param url URL to send the GET request to.
         * @param headers Headers to include in the request.
         * @return Response from the request.
         * @throws IOException if an I/O error occurs.
         */
        Response get(String url, Map<String, String> headers) throws IOException;
    
        /**
         * Asynchronous GET request.
         *
         * @param url URL to send the GET request to.
         * @param headers Headers to include in the request.
         * @param callback Callback for handling the response.
         * @return A Future representing the ongoing request.
         */
        Future<Response> getAsync(String url, Map<String, String> headers, AsyncCallback<Response> callback);
    
        /**
         * Synchronous POST request.
         *
         * @param url URL to send the POST request to.
         * @param headers Headers to include in the request.
         * @param params Parameters to include in the request.
         * @param body Request body.
         * @return Response from the request.
         * @throws IOException if an I/O error occurs.
         */
        Response post(String url, Map<String, String> headers, Map<String, String> params, RequestBody body) throws IOException;
    
        /**
         * Asynchronous POST request.
         *
         * @param url URL to send the POST request to.
         * @param headers Headers to include in the request.
         * @param params Parameters to include in the request.
         * @param body Request body.
         * @param callback Callback for handling the response.
         * @return A Future representing the ongoing request.
         */
        Future<Response> postAsync(String url, Map<String, String> headers, Map<String, String> params, RequestBody body, AsyncCallback<Response> callback);
    
        /**
         * Upload a file.
         *
         * @param url URL to upload the file to.
         * @param headers Headers to include in the request.
         * @param file File to upload.
         * @param params Parameters to include in the request.
         * @return Response from the request.
         * @throws IOException if an I/O error occurs.
         */
        Response upload(String url, Map<String, String> headers, File file, Map<String, String> params) throws IOException;
    
        /**
         * Download a file.
         *
         * @param url URL to download the file from.
         * @param headers Headers to include in the request.
         * @param destination File to save the downloaded content to.
         * @param params Parameters to include in the request.
         * @return Response from the request.
         * @throws IOException if an I/O error occurs.
         */
        Response download(String url, Map<String, String> headers, File destination, Map<String, String> params) throws IOException;
    
        /**
         * Set authentication information.
         *
         * @param token Authentication token.
         */
        void setAuthentication(String token);
    
        /**
         * Task scheduling, supporting asynchronous task queues.
         *
         * @param task Runnable task to enqueue.a
         */
        void enqueueAsyncTask(Runnable task);
    
        /**
         * Enable caching support.
         *
         * @param cacheDirectory Directory for caching.
         * @param maxCacheSize Maximum cache size.
         */
        void enableCaching(File cacheDirectory, long maxCacheSize);
    
        /**
         * Cancel all requests.
         */
        void cancelAllRequests();
    
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114

    结束

    持续coding中

  • 相关阅读:
    创建型模式
    前端一个页面依赖多个接口解决之node接口聚合
    OceanBase 单机租户最多能支持多少分区?
    贪吃蛇项目实践
    2366. 将数组排序的最少替换次数
    【百度智能体】零代码创建职场高情商话术助手智能体
    Android-CAS与原子变量(无锁并发和有锁并发)
    PyQt5 基础知识(六):展示控件
    Centos7常用基本命令使用(帮助类、文件目录类)
    css横向滚动条支持鼠标滚轮
  • 原文地址:https://blog.csdn.net/weixin_45487988/article/details/133979455