Created
August 23, 2012 18:24
-
-
Save curt-labs/3439908 to your computer and use it in GitHub Desktop.
Convert catID to catTitle via CURT API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type APICategory struct { | |
Parent Category | |
Conent []Content | |
Sub_categories []Category | |
} | |
type Category struct { | |
CatID int | |
DateAdded string | |
ParentID int | |
CatTitle string | |
ShortDesc string | |
LongDesc string | |
Image string | |
IsLifestyle int | |
CodeID int | |
Sort int | |
VehicleSpecific bool | |
} | |
type Content struct { | |
isHTML bool | |
cType string | |
content string | |
} | |
func CategoryParts(w http.ResponseWriter, r *http.Request) { | |
params := r.URL.Query() | |
id := params.Get(":id") | |
var buf bytes.Buffer | |
buf.WriteString("https://api.curtmfg.com/v2/GetCategory?dataType=json&catID=") | |
buf.WriteString(id) | |
req, _ := http.NewRequest("GET", buf.String(), nil) | |
// Set up Tansport using app engines urlfetch service | |
t := &urlfetch.Transport{Context: appengine.NewContext(r)} | |
// Roundtrip our request to the api and handle response | |
r2, err := t.RoundTrip(req) | |
if err != nil { | |
return | |
} | |
if r2.StatusCode != 200 { | |
return | |
} | |
// Set up close of body data - save memory | |
defer r2.Body.Close() | |
// Parse our response data int a buffer | |
// then unmarshal the json into our Customer struct | |
buf.Reset() | |
buf.ReadFrom(r2.Body) | |
var i APICategory | |
err = json.Unmarshal(buf.Bytes(), &i) | |
title := i.Parent.CatTitle | |
if err != nil || len(title) == 0 { | |
http.Redirect(w, r, "/", http.StatusFound) | |
} else { | |
buf.Reset() | |
buf.WriteString("/#category/") | |
buf.WriteString(title) | |
http.Redirect(w, r, buf.String(), http.StatusFound) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment