Timeout parameter of Python Requests causing exception - python

I have some Requests code that is meant to allow my code to retry a URL a set number of times after a set length of time before time out:
import requests
s = Session()
s.mount('http://www.example.com/', HTTPAdapter(
max_retries=Retry(total=50, status_forcelist=
[400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418,
421, 422, 423, 424, 425, 426, 428, 429, 431, 451, 500, 501, 502, 503, 504, 505, 506, 507, 508, 510, 511])
)
)
url = 'www.example.com/somepage'
params = {'param1': '1', 'param2': '2'}
headers = {'someheader': 'header'}
response = s.get(url, params=params, headers=headers, timeout=60)
However this is causing the below error, which I am unsure of the reason for. Can anyone tell me why this is happening?
Traceback (most recent call last):
File "C:\Python27\ExampleScript.py", line 244, in examplefunction
response = s.get(url, params=params, headers=headers, timeout=60)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 468, in get
return self.request('GET', url, **kwargs)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 456, in request
resp = self.send(prep, **send_kwargs)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 559, in send
r = adapter.send(request, **kwargs)
File "C:\Python27\lib\site-packages\requests\adapters.py", line 327, in send
timeout=timeout
File "C:\Python27\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 558, in urlopen
"broken by '%r': %s" % (retries, err, url))
TypeError: %d format: a number is required, not Retry
Thanks

Related

The transaction declared chain ID 5777, but the connected node is on 1337

