github.com/blend/go-sdk@v1.20220411.3/web/json_result_provider.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 web
     9  
    10  import (
    11  	"net/http"
    12  )
    13  
    14  var (
    15  	// JSON is a static singleton json result provider.
    16  	JSON JSONResultProvider
    17  	// assert it implements result provider.
    18  	_ ResultProvider = (*JSONResultProvider)(nil)
    19  )
    20  
    21  // JSONResultProvider are context results for api methods.
    22  type JSONResultProvider struct{}
    23  
    24  // NotFound returns a service response.
    25  func (jrp JSONResultProvider) NotFound() Result {
    26  	return &JSONResult{
    27  		StatusCode: http.StatusNotFound,
    28  		Response:   "Not Found",
    29  	}
    30  }
    31  
    32  // NotAuthorized returns a service response.
    33  func (jrp JSONResultProvider) NotAuthorized() Result {
    34  	return &JSONResult{
    35  		StatusCode: http.StatusUnauthorized,
    36  		Response:   "Not Authorized",
    37  	}
    38  }
    39  
    40  // Forbidden returns a 403 Forbidden response.
    41  func (jrp JSONResultProvider) Forbidden() Result {
    42  	return &JSONResult{
    43  		StatusCode: http.StatusForbidden,
    44  		Response:   "Forbidden",
    45  	}
    46  }
    47  
    48  // InternalError returns a service response.
    49  func (jrp JSONResultProvider) InternalError(err error) Result {
    50  	if err != nil {
    51  		return ResultWithLoggedError(&JSONResult{
    52  			StatusCode: http.StatusInternalServerError,
    53  			Response:   err.Error(),
    54  		}, err)
    55  	}
    56  	return ResultWithLoggedError(&JSONResult{
    57  		StatusCode: http.StatusInternalServerError,
    58  		Response:   "Internal Server Error",
    59  	}, err)
    60  }
    61  
    62  // BadRequest returns a service response.
    63  func (jrp JSONResultProvider) BadRequest(err error) Result {
    64  	if err != nil {
    65  		return &JSONResult{
    66  			StatusCode: http.StatusBadRequest,
    67  			Response:   err.Error(),
    68  		}
    69  	}
    70  	return &JSONResult{
    71  		StatusCode: http.StatusBadRequest,
    72  		Response:   "Bad Request",
    73  	}
    74  }
    75  
    76  // OK returns a service response.
    77  func (jrp JSONResultProvider) OK() Result {
    78  	return &JSONResult{
    79  		StatusCode: http.StatusOK,
    80  		Response:   "OK!",
    81  	}
    82  }
    83  
    84  // Status returns a plaintext result.
    85  func (jrp JSONResultProvider) Status(statusCode int, response interface{}) Result {
    86  	return &JSONResult{
    87  		StatusCode: statusCode,
    88  		Response:   ResultOrDefault(response, http.StatusText(statusCode)),
    89  	}
    90  }
    91  
    92  // Result returns a json response.
    93  func (jrp JSONResultProvider) Result(response interface{}) Result {
    94  	return &JSONResult{
    95  		StatusCode: http.StatusOK,
    96  		Response:   response,
    97  	}
    98  }