github.com/altipla-consulting/ravendb-go-client@v0.1.3/get_index_operation.go (about)

     1  package ravendb
     2  
     3  import (
     4  	"net/http"
     5  )
     6  
     7  var _ IMaintenanceOperation = &GetIndexOperation{}
     8  
     9  type GetIndexOperation struct {
    10  	_indexName string
    11  
    12  	Command *GetIndexCommand
    13  }
    14  
    15  func NewGetIndexOperation(indexName string) *GetIndexOperation {
    16  	panicIf(indexName == "", "Index name connot be empty")
    17  	return &GetIndexOperation{
    18  		_indexName: indexName,
    19  	}
    20  }
    21  
    22  func (o *GetIndexOperation) GetCommand(conventions *DocumentConventions) (RavenCommand, error) {
    23  	var err error
    24  	o.Command, err = NewGetIndexCommand(o._indexName)
    25  	if err != nil {
    26  		return nil, err
    27  	}
    28  	return o.Command, nil
    29  }
    30  
    31  var (
    32  	_ RavenCommand = &GetIndexCommand{}
    33  )
    34  
    35  type GetIndexCommand struct {
    36  	RavenCommandBase
    37  
    38  	_indexName string
    39  
    40  	Result *IndexDefinition
    41  }
    42  
    43  func NewGetIndexCommand(indexName string) (*GetIndexCommand, error) {
    44  	if indexName == "" {
    45  		return nil, newIllegalArgumentError("Index name connot be empty")
    46  	}
    47  
    48  	res := &GetIndexCommand{
    49  		RavenCommandBase: NewRavenCommandBase(),
    50  
    51  		_indexName: indexName,
    52  	}
    53  	res.IsReadRequest = true
    54  	return res, nil
    55  }
    56  
    57  func (c *GetIndexCommand) createRequest(node *ServerNode) (*http.Request, error) {
    58  	url := node.URL + "/databases/" + node.Database + "/indexes?name=" + urlUtilsEscapeDataString(c._indexName)
    59  
    60  	return newHttpGet(url)
    61  }
    62  
    63  func (c *GetIndexCommand) setResponse(response []byte, fromCache bool) error {
    64  	if response == nil {
    65  		return throwInvalidResponse()
    66  	}
    67  
    68  	var res struct {
    69  		Results []*IndexDefinition `json:"Results"`
    70  	}
    71  
    72  	err := jsonUnmarshal(response, &res)
    73  	if err != nil {
    74  		return err
    75  	}
    76  	if len(res.Results) == 0 {
    77  		return throwInvalidResponse()
    78  	}
    79  	c.Result = res.Results[0]
    80  	return nil
    81  }