gitee.com/larksuite/oapi-sdk-go/v3@v3.0.3/core/apiresp.go (about) 1 /* 2 * MIT License 3 * 4 * Copyright (c) 2022 Lark Technologies Pte. Ltd. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 7 * 8 * The above copyright notice and this permission notice, shall be included in all copies or substantial portions of the Software. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 11 */ 12 13 package larkcore 14 15 import ( 16 "encoding/json" 17 "fmt" 18 "mime" 19 "net/http" 20 "strconv" 21 "strings" 22 ) 23 24 type ApiResp struct { 25 StatusCode int `json:"-"` 26 Header http.Header `json:"-"` 27 RawBody []byte `json:"-"` 28 } 29 30 func (resp ApiResp) Write(writer http.ResponseWriter) { 31 writer.WriteHeader(resp.StatusCode) 32 for k, vs := range resp.Header { 33 for _, v := range vs { 34 writer.Header().Add(k, v) 35 } 36 } 37 if _, err := writer.Write(resp.RawBody); err != nil { 38 panic(err) 39 } 40 } 41 42 func (resp ApiResp) JSONUnmarshalBody(val interface{}) error { 43 if !strings.Contains(resp.Header.Get(contentTypeHeader), contentTypeJson) { 44 return fmt.Errorf("response content-type not json, response: %v", resp) 45 } 46 return json.Unmarshal(resp.RawBody, val) 47 } 48 49 func (resp ApiResp) RequestId() string { 50 logID := resp.Header.Get(HttpHeaderKeyLogId) 51 if logID != "" { 52 return logID 53 } 54 return resp.Header.Get(HttpHeaderKeyRequestId) 55 } 56 57 func (resp ApiResp) String() string { 58 contentType := resp.Header.Get(contentTypeHeader) 59 body := fmt.Sprintf("<binary> len %d", len(resp.RawBody)) 60 if strings.Contains(contentType, "json") || strings.Contains(contentType, "text") { 61 body = string(resp.RawBody) 62 } 63 return fmt.Sprintf("StatusCode: %d, Header:%v, Content-Type: %s, Body: %v", resp.StatusCode, 64 resp.Header, resp.Header.Get(contentTypeHeader), body) 65 } 66 67 type CodeError struct { 68 Code int `json:"code"` 69 Msg string `json:"msg"` 70 Err *struct { 71 Details []*CodeErrorDetail `json:"details,omitempty"` 72 PermissionViolations []*CodeErrorPermissionViolation `json:"permission_violations,omitempty"` 73 FieldViolations []*CodeErrorFieldViolation `json:"field_violations,omitempty"` 74 } `json:"error"` 75 } 76 77 func (ce CodeError) Error() string { 78 return ce.String() 79 } 80 81 func (ce CodeError) String() string { 82 sb := strings.Builder{} 83 sb.WriteString("msg:") 84 sb.WriteString(ce.Msg) 85 sb.WriteString(",code:") 86 sb.WriteString(strconv.Itoa(ce.Code)) 87 return sb.String() 88 } 89 90 type CodeErrorDetail struct { 91 Key string `json:"key,omitempty"` 92 Value string `json:"value,omitempty"` 93 } 94 95 type CodeErrorPermissionViolation struct { 96 Type string `json:"type,omitempty"` 97 Subject string `json:"subject,omitempty"` 98 Description string `json:"description,omitempty"` 99 } 100 101 type CodeErrorFieldViolation struct { 102 Field string `json:"field,omitempty"` 103 Value string `json:"value,omitempty"` 104 Description string `json:"description,omitempty"` 105 } 106 107 func FileNameByHeader(header http.Header) string { 108 filename := "" 109 _, media, _ := mime.ParseMediaType(header.Get("Content-Disposition")) 110 if len(media) > 0 { 111 filename = media["filename"] 112 } 113 return filename 114 }