github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/opensearchapi.response_example_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  //
     3  // The OpenSearch Contributors require contributions made to
     4  // this file be licensed under the Apache-2.0 license or a
     5  // compatible open source license.
     6  //
     7  // Modifications Copyright OpenSearch Contributors. See
     8  // GitHub history for details.
     9  
    10  // Licensed to Elasticsearch B.V. under one or more contributor
    11  // license agreements. See the NOTICE file distributed with
    12  // this work for additional information regarding copyright
    13  // ownership. Elasticsearch B.V. licenses this file to you under
    14  // the Apache License, Version 2.0 (the "License"); you may
    15  // not use this file except in compliance with the License.
    16  // You may obtain a copy of the License at
    17  //
    18  //    http://www.apache.org/licenses/LICENSE-2.0
    19  //
    20  // Unless required by applicable law or agreed to in writing,
    21  // software distributed under the License is distributed on an
    22  // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    23  // KIND, either express or implied.  See the License for the
    24  // specific language governing permissions and limitations
    25  // under the License.
    26  
    27  package opensearchapi_test
    28  
    29  import (
    30  	"log"
    31  
    32  	"github.com/opensearch-project/opensearch-go/v2"
    33  )
    34  
    35  func ExampleResponse_IsError() {
    36  	client, _ := opensearch.NewDefaultClient()
    37  
    38  	res, err := client.Info()
    39  
    40  	// Handle connection errors
    41  	//
    42  	if err != nil {
    43  		log.Fatalf("ERROR: %v", err)
    44  	}
    45  	defer res.Body.Close()
    46  
    47  	// Handle error response (4xx, 5xx)
    48  	//
    49  	if res.IsError() {
    50  		log.Fatalf("ERROR: %s", res.Status())
    51  	}
    52  
    53  	// Handle successful response (2xx)
    54  	//
    55  	log.Println(res)
    56  }
    57  
    58  func ExampleResponse_Status() {
    59  	client, _ := opensearch.NewDefaultClient()
    60  
    61  	res, _ := client.Info()
    62  	log.Println(res.Status())
    63  
    64  	// 200 OK
    65  }
    66  
    67  func ExampleResponse_String() {
    68  	client, _ := opensearch.NewDefaultClient()
    69  
    70  	res, _ := client.Info()
    71  	log.Println(res.String())
    72  
    73  	// [200 OK] {
    74  	// "name" : "opensearch1",
    75  	// "cluster_name" : "opensearch-go",
    76  	// ...
    77  	// }
    78  }