<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import os <EOL> import logging <EOL> import json <EOL> import salt . utils . http <EOL> from salt . exceptions import CommandExecutionError <EOL> log = logging . getLogger ( __name__ ) <EOL> def _get_token ( profile ) : <EOL> '''<STR_LIT>''' <EOL> token = os . environ . get ( '<STR_LIT>' , profile . get ( '<STR_LIT>' ) ) <EOL> if token is None : <EOL> raise CommandExecutionError ( '<STR_LIT>' ) <EOL> return token <EOL> def write_ ( path , key , value , profile = None ) : <EOL> '''<STR_LIT>''' <EOL> result = _query ( <EOL> '<STR_LIT:POST>' , <EOL> path , <EOL> profile = profile , <EOL> data = json . dumps ( { key : value } ) , <EOL> ) <EOL> return read_ ( path , key , profile ) <EOL> def read_ ( path , key = None , profile = None ) : <EOL> '''<STR_LIT>''' <EOL> result = _query ( <EOL> '<STR_LIT:GET>' , <EOL> path , <EOL> profile = profile , <EOL> decode = True , <EOL> decode_type = '<STR_LIT>' , <EOL> ) <EOL> data = result [ '<STR_LIT>' ] . get ( '<STR_LIT:data>' , { } ) <EOL> if key is None : <EOL> return data <EOL> value = data . get ( key ) <EOL> if value is None : <EOL> log . error ( '<STR_LIT>' ) <EOL> return value <EOL> def _query ( method , path , profile = None , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> token = _get_token ( profile ) <EOL> headers = { '<STR_LIT>' : token } <EOL> url = '<STR_LIT>' . format ( <EOL> profile . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> profile . get ( '<STR_LIT>' ) , <EOL> profile . get ( '<STR_LIT>' ) , <EOL> path , <EOL> ) <EOL> result = salt . utils . http . query ( <EOL> url , <EOL> method , <EOL> header_dict = headers , <EOL> status = True , <EOL> ** kwargs <EOL> ) <EOL> if not str ( result [ '<STR_LIT:status>' ] ) . startswith ( '<STR_LIT:2>' ) : <EOL> error = result . get ( <EOL> '<STR_LIT:error>' , <EOL> '<STR_LIT>' . format ( result [ '<STR_LIT:status>' ] ) <EOL> ) <EOL> log . error ( error ) <EOL> raise CommandExecutionError ( error ) <EOL> return result </s>
