github.com/blend/go-sdk@v1.20220411.3/pagerduty/get_service.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package pagerduty
     9  
    10  import (
    11  	"context"
    12  	"encoding/json"
    13  	"fmt"
    14  	"net/http"
    15  
    16  	"github.com/blend/go-sdk/ex"
    17  	"github.com/blend/go-sdk/r2"
    18  )
    19  
    20  // GetService gets a service
    21  func (hc HTTPClient) GetService(ctx context.Context, id string) (service Service, err error) {
    22  	var res *http.Response
    23  	res, err = hc.Request(ctx,
    24  		r2.OptGet(),
    25  		r2.OptPath(fmt.Sprintf("/services/%s", id)),
    26  	).Do()
    27  	if err != nil {
    28  		return
    29  	}
    30  	if statusCode := res.StatusCode; statusCode < 200 || statusCode > 299 {
    31  		statusErr := ErrNon200Status
    32  		if statusCode == 404 {
    33  			statusErr = Err404Status
    34  		}
    35  		err = ex.New(statusErr, ex.OptMessagef("method: post, path: /services/%s, status: %d", id, statusCode))
    36  		return
    37  	}
    38  	defer res.Body.Close()
    39  	var output map[string]Service
    40  	if err = json.NewDecoder(res.Body).Decode(&output); err != nil {
    41  		err = ex.New(err)
    42  		return
    43  	}
    44  
    45  	service, ok := output["service"]
    46  	if !ok {
    47  		err = ex.New("JSON response did not include the service field")
    48  	}
    49  
    50  	return
    51  }