mindxdl--common--http_handler.go
// 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的更多相关文章
- Socket聊天程序——Common
写在前面: 上一篇记录了Socket聊天程序的客户端设计,为了记录的完整性,这里还是将Socket聊天的最后一个模块--Common模块记录一下.Common的设计如下: 功能说明: Common模块 ...
- angularjs 1 开发简单案例(包含common.js,service.js,controller.js,page)
common.js var app = angular.module('app', ['ngFileUpload']) .factory('SV_Common', function ($http) { ...
- Common Bugs in C Programming
There are some Common Bugs in C Programming. Most of the contents are directly from or modified from ...
- ANSI Common Lisp Practice - My Answers - Chatper - 3
Ok, Go ahead. 1 (a) (b) (c) (d) 2 注:union 在 Common Lisp 中的作用就是求两个集合的并集.但是这有一个前提,即给的两个列表已经满足集合的属性了.具体 ...
- [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 ...
- [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 ...
- [LeetCode] Longest Common Prefix 最长共同前缀
Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...
- 48. 二叉树两结点的最低共同父结点(3种变种情况)[Get lowest common ancestor of binary tree]
[题目] 输入二叉树中的两个结点,输出这两个结点在数中最低的共同父结点. 二叉树的结点定义如下: C++ Code 123456 struct BinaryTreeNode { int ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- 【leetcode】Longest Common Prefix
题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...
随机推荐
- 典型C内存空间分布图
下图是一个典型的C内存空间分布图 这是Linux下32位环境的用户空间内存分布情况 内核空间 :一部分核心软件独立于普通应用程序,运行在较高的特权级别上,驻留在被保护的内存空间上,拥有访问硬件设备的所 ...
- STL再回顾(非常见知识点)
目录 为人熟知的pair类型 再谈STL 迭代器的使用 常用的STL容器 顺序容器 vector(向量) 构造方式 拥有的常用的成员函数(java人称方法) string 构造方式 成员函数 dequ ...
- Windows优先使用IPv4
当前主流的Windows系统(从Windows 7之后)都会同时使用ipv6和ipv4,并且优先使用ipv6.当你ping另一个服务器的时候就能看到,优先使用的是ipv6进行通信.由于能够在DNS中解 ...
- 2021年3月-第01阶段-Linux基础-Linux系统的启动流程
Linux系统的启动流程 理解Linux操作系统启动流程,能有助于后期在企业中更好的维护Linux服务器,能快速定位系统问题,进而解决问题. 上图为Linux操作系统启动流程 1.加载BIOS 计算机 ...
- Python数据科学手册-机器学习之特征工程
特征工程常见示例: 分类数据.文本.图像. 还有提高模型复杂度的 衍生特征 和 处理 缺失数据的填充 方法.这个过程被叫做向量化.把任意格式的数据 转换成具有良好特性的向量形式. 分类特征 比如房屋数 ...
- 实践torch.fx第二篇-fx量化实操
好久不见各位,哈哈,又鸽了好久. 本文紧接上一篇<实践torch.fx第一篇--基于Pytorch的模型优化量化神器>继续说,主要讲如何利用FX进行模型量化. 为什么这篇文章拖了这么久,有 ...
- 基于 Apache Hudi + Presto + AWS S3 构建开放Lakehouse
认识Lakehouse 数据仓库被认为是对结构化数据执行分析的标准,但它不能处理非结构化数据. 包括诸如文本.图像.音频.视频和其他格式的信息. 此外机器学习和人工智能在业务的各个方面变得越来越普遍, ...
- kube-scheduler 调度调优
文章转载自:https://www.kuboard.cn/learning/k8s-advanced/schedule/tuning.html kube-scheduler 是 Kubernetes ...
- Pod的滚动升级过程
- firewalld防火墙基本使用
FirewallD 是 CentOS 7 服务器上默认可用的防火墙管理工具.基本上,它是 iptables 的封装,有图形配置工具 firewall-config 和命令行工具 firewall-cm ...