Exemple #1
0
 def fn(body, as_obj=False):
     try:
         if id_field and model.get_by_id(body.get(id_field)):
             raise EntityAlreadyExists(model.__name__)
     except TypeError as e:
         raise InvalidField(*e)
     field_kwargs = (assoc(pick(fields, body), "id", body.get(id_field))
                     if id_field else pick(fields, body))
     entity = model(**field_kwargs)
     entity.put()
     return entity if as_obj else entity.to_dict()
Exemple #2
0
 def mutate_and_get_payload(cls, root, info, **input):
     provider_key = get_key(input.get("id"))
     if provider_key.parent() != info.context.user_key:
         raise ExecutionUnauthorized
     body = pick(_provider_config_fields, input)
     provider = put_config(provider_key, body)
     return UpdateProviderConfig(provider=provider)
Exemple #3
0
 def fn(id, body, as_obj=False):
     entity = get_entity(model, id, urlsafe)
     field_kwargs = pick(fields, body)
     if field_kwargs:
         entity.populate(**field_kwargs)
         entity.put()
     return entity if as_obj else entity.to_dict()
Exemple #4
0
    def mutate_and_get_payload(cls, root, info, **input):
        service_key = get_key(input.get("id"))
        if service_key.parent().parent() != info.context.user_key:
            raise ExecutionUnauthorized

        body = pick(_service_static_fields, input)
        service = put_static(service_key, body)
        return UpdateServiceStatic(service=service)
Exemple #5
0
 def mutate_and_get_payload(cls, root, info, **input):
     account_key = get_key(input.get("id"))
     if account_key.parent() != info.context.user_key:
         raise ExecutionUnauthorized
     account = update(
         fields=filter(lambda x: x != "id", cls.Input._meta.fields.keys()),
         account=account_key,
         body=pick(["site", "name"], input),
         as_obj=True,
     )
     return UpdateAccount(account=account)
Exemple #6
0
 def mutate_and_get_payload(cls, root, info, **input):
     provider_key = get_key(input.get("id"))
     if provider_key.parent() != info.context.user_key:
         raise ExecutionUnauthorized
     provider = update(
         fields=filter(lambda x: x != "id", cls.Input._meta.fields.keys()),
         provider=provider_key,
         body=pick(_provider_fields, input),
         as_obj=True,
     )
     return UpdateProvider(provider=provider)
Exemple #7
0
 def mutate_and_get_payload(cls, root, info, **input):
     fields = merge(
         pick(["email", "name"], input),
         {
             "password_hash": generate_password_hash(
                 input.get("password"), method="sha256"
             )
         },
     )
     user = helpers.make_create(
         UserModel, ["email", "name", "password_hash"], "email"
     )(fields, as_obj=True)
     return SignUp(user=user)
Exemple #8
0
def sign_up():
    data = request.get_json()
    fields = merge(
        pick(["email", "name"], data),
        {
            "password_hash": generate_password_hash(
                data.get("password"), method="sha256"
            )
        },
    )
    user = helpers.make_create(UserModel, ["email", "name", "password_hash"], "email")(
        fields, as_obj=True
    )
    access_token = create_access_token(identity=user.key.urlsafe())
    return jsonify({"access_token": access_token})
Exemple #9
0
    def mutate_and_get_payload(cls, root, info, **input):
        account_key = get_key(input.get("account"))
        provider_key = get_key(input.get("provider"))
        if (
            account_key.parent() != info.context.user_key
            or provider_key.parent() != info.context.user_key
        ):
            raise ExecutionUnauthorized

        service = create(
            fields=filter(lambda x: x != "account", cls.Input._meta.fields.keys()),
            account=account_key,
            provider=provider_key,
            body=pick(_service_fields, input),
            as_obj=True,
        )
        return CreateService(service=service)
Exemple #10
0
    def mutate_and_get_payload(cls, root, info, **input):
        def check_and_get_provider():
            if not input.get("provider"):
                return None
            provider_key = get_key(input.get("provider"))
            if provider_key.parent() != info.context.user_key:
                raise ExecutionUnauthorized
            return provider_key

        service_key = get_key(input.get("id"))
        if service_key.parent().parent() != info.context.user_key:
            raise ExecutionUnauthorized

        service = update(
            fields=filter(lambda x: x != "id", cls.Input._meta.fields.keys()),
            service=service_key,
            provider=check_and_get_provider(),
            body=pick(_service_fields, input),
            as_obj=True,
        )
        return UpdateService(service=service)
