diff options
author | Christian Pointner <equinox@helsinki.at> | 2015-12-12 03:18:59 (GMT) |
---|---|---|
committer | Christian Pointner <equinox@helsinki.at> | 2015-12-12 03:18:59 (GMT) |
commit | 3e15d6d3c032b3680071779471770ff0f5acb0ec (patch) | |
tree | f906113264e319648368aa986f3335976c90e46e /src/helsinki.at/rhimport | |
parent | aa4a870d1df23af1c26d5b675c278a08a1b8d2ec (diff) |
imporved result reporting
Diffstat (limited to 'src/helsinki.at/rhimport')
-rw-r--r-- | src/helsinki.at/rhimport/importer.go | 74 |
1 files changed, 54 insertions, 20 deletions
diff --git a/src/helsinki.at/rhimport/importer.go b/src/helsinki.at/rhimport/importer.go index 7c85fdd..505fffd 100644 --- a/src/helsinki.at/rhimport/importer.go +++ b/src/helsinki.at/rhimport/importer.go @@ -122,17 +122,15 @@ type ImportResult struct { Cut int } -func NewImportResult(rdres *RDWebResult) *ImportResult { - res := new(ImportResult) - res.ResponseCode = rdres.ResponseCode - res.ErrorString = rdres.ErrorString +func (self *ImportResult) fromRDWebResult(rdres *RDWebResult) { + self.ResponseCode = rdres.ResponseCode + self.ErrorString = rdres.ErrorString if rdres.AudioConvertError != 0 { - res.ErrorString += fmt.Sprint(", Audio Convert Error: %d", rdres.AudioConvertError) + self.ErrorString += fmt.Sprint(", Audio Convert Error: %d", rdres.AudioConvertError) } - return res } -func add_cart(ctx *ImportContext) (result interface{}, err error) { +func add_cart(ctx *ImportContext, result *ImportResult) (err error) { var b bytes.Buffer w := multipart.NewWriter(&b) @@ -165,12 +163,25 @@ func add_cart(ctx *ImportContext) (result interface{}, err error) { defer res.Body.Close() if res.StatusCode != http.StatusOK { - return NewRDWebResultFromXML(res.Body) + var rdres *RDWebResult + if rdres, err = NewRDWebResultFromXML(res.Body); err != nil { + return + } + result.fromRDWebResult(rdres) + return } - return NewRDCartAddFromXML(res.Body) + var cartadd *RDCartAdd + if cartadd, err = NewRDCartAddFromXML(res.Body); err != nil { + return + } + result.ResponseCode = res.StatusCode + result.ErrorString = "OK" + result.Cart = cartadd.Carts[0].Number + ctx.Cart = result.Cart + return } -func add_cut(ctx *ImportContext) (result interface{}, err error) { +func add_cut(ctx *ImportContext, result *ImportResult) (err error) { var b bytes.Buffer w := multipart.NewWriter(&b) @@ -195,12 +206,25 @@ func add_cut(ctx *ImportContext) (result interface{}, err error) { defer res.Body.Close() if res.StatusCode != http.StatusOK { - return NewRDWebResultFromXML(res.Body) + var rdres *RDWebResult + if rdres, err = NewRDWebResultFromXML(res.Body); err != nil { + return + } + result.fromRDWebResult(rdres) + return + } + var cutadd *RDCutAdd + if cutadd, err = NewRDCutAddFromXML(res.Body); err != nil { + return } - return NewRDCutAddFromXML(res.Body) + result.ResponseCode = res.StatusCode + result.ErrorString = "OK" + result.Cut = cutadd.Cuts[0].Number + ctx.Cut = cutadd.Cuts[0].Number + return } -func remove_cart(ctx *ImportContext) (result *RDWebResult, err error) { +func remove_cart(ctx *ImportContext, result *ImportResult) (err error) { var b bytes.Buffer w := multipart.NewWriter(&b) @@ -223,7 +247,13 @@ func remove_cart(ctx *ImportContext) (result *RDWebResult, err error) { return } defer res.Body.Close() - return NewRDWebResultFromXML(res.Body) + + var rdres *RDWebResult + if rdres, err = NewRDWebResultFromXML(res.Body); err != nil { + return + } + result.fromRDWebResult(rdres) + return } func send_post_request(url string, b *bytes.Buffer, contenttype string) (res *http.Response, err error) { @@ -279,7 +309,7 @@ func import_audio_create_request(ctx *ImportContext, easy *curl.CURL) (form *cur return } -func import_audio(ctx *ImportContext) (result *RDWebResult, err error) { +func import_audio(ctx *ImportContext, result *ImportResult) (err error) { easy := curl.EasyInit() if easy != nil { @@ -321,7 +351,12 @@ func import_audio(ctx *ImportContext) (result *RDWebResult, err error) { // if status_code, err = easy.Getinfo(curl.INFO_RESPONSE_CODE); err != nil { // return // } - return NewRDWebResultFromXML(bufio.NewReader(&resbody)) + var rdres *RDWebResult + if rdres, err = NewRDWebResultFromXML(bufio.NewReader(&resbody)); err != nil { + return + } + result.fromRDWebResult(rdres) + return } else { err = fmt.Errorf("Error initializing libcurl") } @@ -361,16 +396,15 @@ func ImportFile(ctx *ImportContext) (res *ImportResult, err error) { } } - var rdres *RDWebResult - if rdres, err = import_audio(ctx); err != nil { + res = &ImportResult{} + if err = import_audio(ctx, res); err != nil { return } if ctx.ProgressCallBack != nil { ctx.ProgressCallBack(2, "importing", 1.0, ctx.ProgressCallBackData) } - res = NewImportResult(rdres) - rhdl.Printf("StatusCode: %d, ErrorString: '%s'\n", res.ResponseCode, res.ErrorString) + rhdl.Printf("ImportResult: %+v\n", res) if res.ResponseCode == http.StatusOK { rhl.Println("ImportFile succesfully imported", ctx.SourceFile) |