github.com/secman-team/gh-api@v1.8.2/pkg/cmd/ssh-key/list/http.go (about) 1 package list 2 3 import ( 4 "encoding/json" 5 "errors" 6 "fmt" 7 "io/ioutil" 8 "net/http" 9 "time" 10 11 "github.com/secman-team/gh-api/api" 12 "github.com/secman-team/gh-api/core/ghinstance" 13 ) 14 15 var scopesError = errors.New("insufficient OAuth scopes") 16 17 type sshKey struct { 18 Key string 19 Title string 20 CreatedAt time.Time `json:"created_at"` 21 } 22 23 func userKeys(httpClient *http.Client, host, userHandle string) ([]sshKey, error) { 24 resource := "user/keys" 25 if userHandle != "" { 26 resource = fmt.Sprintf("users/%s/keys", userHandle) 27 } 28 url := fmt.Sprintf("%s%s?per_page=%d", ghinstance.RESTPrefix(host), resource, 100) 29 req, err := http.NewRequest("GET", url, nil) 30 if err != nil { 31 return nil, err 32 } 33 34 resp, err := httpClient.Do(req) 35 if err != nil { 36 return nil, err 37 } 38 defer resp.Body.Close() 39 40 if resp.StatusCode == 404 { 41 return nil, scopesError 42 } else if resp.StatusCode > 299 { 43 return nil, api.HandleHTTPError(resp) 44 } 45 46 b, err := ioutil.ReadAll(resp.Body) 47 if err != nil { 48 return nil, err 49 } 50 51 var keys []sshKey 52 err = json.Unmarshal(b, &keys) 53 if err != nil { 54 return nil, err 55 } 56 57 return keys, nil 58 }