From 3e15d6d3c032b3680071779471770ff0f5acb0ec Mon Sep 17 00:00:00 2001
From: Christian Pointner <equinox@helsinki.at>
Date: Sat, 12 Dec 2015 04:18:59 +0100
Subject: imporved result reporting


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)
-- 
cgit v0.10.2