go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/web/gob_result_provider.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package web
     9  
    10  import (
    11  	"net/http"
    12  )
    13  
    14  var (
    15  	// assert GobResultProbider implements ResultProvider.
    16  	_ ResultProvider = (*GobResultProvider)(nil)
    17  )
    18  
    19  // GobResultProvider returns gob results for common responses.
    20  type GobResultProvider struct{}
    21  
    22  // NotFound returns a service response.
    23  func (grp GobResultProvider) NotFound() Result {
    24  	return &GobResult{
    25  		StatusCode: http.StatusNotFound,
    26  		Response:   http.StatusText(http.StatusNotFound),
    27  	}
    28  }
    29  
    30  // NotAuthorized returns a service response.
    31  func (grp GobResultProvider) NotAuthorized() Result {
    32  	return &GobResult{
    33  		StatusCode: http.StatusUnauthorized,
    34  		Response:   http.StatusText(http.StatusUnauthorized),
    35  	}
    36  }
    37  
    38  // Forbidden returns a service response.
    39  func (grp GobResultProvider) Forbidden() Result {
    40  	return &GobResult{
    41  		StatusCode: http.StatusForbidden,
    42  		Response:   http.StatusText(http.StatusForbidden),
    43  	}
    44  }
    45  
    46  // InternalError returns a service response.
    47  func (grp GobResultProvider) InternalError(err error) Result {
    48  	return &GobResult{
    49  		StatusCode: http.StatusInternalServerError,
    50  		Response:   err.Error(),
    51  	}
    52  }
    53  
    54  // BadRequest returns a service response.
    55  func (grp GobResultProvider) BadRequest(err error) Result {
    56  	return &GobResult{
    57  		StatusCode: http.StatusBadRequest,
    58  		Response:   err.Error(),
    59  	}
    60  }
    61  
    62  // OK returns a service response.
    63  func (grp GobResultProvider) OK() Result {
    64  	return &GobResult{
    65  		StatusCode: http.StatusOK,
    66  		Response:   "OK!",
    67  	}
    68  }
    69  
    70  // Result returns a service response.
    71  func (grp GobResultProvider) Result(statusCode int, result any) Result {
    72  	return &GobResult{
    73  		StatusCode: statusCode,
    74  		Response:   result,
    75  	}
    76  }