Exemple #1
0
Fichier : db.go Projet : elos/gaia
func (db *DB) deleteRecord(r data.Record) error {
	url := db.recordURL(url.Values{
		"kind": []string{r.Kind().String()},
		"id":   []string{r.ID().String()},
	})

	resp, err := db.deleteReq(url)
	if err != nil {
		log.Printf("gaia.(*DB).save Error: while making request: %s", err)
		return data.ErrNoConnection
	}

	switch resp.StatusCode {
	case http.StatusBadRequest:
		log.Print("gaia.(*DB).deleteRecord Error: malformed request")
		return data.ErrNoConnection
	case http.StatusInternalServerError:
		return data.ErrNoConnection
	case http.StatusUnauthorized:
		return data.ErrAccessDenial
	case http.StatusNotFound:
		return data.ErrNotFound
	case http.StatusNoContent:
		// pass, there is nothing to do, the delete has succeeded
	default:
		log.Printf("gaia.(*DB).deleteRecord Error: unexpected status code: %d", resp.StatusCode)
		return data.ErrNoConnection
	}

	return nil
}
Exemple #2
0
Fichier : db.go Projet : elos/gaia
func (db *DB) PopulateByField(field string, value interface{}, r data.Record) error {
	iter, err := db.query(&query{kind: r.Kind(), attrs: data.AttrMap{
		field: value,
	}})

	if err != nil {
		return err
	}

	if !iter.Next(r) {
		return data.ErrNotFound
	}

	return iter.Close()
}
Exemple #3
0
Fichier : db.go Projet : elos/gaia
func (db *DB) PopulateByID(r data.Record) error {
	params := url.Values{}
	params.Set("kind", r.Kind().String())
	params.Set("id", r.ID().String())
	url := db.recordURL(params)

	resp, err := db.get(url)

	if err != nil {
		return err
	}

	switch resp.StatusCode {
	case http.StatusBadRequest:
		log.Print("gaia.(*DB).PopulateByID Bad Request Error")
		fallthrough
	case http.StatusInternalServerError:
		log.Print("gaia.(*DB).PopulateByID Internal Server Error")
		fallthrough
	case http.StatusUnauthorized:
		log.Print("gaia.(*DB).PopulateByID Unauthorized Server Error")
		fallthrough
	case -1: // catch
		return data.ErrAccessDenial
	case http.StatusNotFound:
		return data.ErrNotFound
	case http.StatusCreated:
	case http.StatusOK:
		defer resp.Body.Close()
		body, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			return err
		}

		return json.Unmarshal(body, r)
	default:
		log.Printf("Unexpected status code: %d", resp.StatusCode)
		return data.ErrNoConnection
	}

	return nil
}
Exemple #4
0
Fichier : db.go Projet : elos/gaia
func (db *DB) save(r data.Record) error {
	// setup Params
	url := db.recordURL(url.Values{
		"kind": []string{r.Kind().String()},
		"id":   []string{r.ID().String()},
	})

	resp, err := db.postJSON(url, r)
	if err != nil {
		log.Printf("gaia.(*DB).save Error: while making request: %s", err)
		return data.ErrNoConnection
	}

	switch resp.StatusCode {
	case http.StatusBadRequest:
		log.Print("gaia.(*DB).save Error: malformed request")
		return data.ErrNoConnection
	case http.StatusInternalServerError:
		return data.ErrNoConnection
	case http.StatusUnauthorized:
		return data.ErrAccessDenial
	case http.StatusCreated:
		fallthrough
	case http.StatusOK:
		defer resp.Body.Close()
		body, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			log.Printf("gaia.(*DB).save Error: reading response body: %s", err)
			return data.ErrNoConnection
		}

		if err := json.Unmarshal(body, r); err != nil {
			log.Printf("gaia.(*DB).save Error: unmarshalling JSON into record: %s", err)
			return data.ErrNoConnection
		}
	default:
		log.Printf("gaia.(*DB).save Error: unexpected status code: %d", resp.StatusCode)
		return data.ErrNoConnection
	}

	return nil
}