<commit_before>'''
@author: jmc, mvi
'''

import json
import urllib2
import urllib
import requests

# TODO tags

class CkanAPIWrapper(): 
    '''
    This class uses API interface to return, create, update, delete, search for datasets
    or dataset resources 
    '''
    
    def __init__(self, url, api_key):
        assert url
        self.url = url
        self.api_key = api_key
        
        
    def _send_request(self, data_string, url):
        assert url
        request = urllib2.Request(url)
        # Creating a dataset requires an authorization header.
        if self.api_key:
            request.add_header('Authorization', self.api_key)
        # Make the HTTP request.
        response = urllib2.urlopen(request, data_string)
        response.close()
        assert response.code == 200
        # Use the json module to load CKAN's response into a dictionary.
        response_dict = json.loads(response.read())
        if response.url != url:
            response_dict['_redirected_to'] = response.url
        return response_dict
    
    
    def send_request(self, data_string, url):
        assert url
        response_dict = self._send_request(data_string, url)
        assert response_dict['success']
        # package_create returns the created package as its result.
        return response_dict['result']
    
    
    def site_read(self):
        '''
        For checking if url is CKAN site
        
        :return: true if correct url
        '''
        url = self.url + "/api/action/site_read"
        try:
            resp = self._send_request('', url)<commit_msg>fix: OpenDataNode/ckanext-odn-ic2pc-sync#14 close connection - response close at wrong place<commit_after>'''
@author: jmc, mvi
'''

import json
import urllib2
import urllib
import requests

# TODO tags

class CkanAPIWrapper(): 
    '''
    This class uses API interface to return, create, update, delete, search for datasets
    or dataset resources 
    '''
    
    def __init__(self, url, api_key):
        assert url
        self.url = url
        self.api_key = api_key
        
        
    def _send_request(self, data_string, url):
        assert url
        request = urllib2.Request(url)
        # Creating a dataset requires an authorization header.
        if self.api_key:
            request.add_header('Authorization', self.api_key)
        # Make the HTTP request.
        response = urllib2.urlopen(request, data_string)
        assert response.code == 200
        # Use the json module to load CKAN's response into a dictionary.
        response_dict = json.loads(response.read())
        if response.url != url:
            response_dict['_redirected_to'] = response.url
        response.close()
        return response_dict
    
    
    def send_request(self, data_string, url):
        assert url
        response_dict = self._send_request(data_string, url)
        assert response_dict['success']
        # package_create returns the created package as its result.
        return response_dict['result']
    
    
    def site_read(self):
        '''
        For checking if url is CKAN site
        
        :return: true if correct url
        '''
        url = self.url + "/api/action/site_read"
        try:
            resp = self._send_request('', url)
{'fraction_non_alphanumeric': 0.08432267884322679, 'fraction_numerical': 0.0045662100456621, 'mean_word_length': 2.1687560270009643, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '220101', 'n_tokens_mistral': 931, 'n_tokens_neox': 910, 'n_words': 275}