コード例 #1
0
ファイル: methods.go プロジェクト: songguang/ironcli
// CodePackageUpload uploads a code package
func (w *Worker) CodePackageUpload(code Code) (id string, err error) {
	client := http.Client{}

	body := &bytes.Buffer{}
	mWriter := multipart.NewWriter(body)

	// write meta-data
	mMetaWriter, err := mWriter.CreateFormField("data")
	if err != nil {
		return
	}
	jEncoder := json.NewEncoder(mMetaWriter)
	err = jEncoder.Encode(map[string]interface{}{
		"name":            code.Name,
		"runtime":         code.Runtime,
		"file_name":       code.FileName,
		"config":          code.Config,
		"max_concurrency": code.MaxConcurrency,
		"retries":         code.Retries,
		"retries_delay":   code.RetriesDelay,
	})
	if err != nil {
		return
	}

	// write the zip
	mFileWriter, err := mWriter.CreateFormFile("file", "worker.zip")
	if err != nil {
		return
	}
	zWriter := zip.NewWriter(mFileWriter)

	for sourcePath, sourceText := range code.Source {
		fWriter, err := zWriter.Create(sourcePath)
		if err != nil {
			return "", err
		}
		fWriter.Write([]byte(sourceText))
	}

	zWriter.Close()

	// done with multipart
	mWriter.Close()

	req, err := http.NewRequest("POST", w.codes().URL.String(), body)
	if err != nil {
		return
	}

	req.Header.Set("Accept", "application/json")
	req.Header.Set("Accept-Encoding", "gzip/deflate")
	req.Header.Set("Authorization", "OAuth "+w.Settings.Token)
	req.Header.Set("Content-Type", mWriter.FormDataContentType())
	req.Header.Set("User-Agent", w.Settings.UserAgent)

	// dumpRequest(req) NOTE: never do this here, it breaks stuff
	response, err := client.Do(req)
	if err != nil {
		return
	}
	if err = api.ResponseAsError(response); err != nil {
		return
	}

	// dumpResponse(response)

	data := struct {
		Id         string `json:"id"`
		Msg        string `json:"msg"`
		StatusCode int    `json:"status_code"`
	}{}
	err = json.NewDecoder(response.Body).Decode(&data)
	if err != nil {
		return
	}

	return data.Id, err
}
コード例 #2
0
ファイル: worker.go プロジェクト: songguang/ironcli
// create code package (zip) from parsed .worker info
func pushCodes(zipName string, w *worker.Worker, args worker.Code) (id string, err error) {
	// TODO i don't get why i can't write from disk to wire, but I give up
	var body bytes.Buffer
	mWriter := multipart.NewWriter(&body)
	mMetaWriter, err := mWriter.CreateFormField("data")
	if err != nil {
		return "", err
	}

	jEncoder := json.NewEncoder(mMetaWriter)
	if err := jEncoder.Encode(args); err != nil {
		return "", err
	}

	if zipName != "" {
		r, err := zip.OpenReader(zipName)
		if err != nil {
			return "", err
		}
		defer r.Close()

		mFileWriter, err := mWriter.CreateFormFile("file", "worker.zip")
		if err != nil {
			return "", err
		}
		zWriter := zip.NewWriter(mFileWriter)

		for _, f := range r.File {
			fWriter, err := zWriter.Create(f.Name)
			if err != nil {
				return "", err
			}
			rc, err := f.Open()
			if err != nil {
				return "", err
			}
			_, err = io.Copy(fWriter, rc)
			rc.Close()
			if err != nil {
				return "", err
			}
		}

		zWriter.Close()
	}
	mWriter.Close()

	req, err := http.NewRequest("POST", api.Action(w.Settings, "codes").URL.String(), &body)
	if err != nil {
		return "", err
	}

	req.Header.Set("Accept", "application/json")
	req.Header.Set("Accept-Encoding", "gzip/deflate")
	req.Header.Set("Authorization", "OAuth "+w.Settings.Token)
	req.Header.Set("Content-Type", mWriter.FormDataContentType())
	req.Header.Set("User-Agent", w.Settings.UserAgent)

	response, err := http.DefaultClient.Do(req)
	if err != nil {
		return "", err
	}

	if err = api.ResponseAsError(response); err != nil {
		return "", err
	}

	var data struct {
		Id string `json:"id"`
	}
	err = json.NewDecoder(response.Body).Decode(&data)
	return data.Id, err
}