diff options
author | Christian Pointner <equinox@helsinki.at> | 2015-12-10 16:59:30 (GMT) |
---|---|---|
committer | Christian Pointner <equinox@helsinki.at> | 2015-12-10 16:59:30 (GMT) |
commit | a454be72ed140ff2bf3e4338794885cde5fb9f58 (patch) | |
tree | 3d91f3c579e91eaf668ff6bf3dc383b037d08f92 /src/helsinki.at/rhimport/importer.go | |
parent | fa921c13ae7d2fb82ab5801244b6b1e9ef7db371 (diff) |
added add_cart function
Diffstat (limited to 'src/helsinki.at/rhimport/importer.go')
-rw-r--r-- | src/helsinki.at/rhimport/importer.go | 70 |
1 files changed, 52 insertions, 18 deletions
diff --git a/src/helsinki.at/rhimport/importer.go b/src/helsinki.at/rhimport/importer.go index 68b71a6..c7b1a66 100644 --- a/src/helsinki.at/rhimport/importer.go +++ b/src/helsinki.at/rhimport/importer.go @@ -114,6 +114,57 @@ func (ctx *ImportContext) getShowInfo() (err error) { return } +func send_post_request(url string, b *bytes.Buffer, contenttype string) (err error) { + var req *http.Request + if req, err = http.NewRequest("POST", url, b); err != nil { + return + } + if contenttype != "" { + req.Header.Set("Content-Type", contenttype) + } + + client := &http.Client{} + var res *http.Response + if res, err = client.Do(req); err != nil { + return + } + defer res.Body.Close() + + if res.StatusCode != http.StatusOK { + // TODO: better error output + err = fmt.Errorf("bad status: %s", res.Status) + } + return +} + +func add_cart(ctx *ImportContext) (err error) { + var b bytes.Buffer + w := multipart.NewWriter(&b) + + if err = w.WriteField("COMMAND", "12"); err != nil { + return + } + if err = w.WriteField("LOGIN_NAME", ctx.UserName); err != nil { + return + } + if err = w.WriteField("PASSWORD", ctx.Password); err != nil { + return + } + if err = w.WriteField("GRPOUP_NAME", ctx.GroupName); err != nil { + return + } + if err = w.WriteField("TYPE", "audio"); err != nil { + return + } + if ctx.Cart != 0 { + if err = w.WriteField("CART_NUMBER", fmt.Sprintf("%d", ctx.Cart)); err != nil { + return + } + } + w.Close() + return send_post_request(ctx.Config.RDXportEndpoint, &b, w.FormDataContentType()) +} + func import_audio(ctx *ImportContext) (err error) { var b bytes.Buffer w := multipart.NewWriter(&b) @@ -160,24 +211,7 @@ func import_audio(ctx *ImportContext) (err error) { f.Close() w.Close() - var req *http.Request - if req, err = http.NewRequest("POST", ctx.Config.RDXportEndpoint, &b); err != nil { - return - } - req.Header.Set("Content-Type", w.FormDataContentType()) - - client := &http.Client{} - var res *http.Response - if res, err = client.Do(req); err != nil { - return - } - defer res.Body.Close() - - if res.StatusCode != http.StatusOK { - // TODO: better error output - err = fmt.Errorf("bad status: %s", res.Status) - } - return + return send_post_request(ctx.Config.RDXportEndpoint, &b, w.FormDataContentType()) } func cleanup_files(ctx *ImportContext) { |