Browse Source

En cours de développement

master
losyme 2 months ago
parent
commit
144d18761f
  1. 215
      <
  2. 6
      client/endpoint.go
  3. 2
      go.mod
  4. 4
      go.sum

215
<

@ -0,0 +1,215 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### sdk ####### Copyright (c) 2021-2022 losyme ################################################# MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package client
import (
"context"
"net/http"
"forge.chapril.org/dune/jw"
"forge.chapril.org/losyme/breaker"
"forge.chapril.org/losyme/errors"
"forge.chapril.org/losyme/king"
"forge.chapril.org/losyme/king/request"
_kong "forge.chapril.org/losyme/kong/context"
)
const (
_errRetry = errors.Sentinel("retry")
_errJobCreated = errors.Sentinel("job created")
_errWorkflowCreated = errors.Sentinel("workflow created")
ErrJobRunning = errors.Sentinel("job running")
ErrJobUpdated = errors.Sentinel("job updated")
)
type endpoint struct {
*Endpoint
breaker *breaker.Breaker
client *http.Client
}
func (ep *endpoint) isReady() bool {
return ep.breaker.IsReady()
}
func (ep *endpoint) update(success bool) {
ep.breaker.Update(success)
}
func (ep *endpoint) afterDoRequest(err error) error {
if err == nil {
ep.update(true)
return nil
}
ep.update(false)
return errors.Wrap(_errRetry, err)
}
func onError(resp *http.Response) error {
respErr := new(_kong.Error)
if err := king.DecodeJSON(resp, respErr); err != nil {
return errors.New(http.StatusText(resp.StatusCode), "status", resp.StatusCode) /////////////////////////////////
}
return respErr
}
func (ep *endpoint) createJob(reqID string, content []byte) (*jw.Job, error) {
var job *jw.Job
err := request.
New(ep.URL).
Path("api/jobs").
Post().
BasicAuth(ep.Username, ep.Password).
ContentTypeJSON().
ID(reqID).
BodyData(content).
Client(ep.client).
Execute(
context.TODO(), // @FIXME
ep.afterDoRequest,
func(resp *http.Response) error {
switch resp.StatusCode {
case http.StatusNoContent:
return nil
case http.StatusCreated:
to := new(jw.Job)
if err := king.DecodeJSON(resp, to); err != nil {
return errors.Wrap(_errJobCreated, err) ////////////////////////////////////////////////////////
}
job = to
return nil
}
return onError(resp)
},
)
return job, err
}
func (ep *endpoint) nextJob(reqID, namespace string) (*jw.Job, error) {
var job *jw.Job
err := request.
New(ep.URL).
Path("api/jobs/next/"+namespace).
Get().
BasicAuth(ep.Username, ep.Password).
ID(reqID).
Client(ep.client).
Execute(
context.TODO(), // @FIXME
ep.afterDoRequest,
func(resp *http.Response) error {
switch resp.StatusCode {
case http.StatusNoContent:
return nil
case http.StatusOK:
to := new(jw.Job)
if err := king.DecodeJSON(resp, to); err != nil {
return errors.Wrap(ErrJobRunning, err) /////////////////////////////////////////////////////////
}
job = to
return nil
}
return onError(resp)
},
)
return job, err
}
func (ep *endpoint) updateJob(reqID string, content []byte) (*jw.Job, error) {
var job *jw.Job
err := request.
New(ep.URL).
Path("api/jobs").
Put().
BasicAuth(ep.Username, ep.Password).
ContentTypeJSON().
ID(reqID).
BodyData(content).
Client(ep.client).
Execute(
context.TODO(), // @FIXME
ep.afterDoRequest,
func(resp *http.Response) error {
switch resp.StatusCode {
case http.StatusOK:
to := new(jw.Job)
if err := king.DecodeJSON(resp, to); err != nil {
return errors.Wrap(ErrJobUpdated, err) /////////////////////////////////////////////////////////
}
job = to
return nil
}
return onError(resp)
},
)
return job, err
}
func (ep *endpoint) createWorkflow(reqID string, content []byte) (*jw.Workflow, error) {
var wf *jw.Workflow
err := request.
New(ep.URL).
Path("api/workflows").
Post().
BasicAuth(ep.Username, ep.Password).
ContentTypeJSON().
ID(reqID).
BodyData(content).
Client(ep.client).
Execute(
context.TODO(), // @FIXME
ep.afterDoRequest,
func(resp *http.Response) error {
switch resp.StatusCode {
case http.StatusCreated:
to := new(jw.Workflow)
if err := king.DecodeJSON(resp, to); err != nil {
return errors.Wrap(_errWorkflowCreated, err) ///////////////////////////////////////////////////
}
wf = to
return nil
}
return onError(resp)
},
)
return wf, err
}
/*
######################################################################################################## @(°_°)@ #######
*/

