github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/file_contents_query.go (about)

     1  package hedera
     2  
     3  /*-
     4   *
     5   * Hedera Go SDK
     6   *
     7   * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC
     8   *
     9   * Licensed under the Apache License, Version 2.0 (the "License");
    10   * you may not use this file except in compliance with the License.
    11   * You may obtain a copy of the License at
    12   *
    13   *      http://www.apache.org/licenses/LICENSE-2.0
    14   *
    15   * Unless required by applicable law or agreed to in writing, software
    16   * distributed under the License is distributed on an "AS IS" BASIS,
    17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    18   * See the License for the specific language governing permissions and
    19   * limitations under the License.
    20   *
    21   */
    22  
    23  import (
    24  	"time"
    25  
    26  	"github.com/hashgraph/hedera-protobufs-go/services"
    27  )
    28  
    29  // FileContentsQuery retrieves the contents of a file.
    30  type FileContentsQuery struct {
    31  	Query
    32  	fileID *FileID
    33  }
    34  
    35  // NewFileContentsQuery creates a FileContentsQuery which retrieves the contents of a file.
    36  func NewFileContentsQuery() *FileContentsQuery {
    37  	header := services.QueryHeader{}
    38  	return &FileContentsQuery{
    39  		Query: _NewQuery(true, &header),
    40  	}
    41  }
    42  
    43  // When execution is attempted, a single attempt will timeout when this deadline is reached. (The SDK may subsequently retry the execution.)
    44  func (q *FileContentsQuery) SetGrpcDeadline(deadline *time.Duration) *FileContentsQuery {
    45  	q.Query.SetGrpcDeadline(deadline)
    46  	return q
    47  }
    48  
    49  // SetFileID sets the FileID of the file whose contents are requested.
    50  func (q *FileContentsQuery) SetFileID(fileID FileID) *FileContentsQuery {
    51  	q.fileID = &fileID
    52  	return q
    53  }
    54  
    55  // GetFileID returns the FileID of the file whose contents are requested.
    56  func (q *FileContentsQuery) GetFileID() FileID {
    57  	if q.fileID == nil {
    58  		return FileID{}
    59  	}
    60  
    61  	return *q.fileID
    62  }
    63  
    64  func (q *FileContentsQuery) GetCost(client *Client) (Hbar, error) {
    65  	return q.Query.getCost(client, q)
    66  }
    67  
    68  // Execute executes the Query with the provided client
    69  func (q *FileContentsQuery) Execute(client *Client) ([]byte, error) {
    70  	resp, err := q.Query.execute(client, q)
    71  
    72  	if err != nil {
    73  		return []byte{}, err
    74  	}
    75  
    76  	return resp.GetFileGetContents().FileContents.Contents, nil
    77  }
    78  
    79  // SetMaxQueryPayment sets the maximum payment allowed for this Query.
    80  func (q *FileContentsQuery) SetMaxQueryPayment(maxPayment Hbar) *FileContentsQuery {
    81  	q.Query.SetMaxQueryPayment(maxPayment)
    82  	return q
    83  }
    84  
    85  // SetQueryPayment sets the payment amount for this Query.
    86  func (q *FileContentsQuery) SetQueryPayment(paymentAmount Hbar) *FileContentsQuery {
    87  	q.Query.SetQueryPayment(paymentAmount)
    88  	return q
    89  }
    90  
    91  // SetNodeAccountIDs sets the _Node AccountID for this FileContentsQuery.
    92  func (q *FileContentsQuery) SetNodeAccountIDs(accountID []AccountID) *FileContentsQuery {
    93  	q.Query.SetNodeAccountIDs(accountID)
    94  	return q
    95  }
    96  
    97  // SetMaxRetry sets the max number of errors before execution will fail.
    98  func (q *FileContentsQuery) SetMaxRetry(count int) *FileContentsQuery {
    99  	q.Query.SetMaxRetry(count)
   100  	return q
   101  }
   102  
   103  // SetMaxBackoff The maximum amount of time to wait between retries.
   104  // Every retry attempt will increase the wait time exponentially until it reaches this time.
   105  func (q *FileContentsQuery) SetMaxBackoff(max time.Duration) *FileContentsQuery {
   106  	q.Query.SetMaxBackoff(max)
   107  	return q
   108  }
   109  
   110  // SetMinBackoff sets the minimum amount of time to wait between retries.
   111  func (q *FileContentsQuery) SetMinBackoff(min time.Duration) *FileContentsQuery {
   112  	q.Query.SetMinBackoff(min)
   113  	return q
   114  }
   115  
   116  // SetPaymentTransactionID assigns the payment transaction id.
   117  func (q *FileContentsQuery) SetPaymentTransactionID(transactionID TransactionID) *FileContentsQuery {
   118  	q.Query.SetPaymentTransactionID(transactionID)
   119  	return q
   120  }
   121  
   122  func (q *FileContentsQuery) SetLogLevel(level LogLevel) *FileContentsQuery {
   123  	q.Query.SetLogLevel(level)
   124  	return q
   125  }
   126  
   127  // ---------- Parent functions specific implementation ----------
   128  
   129  func (q *FileContentsQuery) getMethod(channel *_Channel) _Method {
   130  	return _Method{
   131  		query: channel._GetFile().GetFileContent,
   132  	}
   133  }
   134  
   135  // Get the name of the Query
   136  func (q *FileContentsQuery) getName() string {
   137  	return "FileContentsQuery"
   138  }
   139  
   140  func (q *FileContentsQuery) buildQuery() *services.Query {
   141  	body := &services.FileGetContentsQuery{
   142  		Header: q.pbHeader,
   143  	}
   144  
   145  	if q.fileID != nil {
   146  		body.FileID = q.fileID._ToProtobuf()
   147  	}
   148  
   149  	return &services.Query{
   150  		Query: &services.Query_FileGetContents{
   151  			FileGetContents: body,
   152  		},
   153  	}
   154  }
   155  
   156  func (q *FileContentsQuery) validateNetworkOnIDs(client *Client) error {
   157  	if client == nil || !client.autoValidateChecksums {
   158  		return nil
   159  	}
   160  
   161  	if q.fileID != nil {
   162  		if err := q.fileID.ValidateChecksum(client); err != nil {
   163  			return err
   164  		}
   165  	}
   166  
   167  	return nil
   168  }
   169  
   170  func (q *FileContentsQuery) getQueryResponse(response *services.Response) queryResponse {
   171  	return response.GetFileGetContents()
   172  }