import { IncomingMessage, request } from "http";
import { Database } from "./LocalDatabase";

export function request_data(id: string, self: &Database): void {
    let url: string = 'http://192.168.178.34:8082/getBulk/' + id;
    let data: string = "";

    let req = request(url, (res: IncomingMessage) => {
        res.setEncoding('utf8');

        res.on('data', (chunk) => {
          data += chunk;
        });
        
        res.on('end', () => {
          let d = JSON.parse(data);
          d.forEach(element => {
            self.add(element.id, element.val)
          });
        });
    });

    req.on('error', (e) => {
        console.error(`problem with request: ${e.message}`);
      });
    req.end();
}