<commit_before>import os
import sys
import csv
import json
import jsonschema
import requests
from pyelasticsearch import ElasticSearch
import xlrd
import xlwt
from base64 import b64encode
import gdata
import gdata.spreadsheet.service


# set headers.  UNCLEAR IF THIS IS USED PROPERLY
HEADERS = {'content-type': 'application/json'}

def get_ENCODE(obj_id,keys):
    '''GET an ENCODE object as JSON and return as dict'''
    url = keys['server']+obj_id+'?limit=all'
    response = requests.get(url, auth=(keys['authid'],keys['authpw']), headers=HEADERS)
    if not response.status_code == 200:
        print >> sys.stderr, response.text
    return response.json()

def GetENCODE(object_id,keys):
    '''GET an ENCODE object as JSON and return as dict'''
    if type(object_id) is str:
        url = keys['server']+object_id+'?limit=all'
        #print(url)
        try:
            response = requests.get(url,auth=(keys['authid'],keys['authpw']), headers=HEADERS)
            if not response.status_code == 200:
                print >> sys.stderr, response.text
        # no
        except Exception as e:
            print("Get request failed:")
            #print(e)
        # yes
        else:
            return response.json()

def patch_ENCODE(obj_id,patch_json,keys):
    '''PATCH an existing ENCODE object and return the response JSON'''
    url = keys['server']+obj_id
    json_payload = json.dumps(patch_json)
    response = requests.patch(url, auth=(keys['authid'],keys['authpw']), data=json_payload)
    print "Patch:"
    print response.status_code
    if not response.status_code == 200:
        print >> sys.stderr, response.text
    return response.json()

def replace_ENCODE(obj_id,put_json,keys):
    '''PUT an existing ENCODE object and return the response JSON
    '''
#    if isinstance(put_json, dict):
    json_payload = json.dumps(put_json)
#    elif isinstance(put_json, basestring):
#        json_payload = put_json
#    else:
#        print >> sys.stderr, 'Datatype to put is not string or dict.'
    url = keys['server']+obj_id
    response = requests.put(url, auth=(keys['authid'],keys['authpw']), data=json_payload)
    print json.dumps(response.json(), indent=4, separators=(',', ': '))    
    if not response.status_code == 200:
        print >> sys.stderr, response.text
    print response.text
    return response.json()

def new_ENCODE(collection_id, object_json,keys):
    '''POST an ENCODE object as JSON and return the resppnse JSON'''
    url = keys['server'] +'/'+collection_id+'/'
    json_payload = json.dumps(object_json)
    response = requests.post(url, auth=(keys['authid'],keys['authpw']), headers=HEADERS, data=json_payload)
    print(response.status_code)
    if not response.status_code == 201:
        print >> sys.stderr, response.text
    return response.json()

def KeyENCODE(key_file,user_name,server_name):
    '''
    get keys from file
    '''
    key_open = open(key_file)<commit_msg>Headers added to put and patch<commit_after>import os
import sys
import csv
import json
import jsonschema
import requests
from pyelasticsearch import ElasticSearch
import xlrd
import xlwt
from base64 import b64encode
import gdata
import gdata.spreadsheet.service


# set headers.  UNCLEAR IF THIS IS USED PROPERLY
HEADERS = {'content-type': 'application/json'}

def get_ENCODE(obj_id,keys):
    '''GET an ENCODE object as JSON and return as dict'''
    url = keys['server']+obj_id+'?limit=all'
    response = requests.get(url, auth=(keys['authid'],keys['authpw']), headers=HEADERS)
    if not response.status_code == 200:
        print >> sys.stderr, response.text
    return response.json()

def GetENCODE(object_id,keys):
    '''GET an ENCODE object as JSON and return as dict'''
    if type(object_id) is str:
        url = keys['server']+object_id+'?limit=all'
        #print(url)
        try:
            response = requests.get(url,auth=(keys['authid'],keys['authpw']), headers=HEADERS)
            if not response.status_code == 200:
                print >> sys.stderr, response.text
        # no
        except Exception as e:
            print("Get request failed:")
            #print(e)
        # yes
        else:
            return response.json()

def patch_ENCODE(obj_id,patch_json,keys):
    '''PATCH an existing ENCODE object and return the response JSON'''
    url = keys['server']+obj_id
    json_payload = json.dumps(patch_json)
    response = requests.patch(url, auth=(keys['authid'],keys['authpw']), data=json_payload)
    response = requests.patch(url, auth=(keys['authid'],keys['authpw']), headers=HEADERS, data=json_payload)
    print "Patch:"
    print response.status_code
    if not response.status_code == 200:
        print >> sys.stderr, response.text
    return response.json()

def replace_ENCODE(obj_id,put_json,keys):
    '''PUT an existing ENCODE object and return the response JSON
    '''
#    if isinstance(put_json, dict):
    json_payload = json.dumps(put_json)
#    elif isinstance(put_json, basestring):
#        json_payload = put_json
#    else:
#        print >> sys.stderr, 'Datatype to put is not string or dict.'
    url = keys['server']+obj_id
    response = requests.put(url, auth=(keys['authid'],keys['authpw']), data=json_payload)
    response = requests.put(url, auth=(keys['authid'],keys['authpw']), headers=HEADERS, data=json_payload)
    print json.dumps(response.json(), indent=4, separators=(',', ': '))    
    if not response.status_code == 200:
        print >> sys.stderr, response.text
    print response.text
    return response.json()

def new_ENCODE(collection_id, object_json,keys):
    '''POST an ENCODE object as JSON and return the resppnse JSON'''
    url = keys['server'] +'/'+collection_id+'/'
    json_payload = json.dumps(object_json)
    response = requests.post(url, auth=(keys['authid'],keys['authpw']), headers=HEADERS, data=json_payload)
    print(response.status_code)
    if not response.status_code == 201:
        print >> sys.stderr, response.text
    return response.json()

def KeyENCODE(key_file,user_name,server_name):
    '''
    get keys from file
    '''
    key_open = open(key_file)
{'fraction_non_alphanumeric': 0.12738853503184713, 'fraction_numerical': 0.006532745386248571, 'mean_word_length': 3.549777117384844, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1721956', 'n_tokens_mistral': 1914, 'n_tokens_neox': 1870, 'n_words': 490}