# Collection of code snippets by Arne Vajhøj 
# (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
import urllib.request

def http(url, body):
    req = urllib.request.Request(url, headers={ 'Accept': 'application/json', 'Content-Type': 'application/json'}, data= bytes(body, 'UTF-8'))
    rawresp = urllib.request.urlopen(req)

def create(id, names, moves):
    return '{ "id": %d, "names": "%s", "moves": "%s" }' % (id, names, moves)
    
http('http://localhost:8888/solr/chessopening/update?commit=true', '{ "delete": { "query": "*:*" } }')
http('http://localhost:8888/solr/chessopening/update/json/docs?commit=true', create(1, "Spanish defense, Ruy Lopez", "1. e4, e5, 2. Nf3, Nc6, 3. Bb5,"))
http('http://localhost:8888/solr/chessopening/update/json/docs?commit=true', create(2, "French defense", "1. e4, e6, 2. d4, d5,"))
http('http://localhost:8888/solr/chessopening/update/json/docs?commit=true', create(3, "Sicilian defense", "1. e4, c5,"))
http('http://localhost:8888/solr/chessopening/update/json/docs?commit=true', create(4, "Queens gambit", "1. d4, d5, 2. c4,"))
http('http://localhost:8888/solr/chessopening/update/json/docs?commit=true', create(5, "Dutch defense", "d4, f5,"))
http('http://localhost:8888/solr/chessopening/update/json/docs?commit=true', create(6, "Reti opening", "Nf3, d5, 2. c4,"))
