sqlite的api,便于内部使用
liuxiaolong
2019-05-09 daf7c4ec7270af5fc0b87eeaa3da1909d299be70
common get and post
4个文件已添加
185 ■■■■■ 已修改文件
areaApi.go 46 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
cameraApi.go 28 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
common.go 107 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
taskApi.go 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
areaApi.go
New file
@@ -0,0 +1,46 @@
package dbapi
import (
    "encoding/json"
    "fmt"
    "net/http"
    "strings"
)
type AreaApi struct {
}
//查询本地摄像机树
func (api AreaApi) GetLocalCameraTree(parentId int) []TreeMenu {
    var result []TreeMenu
    url := BASIC_URL + "/data/api-v/area/localmenu"
    request, err := http.NewRequest("GET", url, strings.NewReader(string(parentId)))
    if err != nil {
        return result
    }
    body, err := DoRequest(request, "application/json")
    if err != nil {
        return result
    }
    var res Result
    if err = json.Unmarshal(body, &res); err != nil {
        fmt.Println(err)
    }
    bytes, _ := json.Marshal(res.Data)
    err = json.Unmarshal(bytes, &result)
    if err != nil {
        fmt.Println(err)
    }
    return result
}
//添加目录节点
func (api AreaApi) AreaAdd(area Area) bool {
    flag := false
    return flag
}
cameraApi.go
New file
@@ -0,0 +1,28 @@
package dbapi
import (
    "encoding/json"
    "fmt"
)
type CameraApi struct{}
//通过cameraId获取摄像机信息
func (api CameraApi) GetCameraById(cameraId string) (result Camera, err error) {
    url := BASIC_URL + "/data/api-v/camera/show/" + cameraId
    body, err := DoGetRequest(url, nil, nil)
    if err != nil {
        return result, err
    }
    var res Result
    if err = json.Unmarshal(body, &res); err != nil {
        fmt.Println(err)
    }
    bytes, _ := json.Marshal(res.Data)
    err = json.Unmarshal(bytes, &result)
    return result, err
}
common.go
New file
@@ -0,0 +1,107 @@
package dbapi
import (
    "bytes"
    "encoding/json"
    "io/ioutil"
    "log"
    "net/http"
    "time"
)
const (
    BASIC_URL = "http://192.168.1.182:8000"
)
//发起GET请求
func DoGetRequest(url string, params map[string]string, headers map[string]string) ([]byte, error) {
    var body []byte
    request, err := http.NewRequest("GET", url, nil)
    if err != nil {
        log.Println(err)
        return body, err
    }
    // add params
    query := request.URL.Query()
    if params != nil {
        for key, val := range params {
            query.Add(key, val)
        }
        request.URL.RawQuery = query.Encode()
    }
    //add headers
    client := &http.Client{}
    log.Printf("Go GET URL : %s \n", request.URL.String())
    resp, err := client.Do(request)
    if err != nil {
        return body, err
    }
    defer resp.Body.Close()
    body, err = ioutil.ReadAll(resp.Body)
    if err != nil {
        return body, err
    }
    return body, nil
}
//发起POST请求
func DoPostRequest(url string, body map[string]string, params map[string]string, headers map[string]string) ([]byte, error) {
    var resultByte []byte
    var bodyJson []byte
    if body != nil {
        var err error
        bodyJson, err = json.Marshal(body)
        if err != nil {
            log.Println(err)
            return resultByte, err
        }
    }
    request, err := http.NewRequest("POST", url, bytes.NewBuffer(bodyJson))
    if err != nil {
        log.Println(err)
        return resultByte, err
    }
    request.Header.Set("Content-type", "application/json")
    //add params
    q := request.URL.Query()
    if params != nil {
        for key, val := range params {
            q.Add(key, val)
        }
        request.URL.RawQuery = q.Encode()
    }
    // add headers
    if headers != nil {
        for key, val := range headers {
            request.Header.Add(key, val)
        }
    }
    client := &http.Client{}
    resp, err := client.Do(request)
    if err != nil {
        return resultByte, err
    }
    defer resp.Body.Close()
    resultByte, err = ioutil.ReadAll(resp.Body)
    if err != nil {
        return resultByte, err
    }
    return resultByte, nil
}
func DoRequest(request *http.Request, contentType string) ([]byte, error) {
    var body []byte
    request.Header.Add("Content-Type", contentType)
    timeout := time.Duration(5 * time.Second) //超时时间50ms
    client := &http.Client{Timeout: timeout}
    resp, err := client.Do(request)
    if err != nil {
        return body, err
    }
    defer resp.Body.Close()
    body, err = ioutil.ReadAll(resp.Body)
    if err != nil {
        return body, err
    }
    return body, nil
}
taskApi.go
New file
@@ -0,0 +1,4 @@
package dbapi
type TaskApi struct {}