diff options
-rw-r--r-- | importer.go | 52 |
1 files changed, 36 insertions, 16 deletions
diff --git a/importer.go b/importer.go index bbe4874..55fbe62 100644 --- a/importer.go +++ b/importer.go @@ -25,7 +25,9 @@ package rhimport import ( + "bufio" "bytes" + "encoding/xml" "fmt" "github.com/golang-basic/go-curl" "io" @@ -115,7 +117,23 @@ func (ctx *ImportContext) getShowInfo() (err error) { return } -func add_cart(ctx *ImportContext) (err error) { +type RDWebResult struct { + ResponseCode int `xml:"ResponseCode"` + ErrorString string `xml:"ErrorString"` + AudioConvertError string `xml:"AudioConvertError"` +} + +func decodeRDWebResult(data io.Reader) (result *RDWebResult, err error) { + decoder := xml.NewDecoder(data) + result = &RDWebResult{} + if xmlerr := decoder.Decode(result); xmlerr != nil { + err = fmt.Errorf("Error parsing XML response: %s", xmlerr) + return + } + return +} + +func add_cart(ctx *ImportContext) (result *RDWebResult, err error) { var b bytes.Buffer w := multipart.NewWriter(&b) @@ -141,9 +159,10 @@ func add_cart(ctx *ImportContext) (err error) { } w.Close() return send_post_request(ctx.Config.RDXportEndpoint, &b, w.FormDataContentType()) + // TODO: on success this returns <cartAdd>...</cartAdd> } -func add_cut(ctx *ImportContext) (err error) { +func add_cut(ctx *ImportContext) (result *RDWebResult, err error) { var b bytes.Buffer w := multipart.NewWriter(&b) @@ -161,9 +180,10 @@ func add_cut(ctx *ImportContext) (err error) { } w.Close() return send_post_request(ctx.Config.RDXportEndpoint, &b, w.FormDataContentType()) + // TODO: on success this returns <cutAdd>...</cutAdd> } -func remove_cart(ctx *ImportContext) (err error) { +func remove_cart(ctx *ImportContext) (result *RDWebResult, err error) { var b bytes.Buffer w := multipart.NewWriter(&b) @@ -183,7 +203,7 @@ func remove_cart(ctx *ImportContext) (err error) { return send_post_request(ctx.Config.RDXportEndpoint, &b, w.FormDataContentType()) } -func send_post_request(url string, b *bytes.Buffer, contenttype string) (err error) { +func send_post_request(url string, b *bytes.Buffer, contenttype string) (result *RDWebResult, err error) { var req *http.Request if req, err = http.NewRequest("POST", url, b); err != nil { return @@ -199,9 +219,8 @@ func send_post_request(url string, b *bytes.Buffer, contenttype string) (err err } defer res.Body.Close() - fmt.Printf("Response Code: %d\nBody:\n", res.StatusCode) - io.Copy(os.Stdout, res.Body) - return + // TODO: some commands might return something else... + return decodeRDWebResult(res.Body) } func import_audio_create_request(ctx *ImportContext, easy *curl.CURL) (form *curl.Form, err error) { @@ -241,7 +260,7 @@ func import_audio_create_request(ctx *ImportContext, easy *curl.CURL) (form *cur return } -func import_audio(ctx *ImportContext) (err error) { +func import_audio(ctx *ImportContext) (result *RDWebResult, err error) { easy := curl.EasyInit() if easy != nil { @@ -279,12 +298,11 @@ func import_audio(ctx *ImportContext) (err error) { return } - var status_code interface{} - if status_code, err = easy.Getinfo(curl.INFO_RESPONSE_CODE); err != nil { - return - } - fmt.Printf("Response Code: %d\nBody:\n", status_code.(int)) - resbody.WriteTo(os.Stdout) + // var status_code interface{} + // if status_code, err = easy.Getinfo(curl.INFO_RESPONSE_CODE); err != nil { + // return + // } + return decodeRDWebResult(bufio.NewReader(&resbody)) } else { err = fmt.Errorf("Error initializing libcurl") } @@ -324,14 +342,16 @@ func ImportFile(ctx *ImportContext) (err error) { } } - if err = import_audio(ctx); err != nil { + var result *RDWebResult + if result, err = import_audio(ctx); err != nil { return } - if ctx.ProgressCallBack != nil { ctx.ProgressCallBack(2, "importing", 1.0, ctx.ProgressCallBackData) } + rhdl.Printf("StatusCode: %d, ErrorString: '%s', AudioConverterError: %s\n", result.ResponseCode, result.ErrorString, result.AudioConvertError) + rhl.Println("ImportFile succesfully imported", ctx.SourceFile) return } |