Skip to main content

Catch fatal errors or any type of error, used to easily normalize executions in swift calls with PythonKit

Project description

guard_exception

- guard all exception for swift run python

install

- pip install guard-exception

functions

- guardExceptionWithOutParams: execute function without parameters
- guardException: execute function with parameters
- guardSearchKeyException: look for a key
- guardSearchKeryRecursiveException: look for a key recursively
- guardExceptionIsIterable: return 1 or 0, if exists key on dic

example

import guard_exception as ge

gg = ge.guard_exception()


def divide(a, b):
    return a / b


print(gg.guardException(divide, a=1, b=0))


# result cli
# {'data:': None, 'error': 'division by zero'}

example on swift with PythonKit

import PythonKit
let hvac = Python.import("hvac")
let ge = Python.import("guard_exception")
let gg = ge.guard_exception()

let client = hvac.Client(url: "http://0.0.0.0:8200")

let user = "incorrect_user"
let params = Python.dict()
params["username"] = PythonObject(user)
params["password"] = PythonObject("incorrect_password")

print(gg.guardException(client.login, url: "v1/auth/userpass/login/\(user)", use_token: true, json: params))

// result: cli
//{'data': None, 'error': 'invalid username or password, on post http://0.0.0.0:8200/v1/auth/userpass/login/incorrect_user'}

example complete on swift

// guardException
// guardSearchKeyException
// guardSearchKeryRecursiveException

import Vapor
import PythonKit
let hvac = Python.import("hvac")
let ge = Python.import("guard_exception")
let gg = ge.guard_exception()

// token structure for authentication
// Content: help resolve the api response -> json
public struct Token: Content {
    public var client_token: String!
    public var accessor: String!
    public var policies: [String]!
    public var token_policies: [String]!
}


// swift class for authentication
public class autentication  {
    // MARK: - Properties
    let client = hvac.Client(url: "http://0.0.0.0:8200")
    var token: Token?
    
    // MARK: - Initializers
    public init() { }

    public func login(user: String, password: String) -> Token {
        let params = Python.dict()
        params["username"] = PythonObject(user)
        params["password"] = PythonObject(password)

        let generated = Python.dict(gg.guardException(self.client.login, url: "v1/auth/userpass/login/\(user)", use_token: true, json: params))

        // result genereted
        /*
            {
                'data': 
                {
                    'request_id': '45464674xxxxxxxb', 
                    'lease_id': '', 
                    'renewable': False, 
                    'lease_duration': 0, 
                    'data': None, 
                    'wrap_info': None, 
                    'warnings': None, 
                    'auth': {
                        'client_token': 'hvs.CAESasdfasdfxxxxxxxxxxxxxxxxxxx', 
                        'accessor': 'TvExxxxxxxxxxxxxxx1ESqjW', 
                        'policies': ['default'], 
                        'token_policies': ['default'], 
                        'metadata': {'username': 'rafael'}, 
                        'lease_duration': 2764800, 
                        'renewable': True, 
                        'entity_id': 'dadxxxf8-xxxx-xxxxx-xxxxx-xxxxxxxxxxx', 
                        'token_type': 'service', 
                        'orphan': True, 
                        'mfa_requirement': None, 
                        'num_uses': 0
                    }
                }, 
                'error': None
            }
        */

        let auth = gg.guardExceptionIsIterable(generated, key:"data")

        if Int(auth["data"])! > 0 {

            let client_token = String(gg.guardSearchKeryRecursiveException(generated, key:["data", "auth", "client_token"])["data"])
            let accessor = String(gg.guardSearchKeryRecursiveException(generated, key:["data", "auth", "accessor"])["data"])
            let policies = [String](gg.guardSearchKeryRecursiveException(generated, key:["data", "auth", "policies"])["data"])!
            let token_policies = [String](gg.guardSearchKeryRecursiveException(generated, key:["data", "auth", "token_policies"])["data"])!

            if client_token != nil {
                self.token = Token(
                    client_token: client_token,
                    accessor: accessor,
                    policies: policies,
                    token_policies: token_policies
                )
            }
        }
        
        return self.token ?? Token(client_token: nil, accessor: nil, policies: [], token_policies: [])
    }

}

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

guard_exception-1.1.0.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

guard_exception-1.1.0-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file guard_exception-1.1.0.tar.gz.

File metadata

  • Download URL: guard_exception-1.1.0.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.11 CPython/3.8.10 Linux/5.13.0-39-generic

File hashes

Hashes for guard_exception-1.1.0.tar.gz
Algorithm Hash digest
SHA256 ac06c46e029d1b1c31c7dfd8500a7493d45c2e251a291a1cb48f9dc7ce0967c0
MD5 5dd5eb1b6c24e00793baffaa7354a6a4
BLAKE2b-256 5f9d11a98d36cafa19b4efdb704e851c23aa96b1b596e9172ef1b2c3d3c573d3

See more details on using hashes here.

File details

Details for the file guard_exception-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: guard_exception-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.11 CPython/3.8.10 Linux/5.13.0-39-generic

File hashes

Hashes for guard_exception-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 02681b2cee3c1cb732243c19c77cb8592127f461cbc9a3badf1463d66298bc4c
MD5 852f7914a1ae38d85a0918c6f8310edb
BLAKE2b-256 70ad7b277e4e9217aed159cf1682ca9584b9e636291c13419236fbe693b7130f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page