github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/system/scripts/bind/http.go (about) 1 // This file is part of the Smart Home 2 // Program complex distribution https://github.com/e154/smart-home 3 // Copyright (C) 2016-2023, Filippov Alex 4 // 5 // This library is free software: you can redistribute it and/or 6 // modify it under the terms of the GNU Lesser General Public 7 // License as published by the Free Software Foundation; either 8 // version 3 of the License, or (at your option) any later version. 9 // 10 // This library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 // Library General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public 16 // License along with this library. If not, see 17 // <https://www.gnu.org/licenses/>. 18 19 package bind 20 21 import ( 22 "github.com/e154/smart-home/common/web" 23 ) 24 25 // HttpResponse ... 26 type HttpResponse struct { 27 Body string `json:"body"` 28 Error bool `json:"error"` 29 ErrorMessage string `json:"errorMessage"` 30 } 31 32 // HttpBind ... 33 type HttpBind struct { 34 crawler web.Crawler 35 headers []map[string]string 36 } 37 38 func NewHttpBind() *HttpBind { 39 return &HttpBind{crawler: web.New()} 40 } 41 42 // Get ... 43 func (h *HttpBind) Get(url string) (response HttpResponse) { 44 //log.Infof("call [GET ] request %s", url) 45 _, body, err := h.crawler.Probe(web.Request{Method: "GET", Url: url, Headers: h.headers}) 46 if err != nil { 47 response.Error = true 48 response.ErrorMessage = err.Error() 49 return 50 } 51 response.Body = string(body) 52 return 53 } 54 55 // Post ... 56 func (h *HttpBind) Post(url, data string) (response HttpResponse) { 57 //log.Infof("call [POST] request %s", url) 58 _, body, err := h.crawler.Probe(web.Request{Method: "POST", Url: url, Headers: h.headers, Body: []byte(data)}) 59 if err != nil { 60 response.Error = true 61 response.ErrorMessage = err.Error() 62 return 63 } 64 response.Body = string(body) 65 return 66 } 67 68 // Put ... 69 func (h *HttpBind) Put(url, data string) (response HttpResponse) { 70 //log.Infof("call [PUT] request %s", url) 71 _, body, err := h.crawler.Probe(web.Request{Method: "PUT", Url: url, Headers: h.headers, Body: []byte(data)}) 72 if err != nil { 73 response.Error = true 74 response.ErrorMessage = err.Error() 75 return 76 } 77 response.Body = string(body) 78 return 79 } 80 81 // Delete ... 82 func (h *HttpBind) Delete(url string) (response HttpResponse) { 83 //log.Infof("call [DELETE] request %s", url) 84 _, body, err := h.crawler.Probe(web.Request{Method: "DELETE", Url: url, Headers: h.headers}) 85 if err != nil { 86 response.Error = true 87 response.ErrorMessage = err.Error() 88 return 89 } 90 response.Body = string(body) 91 return 92 } 93 94 func (h *HttpBind) Headers(headers []map[string]string) *HttpBind { 95 return &HttpBind{ 96 crawler: h.crawler, 97 headers: headers, 98 } 99 } 100 101 func (h *HttpBind) BasicAuth(username, password string) *HttpBind { 102 return &HttpBind{crawler: web.New().BasicAuth(username, password)} 103 } 104 105 func (h *HttpBind) DigestAuth(username, password string) *HttpBind { 106 return &HttpBind{crawler: web.New().DigestAuth(username, password)} 107 } 108 109 func (h *HttpBind) Download(uri string) (response HttpResponse) { 110 filePath, err := h.crawler.Download(web.Request{Method: "GET", Url: uri}) 111 if err != nil { 112 response.Error = true 113 response.ErrorMessage = err.Error() 114 return 115 } 116 response.Body = filePath 117 return 118 }