github.com/blend/go-sdk@v1.20240719.1/pagerduty/mock_server.go (about)

     1  /*
     2  
     3  Copyright (c) 2024 - 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  	"encoding/json"
    12  	"net/http"
    13  	"strings"
    14  
    15  	"github.com/blend/go-sdk/webutil"
    16  )
    17  
    18  var (
    19  	_ http.Handler = (*MockAPI)(nil)
    20  )
    21  
    22  // MockAPI implements methods that can be called with the client.
    23  type MockAPI struct {
    24  	ListIncidents  func() (ListIncidentsOutput, error)
    25  	UpdateIncident func(string, UpdateIncidentInput) (Incident, error)
    26  	CreateIncident func(CreateIncidentInput) (Incident, error)
    27  }
    28  
    29  // Handler implements http.Handler.
    30  func (ma MockAPI) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
    31  	if req.Method == http.MethodGet {
    32  		if req.URL.Path == "/incidents" {
    33  			if ma.ListIncidents != nil {
    34  				output, err := ma.ListIncidents()
    35  				if err != nil {
    36  					_ = webutil.WriteJSON(rw, http.StatusInternalServerError, err.Error())
    37  					return
    38  				}
    39  				_ = webutil.WriteJSON(rw, http.StatusOK, output)
    40  				return
    41  			}
    42  		}
    43  	}
    44  	if req.Method == http.MethodPut {
    45  		if strings.HasPrefix(req.URL.Path, "/incidents/") {
    46  			if ma.UpdateIncident != nil {
    47  				incidentID := strings.TrimPrefix(req.URL.Path, "/incidents/")
    48  				var incidentBody updateIncidentInputWrapper
    49  				if err := json.NewDecoder(req.Body).Decode(&incidentBody); err != nil {
    50  					http.Error(rw, err.Error(), http.StatusBadRequest)
    51  					return
    52  				}
    53  				incident, err := ma.UpdateIncident(incidentID, incidentBody.Incident)
    54  				if err != nil {
    55  					_ = webutil.WriteJSON(rw, http.StatusInternalServerError, err.Error())
    56  					return
    57  				}
    58  				_ = webutil.WriteJSON(rw, http.StatusOK, updateIncidentOutputWrapper{Incident: incident})
    59  				return
    60  			}
    61  		}
    62  	}
    63  	if req.Method == http.MethodPost {
    64  		if req.URL.Path == "/incidents" {
    65  			var incidentBody createIncidentInputWrapper
    66  			if err := json.NewDecoder(req.Body).Decode(&incidentBody); err != nil {
    67  				http.Error(rw, err.Error(), http.StatusBadRequest)
    68  				return
    69  			}
    70  			incident, err := ma.CreateIncident(incidentBody.Incident)
    71  			if err != nil {
    72  				_ = webutil.WriteJSON(rw, http.StatusInternalServerError, err.Error())
    73  				return
    74  			}
    75  			_ = webutil.WriteJSON(rw, http.StatusOK, updateIncidentOutputWrapper{Incident: incident})
    76  			return
    77  		}
    78  	}
    79  	http.NotFound(rw, req)
    80  }