Esempio n. 1
0
/**
 * Roep de wordpress API aan
 *
 * @author A. Glansbeek en P. Kompier
 * @params *goweb.Context Context van goweb
 * @params interface De struct waarin json opgeslagen wordt
 * @params string functie naam van API wordpress
 * @params map[string]string map waarin params staan
 */
func CallWordpressApi(cx *goweb.Context, v interface{}, callfunction string, params map[string]string) {
	var contents []uint8

	url := createURL(callfunction, params)
	c := appengine.NewContext(cx.GetRequest())
	client := urlfetch.Client(c)
	response, err := client.Get(url)

	u := user.Current(c)
	var name string
	if u == nil {
		name = "Gast"
	} else {
		name = u.String()
	}

	data.SaveLog(url, name, cx.GetRequest())

	// Error check
	if err != nil {
		cx.RespondWithErrorMessage("Kan geen verbinding maken met de wordpress webservice", http.StatusBadRequest)
		return
	} else {
		// Aan het einde van deze functie sluit response
		defer response.Body.Close()

		// Maak van de content bytes
		contents, err = ioutil.ReadAll(response.Body)

		// Error check
		if err != nil {
			cx.RespondWithErrorMessage("Kan response niet lezen", http.StatusBadRequest)
			return
		}
	}

	err = json.Unmarshal(contents, v)

	// Error check
	if err != nil {
		cx.RespondWithErrorMessage("Kan json niet parsen err: "+err.String(), http.StatusBadRequest)
		return
	}
}