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 ...
随机推荐
- 小技巧:webpack中@的配置和用法
好家伙, 当我们要各种两个文件去引用别的文件时,一般这么写 import msg from '../../msg.js' 那么如果文件藏得很深,'../'会变得很多,不美观,也不直观 所以我们又又又可 ...
- 第九章 kubectl命令行工具使用详解
1.管理k8s核心资源的三种基础方法 陈述式管理方法:主要依赖命令行CLI工具进行管理 声明式管理方法:主要依赖统一资源配置清单(manifest)进行管理 GUI式管理方法:主要依赖图形化操作界面( ...
- 基于koa模块和socket.io模块搭建的node服务器实现通过jwt 验证来渲染列表、私聊、群聊功能
1. 具体代码在需要的下载 https://gitee.com/zyqwasd/socket 效果: 2. package.json文件 1. 下载基本的模块 修改了start 脚本 nodemo ...
- 聊聊计算机之Intel CPU的MESI协议
1.on-chip概念 on-chip:每个CPU有好几个物理核,它们分布在CPU上,称为on-chip on-chip first cache:每个核内的一级缓存 on chip branch ta ...
- ProxySQL查看所有的全局变量及更新操作
mysql> select * from global_variables; +--------------------------------------------------------- ...
- logstash另类输出到es
filebeat配置文件: filebeat.inputs: - type: log enabled: true paths: - /opt/hkd-cloud/hkd-custom/hkd-cust ...
- 【Ceph】Ceph学习理解Ceph的三种存储接口:块设备、文件系统、对象存储
文章转载自:https://blog.51cto.com/liangchaoxi/4048519
- k8s中节点级别的日志
容器化应用程序写入到 stdout 和 stderr 中的任何信息,都将被容器引擎重定向到某个地方.例如,Docker 容器引擎将 stdout 和 stderr 这两个输出流重定向到 logging ...
- 浏览器的 JavaScript 控制台功能调试vue
原始显示结果: 调试其中一个变量的值: 页面上呈现出调试后的效果了
- Python——索引与切片
#索引与切片 ##1.序列 序列:list,tuple,str 其中list是可变序列 typle,str是不可变序列 #修改序列的值 list = [3,4,5] tup = (3,4,5) str ...