func Get_Data(query Query, key string) MatchData { //Get the query URL query_url := queryBuild(query, key) fmt.Println(query_url) //Request the JSON and wait for the response res, err := http.Get(query_url) //handle any errors that may occur errorhandler.Perror(err) //Read the JSON response matchDataFromHttp, err := ioutil.ReadAll(res.Body) //Handle all errors again errorhandler.Perror(err) //Store the json into the result struct called match data var matchData MatchData err = json.Unmarshal([]byte(matchDataFromHttp), &matchData) //Handle Errors Again errorhandler.Perror(err) return matchData }
func main() { //First get the API Key. You can't get the data without it. api_key, err := key.Get("./api_key.dat") errorhandler.Perror(err) //Just testing the query builder var opts players.Query //opts.Account_ID = "220083814"//"76561198180349542" opts.Match_ID = "2107663947" opts.Account_ID = "220083814" t := players.Get_Data(opts, api_key) fmt.Println(t) }
func Get(path string) (key string, err error) { // Read the file and panic if there's an error content, err := ioutil.ReadFile(path) errorhandler.Perror(err) //File shouldn't be empty if string(content) == "" { return "", errors.New("The file was not ready, or the file was empty") } //if the contents are a valid key, return the contents if keyValidator(string(content)) { return string(content), nil } else { panic(errors.New("Not a valid API Key")) } }
func queryBuild(options Query, key string) string { base_query := "https://api.steampowered.com/IDOTA2Match_570/GetMatchDetails/V001/?" //Create a string buffer to append strings based on options var query bytes.Buffer //Add the base string at the first part of the query string query.WriteString(base_query) //Create a switch to build the query if options.Player_Name != "" { query.WriteString("player_name=") query.WriteString(options.Player_Name) query.WriteString("&") } if options.Hero_ID != "" { query.WriteString("hero_id=") query.WriteString(options.Hero_ID) query.WriteString("&") } if options.Match_ID != "" { query.WriteString("match_id=") query.WriteString(options.Match_ID) query.WriteString("&") } if options.Skill != "" { query.WriteString("skill=") //Handle incorrect numbers or characters s_num, err := strconv.Atoi(options.Skill) errorhandler.Perror(err) if s_num > 3 || s_num < 1 { err = errors.New("Number of matches out of range (x > 0 and x <=25)") panic(err) } query.WriteString(options.Skill) query.WriteString("&") } if options.Date_Min != "" { query.WriteString("date_min=") query.WriteString(options.Date_Min) query.WriteString("&") } if options.Date_Max != "" { query.WriteString("date_max=") query.WriteString(options.Date_Max) query.WriteString("&") } if options.Account_ID != "" { query.WriteString("account_id=") //create a variable to hold the account number while it processes var a_string string //turn the ID into an integer for error handling a_num, err := strconv.Atoi(options.Account_ID) errorhandler.Perror(err) if a_num < 4294967295 { fmt.Println("The ID may be 32 bit. It will go through the conversion process") a_string = strconv.Itoa(get64BitID(a_num)) } else { fmt.Println("The ID is 64bit. No Conversion Neccessary") a_string = options.Account_ID } query.WriteString(a_string) query.WriteString("&") } if options.League_ID != "" { query.WriteString("league_id=") query.WriteString(options.League_ID) query.WriteString("&") } if options.Start_At_Math_ID != "" { query.WriteString("start_at_martch_id=") query.WriteString(options.Start_At_Math_ID) query.WriteString("&") } if options.Matches_Requested != "" { query.WriteString("matches_requested=") m_num, err := strconv.Atoi(options.Matches_Requested) //Handle incorrect numbers or characters if err != nil { panic(err) } if m_num > 25 || m_num < 1 { err = errors.New("Number of matches out of range (x > 0 and x <=25)") panic(err) } query.WriteString(options.Matches_Requested) query.WriteString("&") } //add the key at the end of the query query.WriteString("key=") query.WriteString(key) //return the query url return query.String() }