Debian Codegraph @

lfshttp/lfshttp.go

raw symbols SCIP
1 package lfshttp
2
3 import (
4 "encoding/json"
5 "net/http"
6 "regexp"
7
8 "github.com/git-lfs/git-lfs/v3/config"
9 "github.com/git-lfs/git-lfs/v3/errors"
10 "github.com/git-lfs/git-lfs/v3/git"
11 "github.com/git-lfs/git-lfs/v3/tr"
12 )
13
14 var (
15 lfsMediaTypeRE = regexp.MustCompile(`\Aapplication/vnd\.git\-lfs\+json(;|\z)`)
16 jsonMediaTypeRE = regexp.MustCompile(`\Aapplication/json(;|\z)`)
17 )
18
19 type Context interface {
20 GitConfig() *git.Configuration
21 OSEnv() config.Environment
22 GitEnv() config.Environment
23 }
24
25 func NewContext(gitConf *git.Configuration, osEnv, gitEnv map[string]string) Context {
26 c := &testContext{gitConfig: gitConf}
27 if c.gitConfig == nil {
28 c.gitConfig = git.NewConfig("", "")
29 }
30 if osEnv != nil {
31 c.osEnv = testEnv(osEnv)
32 } else {
33 c.osEnv = make(testEnv)
34 }
35
36 if gitEnv != nil {
37 c.gitEnv = testEnv(gitEnv)
38 } else {
39 c.gitEnv = make(testEnv)
40 }
41 return c
42 }
43
44 type testContext struct {
45 gitConfig *git.Configuration
46 osEnv config.Environment
47 gitEnv config.Environment
48 }
49
50 func (c *testContext) GitConfig() *git.Configuration {
51 return c.gitConfig
52 }
53
54 func (c *testContext) OSEnv() config.Environment {
55 return c.osEnv
56 }
57
58 func (c *testContext) GitEnv() config.Environment {
59 return c.gitEnv
60 }
61
62 func IsDecodeTypeError(err error) bool {
63 _, ok := err.(*decodeTypeError)
64 return ok
65 }
66
67 type decodeTypeError struct {
68 Type string
69 }
70
71 func (e *decodeTypeError) TypeError() {}
72
73 func (e *decodeTypeError) Error() string {
74 return tr.Tr.Get("Expected JSON type, got: %q", e.Type)
75 }
76
77 func DecodeJSON(res *http.Response, obj interface{}) error {
78 ctype := res.Header.Get("Content-Type")
79 if !(lfsMediaTypeRE.MatchString(ctype) || jsonMediaTypeRE.MatchString(ctype)) {
80 return &decodeTypeError{Type: ctype}
81 }
82
83 err := json.NewDecoder(res.Body).Decode(obj)
84 res.Body.Close()
85
86 if err != nil {
87 return errors.Wrap(err, tr.Tr.Get("Unable to parse HTTP response for %s %s", res.Request.Method, res.Request.URL))
88 }
89
90 return nil
91 }