Exemplo n.º 1
0
func GetFeatures(filter string) (*[]FeatureElement, error) {
	serviceApiKey, err := documentParser.GetApiKey()
	if err != nil {
		log.Fatalln(err)
	}

	client := &http.Client{}
	request, err := http.NewRequest("GET", "https://appliance.homdna.com/elements", bytes.NewReader([]byte{}))
	request.Header["Content-Type"] = []string{"application/json"}
	request.Header["X-Service-Api-Key"] = []string{*serviceApiKey}
	response, err := client.Do(request)
	if err != nil {
		return nil, err
	}
	if response.StatusCode != http.StatusOK {
		body, _ := ioutil.ReadAll(response.Body)
		fmt.Printf("Status code: %v Status: %v RESPONSE: %v", response.StatusCode, response.Status, string(body))
		return nil, errors.New("Bad Status Code")
	}

	body, err := ioutil.ReadAll(response.Body)
	if err != nil {
		return nil, err
	}

	var elementFeatures = Elements{}

	err = json.Unmarshal(body, &elementFeatures)
	if err != nil {
		return nil, err
	}

	features := make(map[string]interface{})
	for _, feature := range elementFeatures.RoomFeatures {
		features[feature.feature_name] = feature
	}

	fmt.Printf("\n\n %#v \n\n", features)

	switch {
	case filter == "lot_features":
		return &elementFeatures.LotFeatures, nil
	case filter == "exterior_features":
		return &elementFeatures.StructureFeatures, nil
	case filter == "room_features":
		return &elementFeatures.RoomFeatures, nil
	}

	return nil, errors.New("Feature type does not exist")

}
Exemplo n.º 2
0
func GetAppliances() (*map[string]string, error) {
	serviceApiKey, err := documentParser.GetApiKey()
	if err != nil {
		log.Fatalln(err)
	}

	client := &http.Client{}
	request, err := http.NewRequest("GET", "https://appliance.homdna.com/appliances", bytes.NewReader([]byte{}))
	request.Header["Content-Type"] = []string{"application/json"}
	request.Header["X-Service-Api-Key"] = []string{*serviceApiKey}
	response, err := client.Do(request)
	if err != nil {
		return nil, err
	}
	if response.StatusCode != http.StatusOK {
		body, _ := ioutil.ReadAll(response.Body)
		fmt.Printf("Status code: %v Status: %v RESPONSE: %v", response.StatusCode, response.Status, string(body))
		return nil, errors.New("Bad Status Code")
	}

	body, err := ioutil.ReadAll(response.Body)
	if err != nil {
		return nil, err
	}

	parsedAppliances := []ApplianceElement{}
	err = json.Unmarshal(body, &parsedAppliances)
	if err != nil {
		return nil, err
	}

	appliances := make(map[string]string)
	for _, appliance := range parsedAppliances {
		if appliance.Brand.Name == "other" {
			appliances[appliance.Type.Name] = appliance.ApplianceId
		}
	}

	return &appliances, nil
}