// 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. torch.max与torch.argmax

    形式: torch.max(input) → Tensor 返回输入tensor中所有元素的最大值: a = torch.randn(1, 3) >>0.4729 -0.2266 -0.2 ...

  2. Spring配置XML本地提示

    Spring配置XML本地提示:点击eclipse属性-->选择XML Catalog 这里有一点要注意:要选择schema location

  3. 【java8新特性】02:常见的函数式接口

    Jdk8提供的函数式接口都在java.util.function包下,Jdk8的函数式类型的接口都有@FunctionInterface注解所标注,但实际上即使没有该注解标注的有且只有一个抽象方法的接 ...

  4. jmeter性能测试之正则提取响应头或者响应体

    准备工作做好,先发送请求 然后察看结果树中的响应消息 比如我们要提取这个cookie,先调试一下,看能不能提取到 看蓝色的线条,我们提取到了,然后我们把这句话写入到后置处理器中的正则表达式提取里 再次 ...

  5. Kubernetes(K8S)特性有哪些?

    Kubernetes简介 Kubernetes是一个开源的,用于管理云平台中做个主机上的容器化的应用,Kubernetes的目标是让部署容器化的应用简单且高效,Kubernetes提供了应用部署,规划 ...

  6. CentOS7 系统服务器初始化配置、安全加固、内核升级优化常用软件安装的Shell脚本分享

    转载自:https://www.bilibili.com/read/cv13875630?spm_id_from=333.999.0.0 描述: 适用于企业内部 CentOS7 系列操作服务器初始化. ...

  7. python csv写入多列

    import csv import os def main(): current_dir = os.path.abspath('.') file_name = os.path.join(current ...

  8. AlertManager 之微信告警模板,UTC时间错8个小时的解决办法

    注意事项: alertmanager中的web页面显示的报警时间是UTC时间,错8个小时,企业微信报警模板中已经修改过来了 下面配置可以作为参考: 1.prometheus操作 1.1 配置告警规则, ...

  9. PAT (Basic Level) Practice 1027 打印沙漏 分数 20

    本题要求你写个程序把给定的符号打印成沙漏的形状.例如给定17个"*",要求按下列格式打印 ***** *** * *** *****   所谓"沙漏形状",是指 ...

  10. 第一周python作业

    print("hello world") height=float(input("请输入你的身高:")) weight=float(input("请输 ...