github.com/hoffie/larasync@v0.0.0-20151025221940-0384d2bddcef/api/client/repositories.go (about)

     1  package client
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"net/http"
     7  
     8  	"github.com/hoffie/larasync/api"
     9  	"github.com/hoffie/larasync/api/common"
    10  )
    11  
    12  // registerRequest builds a request for registering a new repository
    13  func (c *Client) registerRequest(pubKey [PublicKeySize]byte) (*http.Request, error) {
    14  	if len(c.adminSecret) == 0 {
    15  		return nil, ErrMissingAdminSecret
    16  	}
    17  	body, err := json.Marshal(api.JSONRepository{
    18  		PubKey: pubKey[:],
    19  	})
    20  	if err != nil {
    21  		return nil, err
    22  	}
    23  	req, err := http.NewRequest("PUT", c.BaseURL, bytes.NewReader(body))
    24  	if err != nil {
    25  		return nil, err
    26  	}
    27  	req.Header.Set("Content-Type", "application/json")
    28  	common.SignWithPassphrase(req, c.adminSecret)
    29  	return req, nil
    30  }
    31  
    32  // Register registers the current repository name with the server for the
    33  // first time.
    34  func (c *Client) Register(pubKey [PublicKeySize]byte) error {
    35  	req, err := c.registerRequest(pubKey)
    36  	if err != nil {
    37  		return err
    38  	}
    39  	_, err = c.doRequest(req, http.StatusCreated)
    40  	if err != nil {
    41  		return err
    42  	}
    43  	return nil
    44  }