github.com/blend/go-sdk@v1.20220411.3/web/text_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  	"fmt"
    12  	"net/http"
    13  
    14  	"github.com/blend/go-sdk/webutil"
    15  )
    16  
    17  var (
    18  	// Text is a static singleton text result provider.
    19  	Text TextResultProvider
    20  
    21  	// assert TestResultProvider implements result provider.
    22  	_ ResultProvider = Text
    23  )
    24  
    25  // TextResultProvider is the default response provider if none is specified.
    26  type TextResultProvider struct{}
    27  
    28  // NotFound returns a plaintext result.
    29  func (trp TextResultProvider) NotFound() Result {
    30  	return &RawResult{
    31  		StatusCode:  http.StatusNotFound,
    32  		ContentType: webutil.ContentTypeText,
    33  		Response:    []byte("Not Found"),
    34  	}
    35  }
    36  
    37  // NotAuthorized returns a plaintext result.
    38  func (trp TextResultProvider) NotAuthorized() Result {
    39  	return &RawResult{
    40  		StatusCode:  http.StatusUnauthorized,
    41  		ContentType: webutil.ContentTypeText,
    42  		Response:    []byte("Not Authorized"),
    43  	}
    44  }
    45  
    46  // InternalError returns a plainttext result.
    47  func (trp TextResultProvider) InternalError(err error) Result {
    48  	return ResultWithLoggedError(&RawResult{
    49  		StatusCode:  http.StatusInternalServerError,
    50  		ContentType: webutil.ContentTypeText,
    51  		Response:    []byte(fmt.Sprintf("%+v", err)),
    52  	}, err)
    53  }
    54  
    55  // BadRequest returns a plaintext result.
    56  func (trp TextResultProvider) BadRequest(err error) Result {
    57  	if err != nil {
    58  		return &RawResult{
    59  			StatusCode:  http.StatusBadRequest,
    60  			ContentType: webutil.ContentTypeText,
    61  			Response:    []byte(fmt.Sprintf("Bad Request: %v", err)),
    62  		}
    63  	}
    64  	return &RawResult{
    65  		StatusCode:  http.StatusBadRequest,
    66  		ContentType: webutil.ContentTypeText,
    67  		Response:    []byte("Bad Request"),
    68  	}
    69  }
    70  
    71  // Status returns a plaintext result.
    72  func (trp TextResultProvider) Status(statusCode int, response interface{}) Result {
    73  	return &RawResult{
    74  		StatusCode:  statusCode,
    75  		ContentType: webutil.ContentTypeText,
    76  		Response:    []byte(fmt.Sprint(ResultOrDefault(response, http.StatusText(statusCode)))),
    77  	}
    78  }
    79  
    80  // OK returns an plaintext result.
    81  func (trp TextResultProvider) OK() Result {
    82  	return &RawResult{
    83  		StatusCode:  http.StatusOK,
    84  		ContentType: webutil.ContentTypeText,
    85  		Response:    []byte("OK!"),
    86  	}
    87  }
    88  
    89  // Result returns a plaintext result.
    90  func (trp TextResultProvider) Result(result interface{}) Result {
    91  	return &RawResult{
    92  		StatusCode:  http.StatusOK,
    93  		ContentType: webutil.ContentTypeText,
    94  		Response:    []byte(fmt.Sprintf("%v", result)),
    95  	}
    96  }