6
client/endpoint.go

@ -53,13 +53,13 @@ func (ep *endpoint) afterDoRequest(err error) error {
}
func onError(resp *http.Response) error {
to := new(_kong.Error)
respErr := new(_kong.Error)
if err := king.DecodeJSON(resp, to); err != nil {
if err := king.DecodeJSON(resp, respErr); err != nil {
return errors.New(http.StatusText(resp.StatusCode), "status", resp.StatusCode) /////////////////////////////////
}
return errors.New(to.Message, "status", to.Status) /////////////////////////////////////////////////////////////////
return respErr
}
func (ep *endpoint) createJob(reqID string, content []byte) (*jw.Job, error) {

2
go.mod

@ -7,7 +7,7 @@ require (
forge.chapril.org/losyme/breaker v0.0.0-20220411170549-42dd8e90d60f
forge.chapril.org/losyme/errors v0.0.0-20220410175325-24ea6bde8500
forge.chapril.org/losyme/king v0.0.0-20220410175404-85e49de392ff
forge.chapril.org/losyme/kong v0.0.0-20220410175413-680fbdc4fcb4
forge.chapril.org/losyme/kong v0.0.0-20220413171121-f4f5821ef677
forge.chapril.org/losyme/roundrobin v0.0.0-20220409170047-3bc78a848535
forge.chapril.org/losyme/util v0.0.0-20220409171219-fbe160b317fd
forge.chapril.org/losyme/uuid v0.0.0-20220410175232-691303a2786a

4
go.sum

@ -8,8 +8,8 @@ forge.chapril.org/losyme/errors v0.0.0-20220410175325-24ea6bde8500 h1:XXxlB1nyPa
forge.chapril.org/losyme/errors v0.0.0-20220410175325-24ea6bde8500/go.mod h1:DPi7ShpfKYwozYe7K4lLIv3BadlhTmqVhwhUiQ3u5GE=
forge.chapril.org/losyme/king v0.0.0-20220410175404-85e49de392ff h1:e2cmn8OJOPRu7I+tWh9rqyDBJxvN5pJRQw1NQptP4zg=
forge.chapril.org/losyme/king v0.0.0-20220410175404-85e49de392ff/go.mod h1:n/LrQjdpCJvFvwonRCwEBU5EaR+SX/qA/YLCy/ghHvA=
forge.chapril.org/losyme/kong v0.0.0-20220410175413-680fbdc4fcb4 h1:Xrtaq9LBy1Xa+FL1V5EI4AJyf1Wl05mUbkQQNAEZeLQ=
forge.chapril.org/losyme/kong v0.0.0-20220410175413-680fbdc4fcb4/go.mod h1:BsqoQyl7vZyZImEsWgfFl7xALOuBfzZzk2ZJMr/nL9A=
forge.chapril.org/losyme/kong v0.0.0-20220413171121-f4f5821ef677 h1:D43aIIM51FgJgaQf1Mt5KSossp964yld6H6TkXib5Ls=
forge.chapril.org/losyme/kong v0.0.0-20220413171121-f4f5821ef677/go.mod h1:BsqoQyl7vZyZImEsWgfFl7xALOuBfzZzk2ZJMr/nL9A=
forge.chapril.org/losyme/kvfmt v0.0.0-20220410175310-5d426150816d h1:mCHDtjsvDS0I2rTMCxVRXQ5uhwWy2/EXjLYOSd+NZLg=
forge.chapril.org/losyme/kvfmt v0.0.0-20220410175310-5d426150816d/go.mod h1:K+5LaxyRwTlG/G7prpiyla6FwJVwB2k2ty6Qv4EzvIo=
forge.chapril.org/losyme/roundrobin v0.0.0-20220409170047-3bc78a848535 h1:zpFGjrZgMHU90HKlR/7T9P0zcgm9oy52YgYUKfR47tE=

Loading…
Cancel
Save