diff --git a/client.go b/client.go index 02ff33f6..fb6d2c51 100644 --- a/client.go +++ b/client.go @@ -143,10 +143,10 @@ func (a apiClient) SendMany(ctx context.Context, e []any) ([]string, error) { if err != nil { return nil, fmt.Errorf("error creating event request: %w", err) } - req.Header.Set("content-type", "application/json") + SetBasicRequestHeaders(req) if a.GetEnv() != "" { - req.Header.Add("X-Inngest-Env", a.GetEnv()) + req.Header.Add(HeaderKeyEnv, a.GetEnv()) } resp, err := a.HTTPClient.Post(url, "application/json", bytes.NewBuffer(byt)) diff --git a/consts.go b/consts.go new file mode 100644 index 00000000..9603fc8e --- /dev/null +++ b/consts.go @@ -0,0 +1,6 @@ +package inngestgo + +const ( + SDKLanguage = "go" + SDKVersion = "0.0.1" +) diff --git a/handler.go b/handler.go index 3754ec15..c77251a7 100644 --- a/handler.go +++ b/handler.go @@ -206,6 +206,7 @@ func (h *handler) Register(funcs ...ServableFunction) { func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { h.Logger.Debug("received http request", "method", r.Method) + SetBasicResponseHeaders(w) switch r.Method { case http.MethodPost: @@ -239,7 +240,7 @@ func (h *handler) register(w http.ResponseWriter, r *http.Request) error { URL: fmt.Sprintf("%s://%s%s", scheme, host, path), V: "1", DeployType: "ping", - SDK: "go:v0.0.1", + SDK: HeaderValueSDK, AppName: h.appName, Headers: sdk.Headers{ Env: h.GetEnv(), @@ -340,10 +341,11 @@ func (h *handler) register(w http.ResponseWriter, r *http.Request) error { if err != nil { return fmt.Errorf("error creating signing key: %w", err) } - req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", string(key))) + req.Header.Add(HeaderKeyAuthorization, fmt.Sprintf("Bearer %s", string(key))) if h.GetEnv() != "" { - req.Header.Add("X-Inngest-Env", h.GetEnv()) + req.Header.Add(HeaderKeyEnv, h.GetEnv()) } + SetBasicRequestHeaders(req) resp, err := http.DefaultClient.Do(req) if err != nil { @@ -381,7 +383,7 @@ func (h *handler) invoke(w http.ResponseWriter, r *http.Request) error { defer r.Body.Close() if !IsDev() { - if sig = r.Header.Get("X-Inngest-Signature"); sig == "" { + if sig = r.Header.Get(HeaderKeySignature); sig == "" { return publicerr.Error{ Message: "unauthorized", Status: 401, @@ -499,11 +501,11 @@ func (h *handler) invoke(w http.ResponseWriter, r *http.Request) error { l.Error("error calling function", "error", err) if isNoRetryError(err) { - w.Header().Add("x-inngest-no-retry", "true") + w.Header().Add(HeaderKeyNoRetry, "true") } if at := getRetryAtTime(err); at != nil { - w.Header().Add("retry-after", at.Format(time.RFC3339)) + w.Header().Add(HeaderKeyRetryAfter, at.Format(time.RFC3339)) } return publicerr.Error{ diff --git a/headers.go b/headers.go new file mode 100644 index 00000000..5a046c94 --- /dev/null +++ b/headers.go @@ -0,0 +1,33 @@ +package inngestgo + +import ( + "fmt" + "net/http" +) + +const ( + HeaderKeyAuthorization = "Authorization" + HeaderKeyContentType = "Content-Type" + HeaderKeyEnv = "X-Inngest-Env" + HeaderKeyNoRetry = "X-Inngest-No-Retry" + HeaderKeyRetryAfter = "Retry-After" + HeaderKeySDK = "X-Inngest-SDK" + HeaderKeySignature = "X-Inngest-Signature" + HeaderKeyUserAgent = "User-Agent" +) + +var ( + HeaderValueSDK = fmt.Sprintf("%s:%s", SDKLanguage, SDKVersion) +) + +func SetBasicRequestHeaders(req *http.Request) { + req.Header.Set(HeaderKeyContentType, "application/json") + req.Header.Set(HeaderKeySDK, HeaderValueSDK) + req.Header.Set(HeaderKeyUserAgent, HeaderValueSDK) +} + +func SetBasicResponseHeaders(w http.ResponseWriter) { + w.Header().Set(HeaderKeyContentType, "application/json") + w.Header().Set(HeaderKeySDK, HeaderValueSDK) + w.Header().Set(HeaderKeyUserAgent, HeaderValueSDK) +}