<commit_before># -*- coding: utf-8 -*-

import sys, requests
# Helper function to merge headers
def merge(x, y):
    z = x.copy()
    z.update(y)
    return z

# Note: in order to use this example, you need to have at least one account

USERNAME     = '<EMAIL>'
PASSWORD     = '<PASSWORD>'
CONSUMER_KEY = '<KEY>'

# API server URL
BASE_URL  = "http://127.0.0.1:8080"
LOGIN_URL = '{0}/my/logins/direct'.format(BASE_URL)

# API server will redirect your browser to this URL, should be non-functional

# You will paste the redirect location here when running the script
CALLBACK_URI = 'http://127.0.0.1/cb'

# Our account's bank
OUR_BANK = 'obp-bank-x-gh'

# You probably don't need to change those
loginHeader  = { 'Authorization' : 'DirectLogin username="%s",password="%s",consumer_key="%s"' % (USERNAME, PASSWORD, CONSUMER_KEY)}

# Login and receive authorized token
print 'Login as {0} to {1}'.format(loginHeader, LOGIN_URL)
r = requests.get(LOGIN_URL, headers=loginHeader)

if (r.status_code != 200):
    print "error: could not login"
    sys.exit(0)

# Login OK - create authorization headers
token = r.json()['token']
print "Received token: {0}".format(token)

# Prepare headers
directlogin  = { 'Authorization' : 'DirectLogin token=%s' % token}
content_json = { 'content-type'  : 'application/json' }
limit        = { 'obp_limit'     : '25' }

# Get all private accounts for this user
response = requests.get(u"{0}/obp/v1.4.0/banks/{1}/accounts/private".format(BASE_URL, OUR_BANK), headers=directlogin)
print response.status_code

# Print accounts 
accounts = response.json()['accounts']
for a in accounts:
    print a['id']

# Just picking first account
our_account = accounts[0]['id']
print "our account: {0}".format(our_account)

# Prepare post data and set new label value
post_data = {
    'id' : '%s' % our_account,
    'label' : 'New label',
    'bank_id': '%s' % OUR_BANK 
}

# Send post request with attached json with new label value
response = requests.post(u"{0}/obp/v1.4.0/banks/{1}/accounts/{2}".format(BASE_URL, OUR_BANK, our_account), json=post_data, headers=merge(directlogin, content_json))

# Print result
print response.status_code
print response.text
<commit_msg>Made hello_obp.py compatible with Python3 and changed login data to working example<commit_after># -*- coding: utf-8 -*-

import sys, requests

# Helper function to merge headers
def merge(x, y):
    z = x.copy()
    z.update(y)
    return z


# API server URL
BASE_URL  = "https://apisandbox.openbankproject.com"
#BASE_URL  = "http://localhost:8080"

# Note: in order to use this example, you need to have at least one account
# The data here should work as is on the sandbox given by BASE_URL:
USERNAME     = '<EMAIL>'
PASSWORD     = '<PASSWORD>'
CONSUMER_KEY = '<KEY>'

# Our account's bank
OUR_BANK = 'obp-bankx-n'

# You probably don't need to change those
LOGIN_URL = '{0}/my/logins/direct'.format(BASE_URL)
LOGIN_HEADER  = { 'Authorization' : 'DirectLogin username="%s",password="%s",consumer_key="%s"' % (USERNAME, PASSWORD, CONSUMER_KEY)}



# Login and receive authorized token
print('Login as {0} to {1}'.format(LOGIN_HEADER, LOGIN_URL))
r = requests.get(LOGIN_URL, headers=LOGIN_HEADER)

if (r.status_code != 200):
    print("error: could not login")
    sys.exit(0)

# Login OK - create authorization headers
token = r.json()['token']
print("Received token: {0}".format(token))

# Prepare headers
directlogin  = { 'Authorization' : 'DirectLogin token=%s' % token}
content_json = { 'content-type'  : 'application/json' }
limit        = { 'obp_limit'     : '25' }

# Get all private accounts for this user
response = requests.get(u"{0}/obp/v1.4.0/banks/{1}/accounts/private".format(BASE_URL, OUR_BANK), headers=directlogin)
print(response.status_code)

# Print accounts 
accounts = response.json()['accounts']
for a in accounts:
    print(a['id'])

# Just picking first account
our_account = accounts[0]['id']
print("our account: {0}".format(our_account))

# Prepare post data and set new label value
post_data = {
    'id' : '%s' % our_account,
    'label' : 'New label',
    'bank_id': '%s' % OUR_BANK 
}

# Send post request with attached json with new label value
response = requests.post(u"{0}/obp/v1.4.0/banks/{1}/accounts/{2}".format(BASE_URL, OUR_BANK, our_account), json=post_data, headers=merge(directlogin, content_json))

# Print result
print(response.status_code)
print(response.text)

{'fraction_non_alphanumeric': 0.14321436690156855, 'fraction_numerical': 0.015685383041600363, 'mean_word_length': 4.452292441140025, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '4063396', 'n_tokens_mistral': 1545, 'n_tokens_neox': 1498, 'n_words': 419}