Task: Use a REST API




Go: package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
	"os"
	"strings"
	"time"
)

var key string

func init() {
	
	
	const keyFile = "api_key.txt"
	f, err := os.Open(keyFile)
	if err != nil {
		log.Fatal(err)
	}
	keydata, err := ioutil.ReadAll(f)
	if err != nil {
		log.Fatal(err)
	}
	key = strings.TrimSpace(string(keydata))
}

type EventResponse struct {
	Results []Result
	
}

type Result struct {
	ID          string
	Status      string
	Name        string
	EventURL    string `json:"event_url"`
	Description string
	Time        EventTime
	
}



type EventTime struct{ time.Time }

func (et *EventTime) UnmarshalJSON(data []byte) error {
	var msec int64
	if err := json.Unmarshal(data, &msec); err != nil {
		return err
	}
	et.Time = time.Unix(0, msec*int64(time.Millisecond))
	return nil
}

func (et EventTime) MarshalJSON() ([]byte, error) {
	msec := et.UnixNano() / int64(time.Millisecond)
	return json.Marshal(msec)
}


func (r *Result) String() string {
	var b bytes.Buffer
	fmt.Fprintln(&b, "ID:", r.ID)
	fmt.Fprintln(&b, "URL:", r.EventURL)
	fmt.Fprintln(&b, "Time:", r.Time.Format(time.UnixDate))
	d := r.Description
	const limit = 65
	if len(d) > limit {
		d = d[:limit-1] + "…"
	}
	fmt.Fprintln(&b, "Description:", d)
	return b.String()
}

func main() {
	v := url.Values{
		
		
		"topic": []string{"photo"},
		"time":  []string{",1w"},
		"key":   []string{key},
	}
	u := url.URL{
		Scheme:   "http",
		Host:     "api.meetup.com",
		Path:     "2/open_events.json",
		RawQuery: v.Encode(),
	}
	

	resp, err := http.Get(u.String())
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Body.Close()
	log.Println("HTTP Status:", resp.Status)

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatal(err)
	}

	
	var buf bytes.Buffer
	if err = json.Indent(&buf, body, "", "  "); err != nil {
		log.Fatal(err)
	}
	

	var evresp EventResponse
	json.Unmarshal(body, &evresp)
	

	fmt.Println("Got", len(evresp.Results), "events")
	if len(evresp.Results) > 0 {
		fmt.Println("First event:\n", &evresp.Results[0])
	}
}


Java: package src;

import java.io.BufferedReader;
import java.io.FileReader;
import java.net.URI;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;



public class EventGetter {
	

	String city = "";
	String topic = "";
	
	public String getEvent(String path_code,String key) throws Exception{
		String responseString = "";
		
		URI request = new URIBuilder()			
			.setScheme("http")
			.setHost("api.meetup.com")
			.setPath(path_code)
			
			.setParameter("topic", topic)
			.setParameter("city", city)
			
			.setParameter("key", key)
			.build();
		
		HttpGet get = new HttpGet(request);			
		System.out.println("Get request : "+get.toString());
		
		CloseableHttpClient client = HttpClients.createDefault();
		CloseableHttpResponse response = client.execute(get);
		responseString = EntityUtils.toString(response.getEntity());
		
		return responseString;
	}
	
	public String getApiKey(String key_path){
		String key = "";
		
		try{
			BufferedReader reader = new BufferedReader(new FileReader(key_path));	
			key = reader.readLine().toString();									
			reader.close();
		}
		catch(Exception e){System.out.println(e.toString());}
		
		return key;																
	}
	
}