DEV Community

Cover image for Configuration as a Service (occson) and Python
Tomasz Kowalewski
Tomasz Kowalewski

Posted on

Configuration as a Service (occson) and Python

At the beginning of our occson adventure, let's first ask ourselves a basic question: What is configuration?

A configuration is a collection of information necessary for the operation of a program. It can be, for example, environment variables or structured data formats (json, yaml).

How we handle configurations is also important.
Probably each of us is familiar with problems related to uploading configuration file to server for application :|.
Just as problematic seems to be maintaining the configuration in the right version.
Is the FOO variable needed for the application to work? - How many times have you asked a similar question?

However, there is a solution for these problems - it is Configuration as a Service.
A service that provides configuration to an application :)
We call it occson.

Today I have prepared a ccs client especially for python developers. CCS? Configuration Control System :)

import requests
from AesEverywhere import aes256

class Downloader:
  def __init__(self, uri, access_token, passphrase):
    self.uri = uri
    self.access_token = access_token
    self.passphrase = passphrase

  def call(self):
    response = requests.get(url = self.uri, headers = self.__headers())
    return aes256.decrypt(response.json()['encrypted_content'], self.passphrase)

  def __headers(self):
    return {
      'Content-Type': 'application/json',
      'Authorization': 'Token token={0}'.format(self.access_token)
    }

class Uploader:
  def __init__(self, uri, content, access_token, passphrase, force):
    self.uri = uri
    self.content = content
    self.access_token = access_token
    self.passphrase = passphrase
    self.force = force

  def call(self):
    response = requests.post(url = self.uri, json = self.__data(), headers = self.__headers())
    return response.status_code == 200 or response.status_code == 201

  def __data(self):
    return {
      'encrypted_content': aes256.encrypt(self.content, self.passphrase),
      'force': 'true' if self.force else 'false'
    }

  def __headers(self):
    return {
      'Content-Type': 'application/json',
      'Authorization': 'Token token={0}'.format(self.access_token)
    }

class Document:
  def __init__(self, uri, access_token, passphrase):
    self.uri = self.__build_uri(uri)
    self.access_token = access_token
    self.passphrase = passphrase

  def upload(self, content, force = False):
    return Uploader(self.uri, content, self.access_token, self.passphrase, force).call()

  def download(self):
    return Downloader(self.uri, self.access_token, self.passphrase).call()

  def __build_uri(self, uri):
    return uri.replace('ccs://', 'https://api.occson.com/')
Enter fullscreen mode Exit fullscreen mode

How to use the above client?

Install dependencies (requests, aes-everywhere) And use the ccs API.

import ccs

document = ccs.Document("ccs://.env", "<ACCESS_TOKEN>", "test")
print(document.download())
print(document.upload("A=1\nB=2", True))
Enter fullscreen mode Exit fullscreen mode

How to use occson with ruby? Read about it on our blog

Top comments (0)