exoscale / cs

A simple, yet powerful CloudStack API client for python and the command-line.
BSD 3-Clause "New" or "Revised" License
88 stars 36 forks source link

Add support for custom HTTP request headers #100

Closed falzm closed 5 years ago

falzm commented 5 years ago

This change adds support for user-defined arbitrary headers to be injected to every outgoing HTTP request to the Exoscale API. Configuration is done through a new header_<NAME> key in the cloudstack.ini file:

[cloudstack]
endpoint = https://api.exoscale.ch/compute
key = EXO************************
secret = *******************************************
timeout = 60
header_X-Custom-Header1 = foo
header_X-Custom-Header2 = bar

Story: The cs library should allow adding arbitrary headers

greut commented 5 years ago

Python's configparser supports multiline, e.g. the tox.ini of this very project.

from configparser import ConfigParser

config = """\
[test]
headers =
  foo: bar
  spam: eggs
"""

c = ConfigParser()
c.read_string(config)
hs = [line.strip().split(": ")
      for line in c.get("test", "headers").split("\n")
      if line.strip()]

print(hs)