Background -
I have a python program that calls an API, saves the data to a DataFrame, and writes it to a .JSON
+ .CSV
file.
As part of this program, I have been exploring using an .INI
(config) file, which I would like to -
def api_call():
def api_parameters():
function, which are parameters to form a url string, which will be used to make the API call..INI Structure - (with dummy values!)
[firm_api]
key = 6GFDGFSDGSDFGSDFGFSDGFSDGFD
secret = GFSD898F908GFSD09PJGF-80F
[url_params]
start_date = 2021-10-30
end_date = 2021-11-30
Issue -
I have created a def config
, which reads my .INI
file, and attempts to return my API key
and secret
, as well as the url parameters of start_date
and end_date
.
The problem is that when I call the def config
from either def api_parameters
(this is where I need start_date
and end_date
) OR def api_call
(where I need key
& secret
) I get the a ValueError: too many values to unpack (expected 2)
error.
Function that reads the.INI - this reads my .INI
file and returns the 4x values mentioned above:
def ini_config():
file = 'authen_config.ini'
config = ConfigParser()
config.read(file)
key = config.get('firm_api', 'key')
secret = config.get('firm_api', 'secret')
start_date = config.get('url_params', 'start_date')
end_date = config.get('url_params', 'end_date')
return key, secret, start_date, end_date
Function that needs start_date
and end_date
- this function constructs the url to call:
def api_parameters():
start_date, end_date = ini_config()
str = "https://firm.vendor.com/api/v1/portfolio/views/45265/results?portfolio_id=10&portfolio_type=firm&output_type=json&start_date={}&end_date={}&vendor_firm=333"
url = str.format(start_date, end_date)
print("-----------------------\n", "API URLs constructed\n-----------------------")
return url
api_parameters()
Function that needs key
and secret
- makes the API call:
def api_call():
key, secret = ini_config()
url = api_parameters()
response = requests.get(url, auth = HTTPBasicAuth(key, secret), headers={"Vendor-Firm":"333"})
api_response = json.loads(response.text)
return api_response
api_call()
I think the problem is that although in the def api_call():
and def url_parameters():
I am only trying to call def ini_config():
for the values I need, it is returning all 4x values and confusing the functions.
Does anyone have any suggestions how I can resolve these ValueError: too many values to unpack (expected 2)
?
Your ini_config()
returns 4 values
return key, secret, start_date, end_date
but you try to assign to 2 variables
start_date, end_date = ini_config()
# or
key, secret = ini_config()
and this makes problem.
You have to use 4 variables
key, secret, start_date, end_date = ini_config()
Or you can use popular _
as variable to skip some values
_, _, start_date, end_date = ini_config()
# or
key, secret, _, _ = ini_config()
Eventually you should create two functions - one only to get keys
, and one to get only dates
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.