...

Source file src/github.com/google/go-github/v47/github/users_keys.go

Documentation: github.com/google/go-github/v47/github

     1  // Copyright 2013 The go-github AUTHORS. All rights reserved.
     2  //
     3  // Use of this source code is governed by a BSD-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package github
     7  
     8  import (
     9  	"context"
    10  	"fmt"
    11  )
    12  
    13  // Key represents a public SSH key used to authenticate a user or deploy script.
    14  type Key struct {
    15  	ID        *int64     `json:"id,omitempty"`
    16  	Key       *string    `json:"key,omitempty"`
    17  	URL       *string    `json:"url,omitempty"`
    18  	Title     *string    `json:"title,omitempty"`
    19  	ReadOnly  *bool      `json:"read_only,omitempty"`
    20  	Verified  *bool      `json:"verified,omitempty"`
    21  	CreatedAt *Timestamp `json:"created_at,omitempty"`
    22  }
    23  
    24  func (k Key) String() string {
    25  	return Stringify(k)
    26  }
    27  
    28  // ListKeys lists the verified public keys for a user. Passing the empty
    29  // string will fetch keys for the authenticated user.
    30  //
    31  // GitHub API docs: https://docs.github.com/en/rest/users/keys#list-public-ssh-keys-for-the-authenticated-user
    32  // GitHub API docs: https://docs.github.com/en/rest/users/keys#list-public-keys-for-a-user
    33  func (s *UsersService) ListKeys(ctx context.Context, user string, opts *ListOptions) ([]*Key, *Response, error) {
    34  	var u string
    35  	if user != "" {
    36  		u = fmt.Sprintf("users/%v/keys", user)
    37  	} else {
    38  		u = "user/keys"
    39  	}
    40  	u, err := addOptions(u, opts)
    41  	if err != nil {
    42  		return nil, nil, err
    43  	}
    44  
    45  	req, err := s.client.NewRequest("GET", u, nil)
    46  	if err != nil {
    47  		return nil, nil, err
    48  	}
    49  
    50  	var keys []*Key
    51  	resp, err := s.client.Do(ctx, req, &keys)
    52  	if err != nil {
    53  		return nil, resp, err
    54  	}
    55  
    56  	return keys, resp, nil
    57  }
    58  
    59  // GetKey fetches a single public key.
    60  //
    61  // GitHub API docs: https://docs.github.com/en/rest/users/keys#get-a-public-ssh-key-for-the-authenticated-user
    62  func (s *UsersService) GetKey(ctx context.Context, id int64) (*Key, *Response, error) {
    63  	u := fmt.Sprintf("user/keys/%v", id)
    64  
    65  	req, err := s.client.NewRequest("GET", u, nil)
    66  	if err != nil {
    67  		return nil, nil, err
    68  	}
    69  
    70  	key := new(Key)
    71  	resp, err := s.client.Do(ctx, req, key)
    72  	if err != nil {
    73  		return nil, resp, err
    74  	}
    75  
    76  	return key, resp, nil
    77  }
    78  
    79  // CreateKey adds a public key for the authenticated user.
    80  //
    81  // GitHub API docs: https://docs.github.com/en/rest/users/keys#create-a-public-ssh-key-for-the-authenticated-user
    82  func (s *UsersService) CreateKey(ctx context.Context, key *Key) (*Key, *Response, error) {
    83  	u := "user/keys"
    84  
    85  	req, err := s.client.NewRequest("POST", u, key)
    86  	if err != nil {
    87  		return nil, nil, err
    88  	}
    89  
    90  	k := new(Key)
    91  	resp, err := s.client.Do(ctx, req, k)
    92  	if err != nil {
    93  		return nil, resp, err
    94  	}
    95  
    96  	return k, resp, nil
    97  }
    98  
    99  // DeleteKey deletes a public key.
   100  //
   101  // GitHub API docs: https://docs.github.com/en/rest/users/keys#delete-a-public-ssh-key-for-the-authenticated-user
   102  func (s *UsersService) DeleteKey(ctx context.Context, id int64) (*Response, error) {
   103  	u := fmt.Sprintf("user/keys/%v", id)
   104  
   105  	req, err := s.client.NewRequest("DELETE", u, nil)
   106  	if err != nil {
   107  		return nil, err
   108  	}
   109  
   110  	return s.client.Do(ctx, req, nil)
   111  }
   112  

View as plain text