Exemple #11
0
def request(key, statics, config, method, body):
    make_headers = _make_args(['header'])
    make_payload = _make_args(['body', 'query'])
    headers = make_headers(config, key, statics)
    payload = merge(
        pick(method.get('args'), body),
        make_payload(config, key, statics),
    )
    kwargs = {
        'method': method.get('method'),
        'url': _make_url(method),
        'data' if method.get('method') == 'POST' else 'params': payload,
        'headers': headers,
    }

    try:
        res = requests.request(**kwargs)
        result = res.json()

        contains_error_condition = _make_condition_check(config)
        contains_error_field = _make_kv_check(config)
        has_failed = not res.ok or \
            contains_error_condition(result) or \
            contains_error_field(result)
        prices = _make_prices(config)

        return merge(
            {
                'status': 'failure' if has_failed else 'success',
                'response': {
                    'status_code': res.status_code,
                    'content': result,
                },
            },
            prices(result) if not has_failed else {},
        )
    except (requests.ConnectionError, requests.Timeout) as e:
        raise ServiceCallFailure(*e)
Exemple #12
0
def request(key, statics, config, method, body):
    make_headers = _make_args(["header"])
    make_payload = _make_args(["body", "query"])
    encode = _encode_payload(config)
    headers = make_headers(config, key, statics)
    payload = merge(pick(method.get("args"), body),
                    make_payload(config, key, statics))
    kwargs = {
        "method": method.get("method"),
        "url": _make_url(method),
        "data" if method.get("method") == "POST" else "params":
        encode(payload),
        "headers": headers,
    }

    try:
        res = requests.request(**kwargs)
        result = res.json()

        contains_error_condition = _make_condition_check(config)
        contains_error_field = _make_kv_check(config)
        has_failed = (not res.ok or contains_error_condition(result)
                      or contains_error_field(result))
        prices = _make_prices(config)

        return merge(
            {
                "status": "failure" if has_failed else "success",
                "response": {
                    "status_code": res.status_code,
                    "content": result
                },
            },
            prices(result) if not has_failed else {},
        )
    except (requests.ConnectionError, requests.Timeout) as e:
        raise ServiceCallFailure(*e)
Exemple #13
0
            partial(services.create, update_fields, site),
            request.get_json,
        )(), 201


static_parser = reqparse.RequestParser()
static_parser.add_argument('field', required=True)
static_parser.add_argument('value')
static_parser.add_argument(
    'location',
    choices=['header', 'body', 'query'],
    required=True,
)

single_static_fields = merge(
    pick(['id', 'name'], resource_fields),
    static_fields,
)


class ServiceStatic(Resource):
    @marshal_with(single_static_fields)
    def get(self, id, field):
        return services.get_static(id, field)

    def delete(self, id, field):
        return services.remove_static(id, field), 204


class ServiceStaticPut(Resource):
    @marshal_with(single_static_fields)
Exemple #14
0
                providers.Provider,
                create_fields,
                id_field,
            ),
            request.get_json,
        )(), 201


method_parser = reqparse.RequestParser()
method_parser.add_argument('action', required=True)
method_parser.add_argument('method', choices=['GET', 'POST'])
method_parser.add_argument('path')
method_parser.add_argument('args', action='append')

single_method_fields = merge(
    pick(['name', 'base_url'], resource_fields),
    method_fields,
)


class ProviderMethod(Resource):
    @marshal_with(single_method_fields)
    def get(self, id, action):
        return providers.get_method(id, action)

    def delete(self, id, action):
        return providers.remove_method(id, action), 204


class ProviderMethodPut(Resource):
    @marshal_with(single_method_fields)
Exemple #15
0
def get_balance(id):
    service = helpers.get_entity(Service, id, urlsafe=True)
    return pick(["id", "balance", "quota"], service.to_dict())