I am trying to deploy my SimpleStorage.sol contract to a ganache local chain by making a transaction using python. It seems to have trouble connecting to the chain.
from solcx import compile_standard
from web3 import Web3
import json
import os
from dotenv import load_dotenv
load_dotenv()
with open("./SimpleStorage.sol", "r") as file:
simple_storage_file = file.read()
compiled_sol = compile_standard(
{
"language": "Solidity",
"sources": {"SimpleStorage.sol": {"content": simple_storage_file}},
"settings": {
"outputSelection": {
"*": {"*": ["abi", "metadata", "evm.bytecode", "evm.sourceMap"]}
}
},
},
solc_version="0.6.0",
)
with open("compiled_code.json", "w") as file:
json.dump(compiled_sol, file)
# get bytecode
bytecode = compiled_sol["contracts"]["SimpleStorage.sol"]["SimpleStorage"]["evm"][
"bytecode"
]["object"]
# get ABI
abi = compiled_sol["contracts"]["SimpleStorage.sol"]["SimpleStorage"]["abi"]
# to connect to ganache blockchain
w3 = Web3(Web3.HTTPProvider("HTTP://127.0.0.1:7545"))
chain_id = 5777
my_address = "0xca1EA31e644F13E3E36631382686fD471c62267A"
private_key = os.getenv("PRIVATE_KEY")
# create the contract in python
SimpleStorage = w3.eth.contract(abi=abi, bytecode=bytecode)
# get the latest transaction
nonce = w3.eth.getTransactionCount(my_address)
# 1. Build a transaction
# 2. Sign a transaction
# 3. Send a transaction
transaction = SimpleStorage.constructor().buildTransaction(
{"chainId": chain_id, "from": my_address, "nonce": nonce}
)
print(transaction)
It seems to be connected to the ganache chain because it prints the nonce, but when I build and try to print the transaction
here is the entire traceback call I am receiving
Traceback (most recent call last):
File "C:\Users\evens\demos\web3_py_simple_storage\deploy.py", line
52, in <module>
transaction = SimpleStorage.constructor().buildTransaction(
File "C:\Python310\lib\site-packages\eth_utils\decorators.py", line
18, in _wrapper
return self.method(obj, *args, **kwargs)
File "C:\Users\evens\AppData\Roaming\Python\Python310\site-
packages\web3\contract.py", line 684, in buildTransaction
return fill_transaction_defaults(self.web3, built_transaction)
File "cytoolz/functoolz.pyx", line 250, in
cytoolz.functoolz.curry.__call__
return self.func(*args, **kwargs)
File "C:\Users\evens\AppData\Roaming\Python\Python310\site-
packages\web3\_utils\transactions.py", line 114, in
fill_transaction_defaults
default_val = default_getter(web3, transaction)
File "C:\Users\evens\AppData\Roaming\Python\Python310\site-
packages\web3\_utils\transactions.py", line 60, in <lambda>
'gas': lambda web3, tx: web3.eth.estimate_gas(tx),
File "C:\Users\evens\AppData\Roaming\Python\Python310\site-
packages\web3\eth.py", line 820, in estimate_gas
return self._estimate_gas(transaction, block_identifier)
File "C:\Users\evens\AppData\Roaming\Python\Python310\site-
packages\web3\module.py", line 57, in caller
result = w3.manager.request_blocking(method_str,
File "C:\Users\evens\AppData\Roaming\Python\Python310\site-
packages\web3\manager.py", line 197, in request_blocking
response = self._make_request(method, params)
File "C:\Users\evens\AppData\Roaming\Python\Python310\site-
packages\web3\manager.py", line 150, in _make_request
return request_func(method, params)
File "cytoolz/functoolz.pyx", line 250, in
cytoolz.functoolz.curry.__call__
return self.func(*args, **kwargs)
File "C:\Users\evens\AppData\Roaming\Python\Python310\site-
packages\web3\middleware\formatting.py", line 76, in
apply_formatters
response = make_request(method, params)
File "C:\Users\evens\AppData\Roaming\Python\Python310\site-
packages\web3\middleware\gas_price_strategy.py", line 90, in
middleware
return make_request(method, params)
File "cytoolz/functoolz.pyx", line 250, in
cytoolz.functoolz.curry.__call__
return self.func(*args, **kwargs)
File "C:\Users\evens\AppData\Roaming\Python\Python310\site-
packages\web3\middleware\formatting.py", line 74, in
apply_formatters
response = make_request(method, formatted_params)
File "C:\Users\evens\AppData\Roaming\Python\Python310\site-
packages\web3\middleware\attrdict.py", line 33, in middleware
response = make_request(method, params)
File "cytoolz/functoolz.pyx", line 250, in
cytoolz.functoolz.curry.__call__
return self.func(*args, **kwargs)
File "C:\Users\evens\AppData\Roaming\Python\Python310\site-
packages\web3\middleware\formatting.py", line 74, in
apply_formatters
response = make_request(method, formatted_params)
File "cytoolz/functoolz.pyx", line 250, in
cytoolz.functoolz.curry.__call__
return self.func(*args, **kwargs)
File "C:\Users\evens\AppData\Roaming\Python\Python310\site-
packages\web3\middleware\formatting.py", line 73, in
apply_formatters
formatted_params = formatter(params)
File "cytoolz/functoolz.pyx", line 503, in
cytoolz.functoolz.Compose.__call__
ret = PyObject_Call(self.first, args, kwargs)
File "cytoolz/functoolz.pyx", line 250, in
cytoolz.functoolz.curry.__call__
return self.func(*args, **kwargs)
File "C:\Python310\lib\site-packages\eth_utils\decorators.py", line
91, in wrapper
return ReturnType(result) # type: ignore
File "C:\Python310\lib\site-packages\eth_utils\applicators.py", line
22, in apply_formatter_at_index
yield formatter(item)
File "cytoolz/functoolz.pyx", line 250, in
cytoolz.functoolz.curry.__call__
File "cytoolz/functoolz.pyx", line 250, in
cytoolz.functoolz.curry.__call__
return self.func(*args, **kwargs)
File "C:\Python310\lib\site-packages\eth_utils\applicators.py", line
72, in apply_formatter_if
return formatter(value)
File "cytoolz/functoolz.pyx", line 250, in
cytoolz.functoolz.curry.__call__
return self.func(*args, **kwargs)
File "C:\Users\evens\AppData\Roaming\Python\Python310\site-
packages\web3\middleware\validation.py", line 57, in
validate_chain_id
raise ValidationError(
web3.exceptions.ValidationError: The transaction declared chain ID
5777, but the connected node is on 1337
Had this issue myself, apparently it's some sort of Ganache CLI error but the simplest fix I could find was to change the network id in Ganache through settings>server to 1337. It restarts the session so you'd then need to change the address and private key variable.
If it's the same tutorial I'm doing, you're likely to come unstuck after this... the code for transaction should be:
transaction =
SimpleStorage.constructor().buildTransaction( {
"gasPrice": w3.eth.gas_price,
"chainId": chain_id,
"from": my_address,
"nonce": nonce,
})
print(transaction)
Otherwise you get a value error if you don't set the gasPrice
this line of code is wrong
chain_id = 5777
Ganache chain id is not 5777. This is network id. Network id is used by nodes to transfer data between nodes that are on the same network. Network id is not included in blocks and it is not used for signing transactions or mining blocks.
chain_id = 1377
Chain ID is not included in blocks either, but it is used during the transaction signing and verification process.
It works for me, I get value chain_id from w3.eth.chain_id
transaction = SimpleStorage.constructor().buildTransaction(
{
"gasPrice": w3.eth.gas_price,
"chainId": w3.eth.chain_id,
"from": my_address,
"nonce": nonce,
}
)
Delegates to eth_chainId RPC Method
Returns an integer value for the currently configured “Chain Id” value introduced in EIP-155. Returns None if no Chain Id is available.
Hey it happened to me too, you need to build the constructor like this
SimpleStorage.constructor().buildTransaction( {
"gasPrice": w3.eth.gas_price,
"chainId": chain_id,
"from": my_address,
"nonce": nonce,
You need to add the gas price part.
Worked for me
I encountered this issue today and after debugging, I am certain that the issue is from the dotenv config variables imported. All the variables are been seen by python as strings, and for some reason, the web3 library swaps the value 5777 if the type of your chain id is not an integer.
The perfect fix is to cast your chain_id into type int before deploying your smart contract.
Hope this saves y'all some pondering time, peace out!
Snippet example below:
transaction = SimpleStorage.constructor().buildTransaction(
{
"gasPrice": w3.eth.gas_price,
"chainId": int(chain_id),
"from": my_address,
"nonce": nonce,
}
)
Perhaps, you are following FreeCodeCamp tutorial.
I have got the same problem
In my case, it is working after adding gas price, "gasPrice": w3.eth.gas_price
transaction = SimpleStorage.constructor().buildTransaction({ "gasPrice": w3.eth.gas_price, "chainId" : chain_id, "from": my_address, "nonce": nonce})
print(transaction)
You can check your chainId using the following code
const chainId = await wallet.getChainId();
console.log("chain Id",chainId);
For me it was 1337

Troubles when trying to build and sign a transaction with web3

I'm watching the tutorial of freeCodeCamp.org,
Solidity, Blockchain, and Smart Contract Course – Beginner to Expert Python Tutorial (link of the video course) and I'm stucked at 04:06:47 because when I try to build a transaction and sign it, my compiler gives me back a lot of errors:
INFORMAZIONI: impossibile trovare file corrispondenti ai criteri di
ricerca indicati. Traceback (most recent call last): File
"C:\Users\giuse\OneDrive\Desktop\Sol\web3_py_simple_storage\deploy.py",
line 44, in
transaction = SimpleStorage.constructor().buildTransaction( File "C:\Users\giuse\AppData\Local\Programs\Python\Python39\lib\site-packages\eth_utils\decorators.py",
line 18, in _wrapper
return self.method(obj, *args, **kwargs) File "C:\Users\giuse\AppData\Local\Programs\Python\Python39\lib\site-packages\web3\contract.py",
line 684, in buildTransaction
return fill_transaction_defaults(self.web3, built_transaction) File "cytoolz/functoolz.pyx", line 250, in
cytoolz.functoolz.curry.call
return self.func(*args, **kwargs) File "C:\Users\giuse\AppData\Local\Programs\Python\Python39\lib\site-packages\web3_utils\transactions.py",
line 121, in fill_transaction_defaults
default_val = default_getter(web3, transaction) File "C:\Users\giuse\AppData\Local\Programs\Python\Python39\lib\site-packages\web3_utils\transactions.py",
line 67, in
'gas': lambda web3, tx: web3.eth.estimate_gas(tx), File "C:\Users\giuse\AppData\Local\Programs\Python\Python39\lib\site-packages\web3\eth.py",
line 759, in estimate_gas
return self._estimate_gas(transaction, block_identifier) File "C:\Users\giuse\AppData\Local\Programs\Python\Python39\lib\site-packages\web3\module.py",
line 57, in caller
result = w3.manager.request_blocking(method_str, File "C:\Users\giuse\AppData\Local\Programs\Python\Python39\lib\site-packages\web3\manager.py",
line 197, in request_blocking
response = self._make_request(method, params) File "C:\Users\giuse\AppData\Local\Programs\Python\Python39\lib\site-packages\web3\manager.py",
line 150, in _make_request
return request_func(method, params) File "cytoolz/functoolz.pyx", line 250, in cytoolz.functoolz.curry.call
return self.func(*args, **kwargs) File "C:\Users\giuse\AppData\Local\Programs\Python\Python39\lib\site-packages\web3\middleware\formatting.py",
line 76, in apply_formatters
response = make_request(method, params) File "C:\Users\giuse\AppData\Local\Programs\Python\Python39\lib\site-packages\web3\middleware\gas_price_strategy.py",
line 90, in middleware
return make_request(method, params) File "cytoolz/functoolz.pyx", line 250, in cytoolz.functoolz.curry.call
return self.func(*args, **kwargs) File "C:\Users\giuse\AppData\Local\Programs\Python\Python39\lib\site-packages\web3\middleware\formatting.py",
line 74, in apply_formatters
response = make_request(method, formatted_params) File "C:\Users\giuse\AppData\Local\Programs\Python\Python39\lib\site-packages\web3\middleware\attrdict.py",
line 33, in middleware
response = make_request(method, params) File "cytoolz/functoolz.pyx", line 250, in cytoolz.functoolz.curry.call
return self.func(*args, **kwargs) File "C:\Users\giuse\AppData\Local\Programs\Python\Python39\lib\site-packages\web3\middleware\formatting.py",
line 74, in apply_formatters
response = make_request(method, formatted_params) File "cytoolz/functoolz.pyx", line 250, in cytoolz.functoolz.curry.call
return self.func(*args, **kwargs) File "C:\Users\giuse\AppData\Local\Programs\Python\Python39\lib\site-packages\web3\middleware\formatting.py",
line 73, in apply_formatters
formatted_params = formatter(params) File "cytoolz/functoolz.pyx", line 503, in
cytoolz.functoolz.Compose.call
ret = PyObject_Call(self.first, args, kwargs) File "cytoolz/functoolz.pyx", line 250, in cytoolz.functoolz.curry.call
return self.func(*args, **kwargs) File "C:\Users\giuse\AppData\Local\Programs\Python\Python39\lib\site-packages\eth_utils\decorators.py",
line 91, in wrapper
return ReturnType(result) # type: ignore File "C:\Users\giuse\AppData\Local\Programs\Python\Python39\lib\site-packages\eth_utils\applicators.py",
line 22, in apply_formatter_at_index
yield formatter(item) File "cytoolz/functoolz.pyx", line 250, in cytoolz.functoolz.curry.call
return self.func(*args, **kwargs) File "C:\Users\giuse\AppData\Local\Programs\Python\Python39\lib\site-packages\eth_utils\functional.py",
line 45, in inner
return callback(fn(*args, **kwargs)) File "C:\Users\giuse\AppData\Local\Programs\Python\Python39\lib\site-packages\eth_utils\applicators.py",
line 84, in apply_formatters_to_dict
yield key, formatterskey File "cytoolz/functoolz.pyx", line 250, in cytoolz.functoolz.curry.call
return self.func(*args, **kwargs) File "C:\Users\giuse\AppData\Local\Programs\Python\Python39\lib\site-packages\eth_utils\applicators.py",
line 72, in apply_formatter_if
return formatter(value) File "cytoolz/functoolz.pyx", line 250, in cytoolz.functoolz.curry.call
return self.func(*args, **kwargs) File "C:\Users\giuse\AppData\Local\Programs\Python\Python39\lib\site-packages\web3\middleware\validation.py",
line 57, in validate_chain_id
raise ValidationError( web3.exceptions.ValidationError: The transaction declared chain ID 5777, but the connected node is on 1337
I first searched on internet for solutions, but the only one was to add in the transation build one more parameter (the one of the gasPrice), but it didn't solve my problems.
I hope someone can help me, here is the full code I wrote:
from solcx import compile_standard, install_solc
import json
from web3 import Web3
from dotenv import load_dotenv
import os
load_dotenv()
install_solc("0.6.0")
with open("./simpleStorage.sol", "r") as file:
simple_storage_file = file.read()
compiled_sol = compile_standard(
{
"language": "Solidity",
"sources": {"simpleStorage.sol": {"content": simple_storage_file}},
"settings": {
"outputSelection": {
"*": {"*": ["abi", "metadata", "evm.bytecode", "evm.sourceMap"]}
}
},
},
solc_version="0.6.0",
)
with open("compiledCode.json", "w") as file:
json.dump(compiled_sol, file)
bytecode = compiled_sol["contracts"]["simpleStorage.sol"]["SimpleStorage"]["evm"][
"bytecode"
]["object"]
abi = compiled_sol["contracts"]["simpleStorage.sol"]["SimpleStorage"]["abi"]
w3 = Web3(Web3.HTTPProvider("HTTP://127.0.0.1:7545"))
chain_id = 5777
my_address = "0xd8BADAe3766759e7e298931dF01F452616dc6dde"
pvt_key = os.getenv("PRIVATE_KEY")
SimpleStorage = w3.eth.contract(abi=abi, bytecode=bytecode)
nonce = w3.eth.getTransactionCount(my_address)
transaction = SimpleStorage.constructor().buildTransaction(
{
"chainId": chain_id,
"gasPrice": w3.eth.gas_price,
"from": my_address,
"nonce": nonce,
},
)
signed_txn = w3.eth.sign_transaction(transaction, private_key=pvt_key)
Try changing your chain_id = 5777 to chain_id = 1337.
If you are using ganache quickstart, instead use New Workspace option so you can save it.
Go to settings on top right corner (If you are using GUI from tutorial video)
Go to server tab and select following values and save.
Change values accordingly in your code.
Also change create trasaction code to this
transaction = SimpleStorage.constructor().buildTransaction(
{
"chainId": chain_id,
"gasPrice": w3.eth.gas_price,
"from": my_address,
"nonce": nonce,
}
)
Doing these 2 steps should fix your problem. I just did it for myself.

Error opening a dictionary from an external json file

I wanted python to open my dictionary out of an external json file.
This is with python 3.7.2 in anaconda spyder on windows.
This is the written thing in my file:
r{"0" : {"-25 : 144, 0 : 182, 25 : 224, 50 : 272"}}
r{"1000" : {"-25 : 157, 0 : 198, 25 : 245, 50 : 297"}}
r{"2000" : {"-25 : 172, 0 : 216, 25 : 267, 50 : 324"}}
r{"3000" : {"-25 : 188, 0 : 236, 25 : 292, 50 : 354"}}
r{"4000" : {"-25 : 205, 0 : 258, 25 : 319, 50 : 387"}}
r{"5000" : {"-25 : 224, 0 : 283, 25 : 349, 50 : 423"}}
r{"6000" : {"-25 : 246, 0 : 309, 25 : 381, 50 : 463"}}
r{"7000" : {"-25 : 269, 0 : 339, 25 : 418, 50 : 507"}}
r{"8000" : {"-25 : 295, 0 : 371, 25 : 458, 50 : 555"}}
r{"9000" : {"-25 : 323, 0 : 407, 25 : 502, 50 : 609"}}
r{"10000" : {"-25 : 354, 0 : 446, 25 : 551, 50 : 668"}}
I wanted to open my file with the following code:
togr650 = {}
with open("to_gr_650.json", "r") as config_file:
togr650 = json.load(config_file)
print(togr650)
Unfortunatelly the following error appeard:
Traceback (most recent call last):
File "<ipython-input-29-098635d60a19>", line 1, in <module>
runfile('C:/Users/***/Desktop/take_off_distance.py', wdir='C:/Users/***/Desktop')
File "C:\Users\***\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
execfile(filename, namespace)
File "C:\Users\***\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/***/Desktop/take_off_distance.py", line 26, in <module>
togr650 = json.load(config_file)
File "C:\Users\***\Anaconda3\lib\json\__init__.py", line 296, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "C:\Users\***\Anaconda3\lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "C:\Users\***\Anaconda3\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\***\Anaconda3\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
JSONDecodeError: Expecting value
How do I have to change my code to let it work propperly?

Django S3 image upload error "boto.exception.S3ResponseError: S3ResponseError: 400 Bad Request"

I'm trying to upload an image with Django Rest Framework and AWS S3.
settings.py:
AWS_ACCESS_KEY_ID = ...
AWS_SECRET_ACCESS_KEY = ...
AWS_STORAGE_BUCKET_NAME = ...
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME AWS_S3_OBJECT_PARAMETERS = {
'CacheControl': 'max-age=86400', }
AWS_LOCATION = 'static'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'backend/static'), ] STATIC_URL = 'https://%s/%s/' % (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION) STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage' DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
AWS_DEFAULT_ACL = None
models.py:
class Beat(models.Model):
...
image = models.ImageField(upload_to='static/', default = 'static/None/No-img.gif', null = True)
S3 bucket policy:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Allow All",
"Effect": "Allow",
"Principal": "*",
"Action": [
"s3:GetObject",
"s3:PutObject",
"s3:PutObjectAcl"
],
"Resource": "arn:aws:s3:::my_bucket/*"
}
]
}
However, when I upload an image, it gives error:
boto.exception.S3ResponseError: S3ResponseError: 400 Bad Request
I can't figure out what the problem is.
python manage.py collectstatic works well.
How can I fix this error?
Thanks.
The error message below:
Internal Server Error: /api/beats/create/
Traceback (most recent call last):
File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/django/core/handlers/base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/rest_framework/views.py", line 495, in dispatch
response = self.handle_exception(exc)
File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/rest_framework/views.py", line 455, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/rest_framework/views.py", line 492, in dispatch
response = handler(request, *args, **kwargs)
File "/home/seokchan/server/mdocker/mkdir2/mkdir/backend/beats/views.py", line 114, in post
beat = serializer.save(owner = mowner)
File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/rest_framework/serializers.py", line 214, in save
self.instance = self.create(validated_data)
File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/rest_framework/serializers.py", line 940, in create
instance = ModelClass._default_manager.create(**validated_data)
File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/django/db/models/query.py", line 413, in create
obj.save(force_insert=True, using=self.db)
File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/django/db/models/base.py", line 718, in save
force_update=force_update, update_fields=update_fields)
File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/django/db/models/base.py", line 748, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/django/db/models/base.py", line 831, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/django/db/models/base.py", line 869, in _do_insert
using=using, raw=raw)
File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/django/db/models/query.py", line 1136, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 1288, in execute_sql
for sql, params in self.as_sql():
File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 1241, in as_sql
for obj in self.query.objs
File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 1241, in <listcomp>
for obj in self.query.objs
File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 1240, in <listcomp>
[self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 1192, in pre_save_val
return field.pre_save(obj, add=True)
File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/django/db/models/fields/files.py", line 288, in pre_save
file.save(file.name, file.file, save=False)
File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/django/db/models/fields/files.py", line 87, in save
self.name = self.storage.save(name, content, max_length=self.field.max_length)
File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/django/core/files/storage.py", line 49, in save
return self._save(name, content)
File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/storages/backends/s3boto.py", line 425, in _save
key = self.bucket.get_key(encoded_name)
File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/boto/s3/bucket.py", line 193, in get_key
key, resp = self._get_key_internal(key_name, headers, query_args_l)
File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/boto/s3/bucket.py", line 232, in _get_key_internal
response.status, response.reason, '')
boto.exception.S3ResponseError: S3ResponseError: 400 Bad Request
settings.py needed the lines below:
AWS_REGION = 'ap-northeast-2'
AWS_S3_HOST = 's3.%s.amazonaws.com' % AWS_REGION

Partial updating of object in elastic search using python

So the puamapi/apiobjects_american/4901 object looks like this:
{
"_id": "4701",
"_index": "puamapi",
"_source": {
"CatRais": null,
"Classification": "Photographs",
"Constituents": [],
"CreditLine": "Gift of H. Kelley Rollings, Class of 1948, and Mrs. Rollings",
"CuratorApproved": 0,
"DateBegin": 1921,
"DateEnd": 1921,
"Dated": "1921",
"Department": "Photography",
"DimensionsLabel": "image: 19.3 x 24.6 cm (7 5/8 x 9 11/16 in.)\r\nsheet: 20.2 x 25.4 cm (7 15/16 x 10 in.)",
"Edition": null,
"Medium": "Gelatin silver print",
"ObjectID": 4701,
"ObjectNumber": "1995-341",
"ObjectStatus": "Accessioned Object",
"Restrictions": "Restricted",
"SortNumber": " 1995 341",
"SysTimeStamp": "AAAAAAAAC3k="
},
"_type": "apiobjects_american",
"_version": 4,
"found": true
}
I want to do a partial update on the object, where we add a constituent to the constituent array.
The record looks like this:
{'params': {'item': [{'ConstituentID': 5}]}, 'script': 'if (ctx._source[Constituents] == null) {ctx._source.Constituents = item } else { ctx._source.Constituents+= item }'}
And then I add with an elastic search instance in python:
es.update(index="puamapi", doc_type="apiobjects_american", id=4901, body=record)
But, I'm getting this error
Traceback (most recent call last):
File "json_to_elasticsearch.py", line 138, in <module>
load_xrefs(api_xrefs)
File "json_to_elasticsearch.py", line 118, in load_xrefs
load_xref(table, xref_map[table][0], xref_map[table][1], json.load(file)["RECORDS"])
File "json_to_elasticsearch.py", line 109, in load_xref
es.update(index=database, doc_type=table1, id=id1, body=record)
File "/usr/local/lib/python2.7/dist-packages/elasticsearch/client/utils.py", line 69, in _wrapped
return func(*args, params=params, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/elasticsearch/client/__init__.py", line 460, in update
doc_type, id, '_update'), params=params, body=body)
File "/usr/local/lib/python2.7/dist-packages/elasticsearch/transport.py", line 329, in perform_request
status, headers, data = connection.perform_request(method, url, params, body, ignore=ignore, timeout=timeout)
File "/usr/local/lib/python2.7/dist-packages/elasticsearch/connection/http_urllib3.py", line 109, in perform_request
self._raise_error(response.status, raw_data)
File "/usr/local/lib/python2.7/dist-packages/elasticsearch/connection/base.py", line 108, in _raise_error
raise HTTP_EXCEPTIONS.get(status_code, TransportError)(status_code, error_message, additional_info)
elasticsearch.exceptions.RequestError: TransportError(400, u'illegal_argument_exception', u'[Bastion][127.0.0.1:9300][indices:data/write/update[s]]')
Any insights would be appreciated. Thanks!

Categories