// Copyright (c) 2021. Huawei Technologies Co., Ltd. All rights reserved.

// Package common this file for send internal http request
package common

import (
"encoding/json"
"io/ioutil"
"net/http"
"strings"
"time"
)

// HTTPHandler send inner request
type HTTPHandler struct {
Client *http.Client
}

// Get get
func (handler *HTTPHandler) Get(url, params string, header map[string]string,
timeOut time.Duration) (*ResultMsg, error) {
return handler.sendRequest(http.MethodGet, url, params, header, timeOut)
}

// Post post
func (handler *HTTPHandler) Post(url, params string, header map[string]string,
timeOut time.Duration) (*ResultMsg, error) {
return handler.sendRequest(http.MethodPost, url, params, header, timeOut)
}

// Delete delete
func (handler *HTTPHandler) Delete(url, params string, header map[string]string,
timeOut time.Duration) (*ResultMsg, error) {
return handler.sendRequest(http.MethodDelete, url, params, header, timeOut)
}

// SetHeader SetHeader
func (handler *HTTPHandler) SetHeader(req *http.Request, header map[string]string) {
if header == nil || req == nil {
return
}

for key, val := range header {
req.Header.Set(key, val)
}
}

// NewRequest NewRequest
func (handler *HTTPHandler) NewRequest(method, url, params string) (*http.Request, error) {
if params != "" {
return http.NewRequest(method, url, strings.NewReader(params))
}

return http.NewRequest(method, url, nil)
}

func (handler *HTTPHandler) sendRequest(method, url, params string, header map[string]string,
timeOut time.Duration) (*ResultMsg, error) {
req, err := handler.NewRequest(method, url, params)
if err != nil {
return nil, err
}

handler.SetHeader(req, header)

handler.Client.Timeout = timeOut
resp, err := handler.Client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

responseBody, err := ioutil.ReadAll(resp.Body)
responseData := ResultMsg{}
if err = json.Unmarshal(responseBody, &responseData); err != nil {
return nil, err
}

return &responseData, nil
}

mindxdl--common--http_handler.go的更多相关文章

  1. Socket聊天程序——Common

    写在前面: 上一篇记录了Socket聊天程序的客户端设计,为了记录的完整性,这里还是将Socket聊天的最后一个模块--Common模块记录一下.Common的设计如下: 功能说明: Common模块 ...

  2. angularjs 1 开发简单案例(包含common.js,service.js,controller.js,page)

    common.js var app = angular.module('app', ['ngFileUpload']) .factory('SV_Common', function ($http) { ...

  3. Common Bugs in C Programming

    There are some Common Bugs in C Programming. Most of the contents are directly from or modified from ...

  4. ANSI Common Lisp Practice - My Answers - Chatper - 3

    Ok, Go ahead. 1 (a) (b) (c) (d) 2 注:union 在 Common Lisp 中的作用就是求两个集合的并集.但是这有一个前提,即给的两个列表已经满足集合的属性了.具体 ...

  5. [LeetCode] Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

  6. [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  7. [LeetCode] Longest Common Prefix 最长共同前缀

    Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...

  8. 48. 二叉树两结点的最低共同父结点(3种变种情况)[Get lowest common ancestor of binary tree]

    [题目] 输入二叉树中的两个结点,输出这两个结点在数中最低的共同父结点. 二叉树的结点定义如下:  C++ Code  123456   struct BinaryTreeNode {     int ...

  9. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  10. 【leetcode】Longest Common Prefix

    题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...

随机推荐

  1. Linux有趣命令

    通外网 下载使用阿里云镜像源:wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.re ...

  2. Keepalived之简单有效的配置

    1.简介 官网地址:https://www.keepalived.org/ 源码包下载地址:https://www.keepalived.org/download.html Keepalived是一种 ...

  3. Elasticsearch:Index生命周期管理入门

    如果您要处理时间序列数据,则不想将所有内容连续转储到单个索引中. 取而代之的是,您可以定期将数据滚动到新索引,以防止数据过大而又缓慢又昂贵. 随着索引的老化和查询频率的降低,您可能会将其转移到价格较低 ...

  4. 如何从Django项目中删除或隐藏应用

    1.项目的settings.py文件 INSTALLED_APPS中删除或者注释掉,这是针对数据库这一块儿的 2.项目的urls.py文件 删除或这注释掉应用的路径导入 urlpatterns中删除或 ...

  5. Kubernetes(k8s)通过环境变量将 Pod 信息呈现给容器

    Downward API 有两种方式可以将 Pod 和 Container 字段呈现给运行中的容器: 环境变量 卷文件 这两种呈现 Pod 和 Container 字段的方式统称为 Downward ...

  6. 在Portainer上管理其他docker主机(这只是其中一种方式),另一种方式看这个文档:使用Portainer管理其他主机的docker应用有两种方式

    其他主机开启远程连接docker端口 需要设置一下2375端口的监听.通过修改docker配置文件方式进行监听. 修改配置文件修改监听端口 使用Centos7安装的docker,所以下面的配置是适用于 ...

  7. js对象结构赋值const {XXX } =this

    样例1: const { xxx } = this.state; 上面的写法是es6的写法,其实就相当于: const xxx = this.state.xxx 样例2: const {comment ...

  8. PAT (Basic Level) Practice 1006 换个格式输出整数 分数 15

    让我们用字母 B 来表示"百".字母 S 表示"十",用 12...n 来表示不为零的个位数字 n(<10),换个格式来输出任一个不超过 3 位的正整数. ...

  9. java的分页原理详解

    首先先创建一个Student实体类. import java.io.Serializable; import java.util.Map; public class Student implement ...

  10. 工厂方法在Spring源码中的运用

    我们都知道Spring中IOC是使用的工厂模式,但是对于实现细节就一知半解了,今天这篇文章就带大家解读Spring中是如何使用工厂模式的. 在上篇文章中我们懂了什么是工厂模式,这篇文章就带着学过的概念 ...