github.com/alloyzeus/go-azfl@v0.0.0-20231220071816-9740126a2d07/azcore/service_method_call.go (about)

     1  package azcore
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"github.com/alloyzeus/go-azfl/azob"
     8  	"golang.org/x/text/language"
     9  )
    10  
    11  //region ServiceMethodCallContext
    12  
    13  type ServiceMethodCallContext interface {
    14  	ServiceMethodContext
    15  
    16  	AZServiceMethodCallContext()
    17  
    18  	// MethodName returns the name of the method or the endpoint.
    19  	//
    20  	// For HTTP, this method returns the method. For other protocols, it should
    21  	// be the name of the method e.g., `getUser`.
    22  	MethodName() string
    23  
    24  	// ResourceID returns the identifier of the resource being accessed by
    25  	// the call. For HTTP, it's the path. For other protocols, it should
    26  	// be the identifier (ID) of the entity. If there's more than one
    27  	// resources, e.g., a relationship between two entities, then it returns
    28  	// the identifiers of the entities separated by commas.
    29  	ResourceID() string
    30  }
    31  
    32  //endregion
    33  
    34  type ServiceMethodCallInputMetadata interface {
    35  	//TODO: request id / correlation id / idemptotency key, receive time by
    36  	// the handler.
    37  }
    38  
    39  // ServiceMethodCallOriginInfo holds information about a call's origin.
    40  //
    41  //TODO: key-value custom data
    42  type ServiceMethodCallOriginInfo struct {
    43  	// Address returns the IP address or hostname where this call was initiated
    44  	// from. This field might be empty if it's not possible to resolve
    45  	// the address (e.g., the server is behind a proxy or a load-balancer and
    46  	// they didn't forward the the origin IP).
    47  	Address string
    48  
    49  	// EnvironmentString returns some details of the environment,
    50  	// might include application's version information, where the application
    51  	// which made the request runs on. For web app, this method usually
    52  	// returns the browser's user-agent string.
    53  	EnvironmentString string
    54  
    55  	// AcceptLanguage is analogous to HTTP Accept-Language header field. The
    56  	// languages must be ordered by the human's preference.
    57  	// If the languages comes as weighted, as found in HTTP Accept-Language,
    58  	// sort the languages by their weights then drop the weights.
    59  	AcceptLanguage []language.Tag
    60  
    61  	// DateTime is the time of the caller device when the call was initiated.
    62  	//
    63  	// Analogous to HTTP Date header field.
    64  	DateTime *time.Time
    65  }
    66  
    67  //region ServiceMethodIdempotencyKey
    68  
    69  // ServiceMethodIdempotencyKey represents the identifier of a method call. This
    70  // identifier doubles as idempotency token.
    71  type ServiceMethodIdempotencyKey interface {
    72  	azob.Equatable
    73  
    74  	AZServiceMethodIdempotencyKey()
    75  }
    76  
    77  //endregion
    78  
    79  //region ServiceMethodCallInputData
    80  
    81  // ServiceMethodCallInputData abstracts method request body.
    82  type ServiceMethodCallInputData interface {
    83  }
    84  
    85  //endregion
    86  
    87  //region ServiceMethodCallInvocationError
    88  
    89  // ServiceMethodCallInvocationError is a sub-class of ServiceMethodError which
    90  // indicates that there's an error in the request.
    91  //
    92  // This error class is analogous to HTTP's 4xx status codes.
    93  //
    94  //TODO: sub-classes: acces, parameters, context
    95  type ServiceMethodCallInvocationError interface {
    96  	ServiceMethodError
    97  
    98  	AZServiceMethodCallInvocationError()
    99  }
   100  
   101  //endregion
   102  
   103  type ServiceMethodCallInput[
   104  	SessionIDNumT SessionIDNum, SessionIDT SessionID[SessionIDNumT],
   105  	TerminalIDNumT TerminalIDNum, TerminalIDT TerminalID[TerminalIDNumT],
   106  	UserIDNumT UserIDNum, UserIDT UserID[UserIDNumT],
   107  	SessionSubjectT SessionSubject[
   108  		TerminalIDNumT, TerminalIDT, UserIDNumT, UserIDT],
   109  	SessionT Session[
   110  		SessionIDNumT, SessionIDT,
   111  		TerminalIDNumT, TerminalIDT,
   112  		UserIDNumT, UserIDT,
   113  		SessionSubjectT, SessionT],
   114  	ServiceMethodIdempotencyKeyT ServiceMethodIdempotencyKey,
   115  	InputContextT ServiceMethodCallInputContext[
   116  		SessionIDNumT, SessionIDT, TerminalIDNumT, TerminalIDT,
   117  		UserIDNumT, UserIDT, SessionSubjectT, SessionT,
   118  		ServiceMethodIdempotencyKeyT],
   119  	InputDataT ServiceMethodCallInputData,
   120  ] struct {
   121  	Context InputContextT
   122  	Data    InputDataT
   123  }
   124  
   125  // ServiceMethodCallInputContext provides an abstraction for all input contexts
   126  // in method call inputs.
   127  type ServiceMethodCallInputContext[
   128  	SessionIDNumT SessionIDNum, SessionIDT SessionID[SessionIDNumT],
   129  	TerminalIDNumT TerminalIDNum, TerminalIDT TerminalID[TerminalIDNumT],
   130  	UserIDNumT UserIDNum, UserIDT UserID[UserIDNumT],
   131  	SessionSubjectT SessionSubject[
   132  		TerminalIDNumT, TerminalIDT, UserIDNumT, UserIDT],
   133  	SessionT Session[
   134  		SessionIDNumT, SessionIDT,
   135  		TerminalIDNumT, TerminalIDT,
   136  		UserIDNumT, UserIDT,
   137  		SessionSubjectT, SessionT],
   138  	ServiceMethodIdempotencyKeyT ServiceMethodIdempotencyKey,
   139  ] interface {
   140  	ServiceMethodCallContext
   141  
   142  	AZServiceMethodCallInputContext()
   143  
   144  	// Session returns the session for this context.
   145  	Session() SessionT
   146  
   147  	// IdempotencyKey is a key used to ensure that a distinct operation is
   148  	// performed at most once.
   149  	//
   150  	// This key is different from request ID, where for the same operation,
   151  	// there could be more than one requests in attempt to retry in the event
   152  	// of transit error.
   153  	IdempotencyKey() ServiceMethodIdempotencyKeyT
   154  
   155  	// OriginInfo returns information about the system that made the call.
   156  	OriginInfo() ServiceMethodCallOriginInfo
   157  }
   158  
   159  // ServiceMethodCallInputContextError provides information for
   160  // request-context-related error. It is a sub-class of
   161  // ServiceMethodCallInputError.
   162  type ServiceMethodCallInputContextError interface {
   163  	ServiceMethodCallInvocationError
   164  
   165  	AZServiceMethodCallInputContextError()
   166  }
   167  
   168  // ServiceMethodCallSessionError is a sub-class of
   169  // ServiceMethodCallInputContextError specialized for indicating error
   170  // in the session.
   171  type ServiceMethodCallSessionError interface {
   172  	ServiceMethodCallInputContextError
   173  
   174  	AZServiceMethodCallSessionError()
   175  }
   176  
   177  //region ServiceMethodCallOutputContext
   178  
   179  // ServiceMethodCallOutputContext provides an abstraction for all output contexts
   180  // in method call outputs.
   181  //
   182  //TODO: listing of affected states with their respective revision ID.
   183  //TODO: directive: done/end, redirect, retry (on failure; optionally with
   184  // timing and retry count parameters or exponentially back-off parameters)
   185  type ServiceMethodCallOutputContext interface {
   186  	//TODO: ServiceMethodCallContext, or keep it like this and
   187  	// add a method to access the input context: MethodCallInputContext()
   188  	ServiceMethodContext
   189  
   190  	AZServiceMethodCallOutputContext()
   191  
   192  	// // Succeed returns true when the method achieved its objective.
   193  	// Succeed() bool
   194  
   195  	// Returns the error, if any.
   196  	ServiceMethodErr() ServiceMethodError
   197  
   198  	// Mutated returns true if the method made any changes to any state in the
   199  	// server, even when the method did not succeed. It should not
   200  	// return true if the change has been completely rolled-back before the
   201  	// method returned this context.
   202  	Mutated() bool
   203  }
   204  
   205  //endregion
   206  
   207  //region ServiceMethodCallOutputContextBase
   208  
   209  // ServiceMethodCallOutputContextBase is a base
   210  // for ServiceMethodCallOutputContext implementations.
   211  type ServiceMethodCallOutputContextBase struct {
   212  	context.Context
   213  
   214  	err     ServiceMethodError
   215  	mutated bool
   216  }
   217  
   218  var _ ServiceMethodCallOutputContext = ServiceMethodCallOutputContextBase{}
   219  
   220  // NewMethodCallOutputContext creates a new instance
   221  // of ServiceMethodCallOutputContext.
   222  func NewMethodCallOutputContext(
   223  	err ServiceMethodError,
   224  	mutated bool,
   225  ) ServiceMethodCallOutputContextBase {
   226  	return ServiceMethodCallOutputContextBase{err: err, mutated: mutated}
   227  }
   228  
   229  // AZContext is required for conformance with Context.
   230  func (ServiceMethodCallOutputContextBase) AZContext() {}
   231  
   232  // AZServiceContext is required for conformance with ServiceContext.
   233  func (ServiceMethodCallOutputContextBase) AZServiceContext() {}
   234  
   235  // AZServiceMethodContext is required
   236  // for conformance with ServiceMethodContext.
   237  func (ServiceMethodCallOutputContextBase) AZServiceMethodContext() {}
   238  
   239  // AZServiceMethodCallOutputContext is required
   240  // for conformance with ServiceMethodCallOutputContext.
   241  func (ServiceMethodCallOutputContextBase) AZServiceMethodCallOutputContext() {}
   242  
   243  // ServiceMethodErr is required for conformance with ServiceMethodCallOutputContext.
   244  func (ctx ServiceMethodCallOutputContextBase) ServiceMethodErr() ServiceMethodError { return ctx.err }
   245  
   246  // Mutated is required for conformance with ServiceMethodCallOutputContext.
   247  func (ctx ServiceMethodCallOutputContextBase) Mutated() bool { return ctx.mutated }
   248  
   249  //endregion