1
0
mirror of https://github.com/rclone/rclone.git synced 2025-12-16 00:04:40 +00:00

rc: add NewJobFromBytes for reading jobs from non HTTP transactions

This commit is contained in:
Nick Craig-Wood
2025-07-24 15:00:37 +01:00
parent 0701dd55cd
commit 77faa787e1
2 changed files with 80 additions and 0 deletions

View File

@@ -2,7 +2,9 @@
package jobs package jobs
import ( import (
"bytes"
"context" "context"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"net/http" "net/http"
@@ -532,6 +534,32 @@ func NewJobFromParams(ctx context.Context, in rc.Params) (out rc.Params) {
return out return out
} }
// NewJobFromBytes creates an rc job from a JSON blob as bytes.
//
// The JSON blob should contain a _path entry.
//
// It returns a JSON blob as output which may be an error.
func NewJobFromBytes(ctx context.Context, inBuf []byte) (outBuf []byte) {
var in rc.Params
var out rc.Params
// Parse a JSON blob from the input
err := json.Unmarshal(inBuf, &in)
if err != nil {
out, _ = rc.Error("unknown", in, err, http.StatusBadRequest)
} else {
out = NewJobFromParams(ctx, in)
}
var w bytes.Buffer
err = rc.WriteJSON(&w, out)
if err != nil {
fs.Errorf(nil, "rc: NewJobFromBytes: failed to write JSON output: %v", err)
return []byte(`{"error":"failed to write JSON output"}`)
}
return w.Bytes()
}
func init() { func init() {
rc.Add(rc.Call{ rc.Add(rc.Call{
Path: "job/batch", Path: "job/batch",

View File

@@ -702,6 +702,58 @@ func TestNewJobFromParams(t *testing.T) {
} }
} }
func TestNewJobFromBytes(t *testing.T) {
ctx := context.Background()
for _, test := range []struct {
in string
want string
}{{
in: `{
"_path": "rc/noop",
"a": "potato"
}`,
want: `{
"a": "potato"
}
`,
}, {
in: `{
"_path": "rc/error",
"e": "sausage"
}`,
want: `{
"error": "arbitrary error on input map[e:sausage]",
"input": {
"e": "sausage"
},
"path": "rc/error",
"status": 500
}
`,
}, {
in: `parse error`,
want: `{
"error": "invalid character 'p' looking for beginning of value",
"input": null,
"path": "unknown",
"status": 400
}
`,
}, {
in: `"just a string"`,
want: `{
"error": "json: cannot unmarshal string into Go value of type rc.Params",
"input": null,
"path": "unknown",
"status": 400
}
`,
}} {
got := NewJobFromBytes(ctx, []byte(test.in))
assert.Equal(t, test.want, string(got))
}
}
func TestJobsBatch(t *testing.T) { func TestJobsBatch(t *testing.T) {
ctx := context.Background() ctx := context.Background()