-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor!: module package structure (#87)
- Loading branch information
Showing
33 changed files
with
487 additions
and
443 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package assert | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func Equal[T any](t *testing.T, a T, b T) { | ||
if !reflect.DeepEqual(a, b) { | ||
t.Fatalf("%v != %v", a, b) | ||
} | ||
} | ||
|
||
func NotEqual[T any](t *testing.T, a T, b T) { | ||
if reflect.DeepEqual(a, b) { | ||
t.Fatalf("%v == %v", a, b) | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package mock | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/reugn/go-quartz/job" | ||
) | ||
|
||
type HTTPHandlerMock struct { | ||
DoFunc func(req *http.Request) (*http.Response, error) | ||
} | ||
|
||
func (m HTTPHandlerMock) Do(req *http.Request) (*http.Response, error) { | ||
return m.DoFunc(req) | ||
} | ||
|
||
var ( | ||
HTTPHandlerOk job.HTTPHandler | ||
HTTPHandlerErr job.HTTPHandler | ||
) | ||
|
||
func init() { | ||
HTTPHandlerMockOk := struct{ HTTPHandlerMock }{} | ||
HTTPHandlerMockOk.DoFunc = func(request *http.Request) (*http.Response, error) { | ||
return &http.Response{ | ||
StatusCode: 200, | ||
Request: request, | ||
}, nil | ||
} | ||
HTTPHandlerOk = job.HTTPHandler(HTTPHandlerMockOk) | ||
|
||
HTTPHandlerMockErr := struct{ HTTPHandlerMock }{} | ||
HTTPHandlerMockErr.DoFunc = func(request *http.Request) (*http.Response, error) { | ||
return &http.Response{ | ||
StatusCode: 500, | ||
Request: request, | ||
}, nil | ||
} | ||
HTTPHandlerErr = job.HTTPHandler(HTTPHandlerMockErr) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package job | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"net/http/httputil" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/reugn/go-quartz/quartz" | ||
) | ||
|
||
// CurlJob represents a cURL command Job, implements the quartz.Job interface. | ||
// cURL is a command-line tool for getting or sending data including files | ||
// using URL syntax. | ||
type CurlJob struct { | ||
sync.Mutex | ||
httpClient HTTPHandler | ||
request *http.Request | ||
response *http.Response | ||
jobStatus Status | ||
description string | ||
callback func(context.Context, *CurlJob) | ||
} | ||
|
||
var _ quartz.Job = (*CurlJob)(nil) | ||
|
||
// HTTPHandler sends an HTTP request and returns an HTTP response, | ||
// following policy (such as redirects, cookies, auth) as configured | ||
// on the implementing HTTP client. | ||
type HTTPHandler interface { | ||
Do(req *http.Request) (*http.Response, error) | ||
} | ||
|
||
// CurlJobOptions represents optional parameters for constructing a CurlJob. | ||
type CurlJobOptions struct { | ||
HTTPClient HTTPHandler | ||
Callback func(context.Context, *CurlJob) | ||
} | ||
|
||
// NewCurlJob returns a new CurlJob using the default HTTP client. | ||
func NewCurlJob(request *http.Request) *CurlJob { | ||
return NewCurlJobWithOptions(request, CurlJobOptions{HTTPClient: http.DefaultClient}) | ||
} | ||
|
||
// NewCurlJobWithOptions returns a new CurlJob configured with CurlJobOptions. | ||
func NewCurlJobWithOptions(request *http.Request, opts CurlJobOptions) *CurlJob { | ||
if opts.HTTPClient == nil { | ||
opts.HTTPClient = http.DefaultClient | ||
} | ||
return &CurlJob{ | ||
httpClient: opts.HTTPClient, | ||
request: request, | ||
jobStatus: StatusNA, | ||
description: formatRequest(request), | ||
callback: opts.Callback, | ||
} | ||
} | ||
|
||
// Description returns the description of the CurlJob. | ||
func (cu *CurlJob) Description() string { | ||
return fmt.Sprintf("CurlJob:\n%s", cu.description) | ||
} | ||
|
||
// DumpResponse returns the response of the job in its HTTP/1.x wire | ||
// representation. | ||
// If body is true, DumpResponse also returns the body. | ||
func (cu *CurlJob) DumpResponse(body bool) ([]byte, error) { | ||
cu.Lock() | ||
defer cu.Unlock() | ||
return httputil.DumpResponse(cu.response, body) | ||
} | ||
|
||
// JobStatus returns the status of the CurlJob. | ||
func (cu *CurlJob) JobStatus() Status { | ||
cu.Lock() | ||
defer cu.Unlock() | ||
return cu.jobStatus | ||
} | ||
|
||
func formatRequest(r *http.Request) string { | ||
var request []string | ||
url := fmt.Sprintf("%v %v %v", r.Method, r.URL, r.Proto) | ||
request = append(request, url) | ||
for name, headers := range r.Header { | ||
for _, h := range headers { | ||
request = append(request, fmt.Sprintf("%v: %v", name, h)) | ||
} | ||
} | ||
if r.ContentLength > 0 { | ||
request = append(request, fmt.Sprintf("Content Length: %d", r.ContentLength)) | ||
} | ||
return strings.Join(request, "\n") | ||
} | ||
|
||
// Execute is called by a Scheduler when the Trigger associated with this job fires. | ||
func (cu *CurlJob) Execute(ctx context.Context) error { | ||
cu.Lock() | ||
cu.request = cu.request.WithContext(ctx) | ||
var err error | ||
cu.response, err = cu.httpClient.Do(cu.request) | ||
|
||
if err == nil && cu.response.StatusCode >= 200 && cu.response.StatusCode < 400 { | ||
cu.jobStatus = StatusOK | ||
} else { | ||
cu.jobStatus = StatusFailure | ||
} | ||
cu.Unlock() | ||
|
||
if cu.callback != nil { | ||
cu.callback(ctx, cu) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Package job contains implementations of the quartz.Job interface. | ||
package job |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.