[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adding use-cases 1 to 4 in the new callout scructure #48

Merged
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
/callouts/python/.pytest_cache/**
/callouts/python/.pytest_cache
/.vscode/**
/.idea/**
plugins/bazel-*
**/__pycache__
21 changes: 21 additions & 0 deletions callouts/python/extproc/example/add_body/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
FROM marketplace.gcr.io/google/debian12
RUN apt-get update && apt-get install -y python3-pip python3-grpc-tools
WORKDIR /home/callouts/python
COPY ./extproc/service ./extproc/service
COPY ./extproc/proto ./extproc/proto
COPY ./extproc/ssl_creds ./extproc/ssl_creds
COPY ./extproc/example/add_body ./extproc/example/add_body
EXPOSE 443
EXPOSE 8080
EXPOSE 80
CMD [ "/usr/bin/python3", "-um", "extproc.example.add_body.service_callout_example" ]
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright 2024 Google LLC.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from grpc import ServicerContext
from extproc.proto import service_pb2
from extproc.service import callout_server


class CalloutServerExample(callout_server.CalloutServer):
"""Example callout server.

Provides a non-comprehensive set of responses for each of the possible
callout interactions.

On a request body callout we provide a mutation to append '-added-body' to the body. On response body
callouts we send a mutation to replace the body with 'new-body'.
"""

def on_request_body(
self, body: service_pb2.HttpBody, context: ServicerContext
) -> service_pb2.BodyResponse:
"""Custom processor on the request body."""
return callout_server.add_body_mutation(body='-added-body')

def on_response_body(
self, body: service_pb2.HttpBody, context: ServicerContext
) -> service_pb2.BodyResponse:
"""Custom processor on the response body."""
return callout_server.add_body_mutation(body='new-body', clear_body=True)


if __name__ == '__main__':
# Run the gRPC service
CalloutServerExample(port=443, insecure_port=8080, health_check_port=80).run()
21 changes: 21 additions & 0 deletions callouts/python/extproc/example/add_header/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
FROM marketplace.gcr.io/google/debian12
RUN apt-get update && apt-get install -y python3-pip python3-grpc-tools
WORKDIR /home/callouts/python
COPY ./extproc/service ./extproc/service
COPY ./extproc/proto ./extproc/proto
COPY ./extproc/ssl_creds ./extproc/ssl_creds
COPY ./extproc/example/add_header ./extproc/example/add_header
EXPOSE 443
EXPOSE 8080
EXPOSE 80
CMD [ "/usr/bin/python3", "-um", "extproc.example.add_header.service_callout_example" ]
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copyright 2024 Google LLC.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from grpc import ServicerContext
from extproc.proto import service_pb2
from extproc.service import callout_server


class CalloutServerExample(callout_server.CalloutServer):
"""Example callout server.

Provides a non-comprehensive set of responses for each of the possible
callout interactions.

For request header callouts we provide a mutation to add a header
'{header-request: request}', remove a header 'foo', and to clear the
route cache. On response header callouts, we respond with a mutation to add
the header '{header-response: response}'.
"""
def on_request_headers(
self, headers: service_pb2.HttpHeaders, context: ServicerContext
) -> service_pb2.HeadersResponse:
"""Custom processor on request headers."""
return callout_server.add_header_mutation(
add=[('header-request', 'request')], remove=['foo'],
clear_route_cache=True
)

def on_response_headers(
self, headers: service_pb2.HttpHeaders, context: ServicerContext
) -> service_pb2.HeadersResponse:
"""Custom processor on response headers."""
return callout_server.add_header_mutation(
add=[('header-response', 'response')]
)

if __name__ == '__main__':
# Run the gRPC service
CalloutServerExample(port=443, insecure_port=8080, health_check_port=80).run()
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

# Copyright 2023 Google LLC.
# Copyright 2024 Google LLC.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
21 changes: 21 additions & 0 deletions callouts/python/extproc/example/normalize_header/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
FROM marketplace.gcr.io/google/debian12
RUN apt-get update && apt-get install -y python3-pip python3-grpc-tools
WORKDIR /home/callouts/python
COPY ./extproc/service ./extproc/service
COPY ./extproc/proto ./extproc/proto
COPY ./extproc/ssl_creds ./extproc/ssl_creds
COPY ./extproc/example/normalize_header ./extproc/example/normalize_header
EXPOSE 443
EXPOSE 8080
EXPOSE 80
CMD [ "/usr/bin/python3", "-um", "extproc.example.normalize_header.service_callout_example" ]
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright 2024 Google LLC.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from grpc import ServicerContext
from extproc.proto import service_pb2
from extproc.service import callout_server


class CalloutServerExample(callout_server.CalloutServer):
"""Example callout server.

Provides a non-comprehensive set of responses for each of the possible
callout interactions.

For request header callouts we check the host header
and create a new HTTP header (client-device-type)
to shard requests based on device.
"""

def on_request_headers(
self, headers: service_pb2.HttpHeaders, context: ServicerContext
) -> service_pb2.HeadersResponse:
"""Custom processor on request headers."""
return callout_server.normalize_header_mutation(
headers=headers,
clear_route_cache=True
)


if __name__ == '__main__':
# Run the gRPC service
CalloutServerExample(port=443, insecure_port=8080, health_check_port=80).run()
21 changes: 21 additions & 0 deletions callouts/python/extproc/example/update_header/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
FROM marketplace.gcr.io/google/debian12
RUN apt-get update && apt-get install -y python3-pip python3-grpc-tools
WORKDIR /home/callouts/python
COPY ./extproc/service ./extproc/service
COPY ./extproc/proto ./extproc/proto
COPY ./extproc/ssl_creds ./extproc/ssl_creds
COPY ./extproc/example/update_header ./extproc/example/update_header
EXPOSE 443
EXPOSE 8080
EXPOSE 80
CMD [ "/usr/bin/python3", "-um", "extproc.example.update_header.service_callout_example" ]
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Copyright 2024 Google LLC.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from grpc import ServicerContext
from extproc.proto import service_pb2
from extproc.service import callout_server


class CalloutServerExample(callout_server.CalloutServer):
"""Example callout server.

Provides a non-comprehensive set of responses for each of the possible
callout interactions.

For request header callouts we provide a mutation to update a header
'{header-request: request}', and to clear the route cache.

On response header callouts, we respond with a mutation to update
the header '{header-response: response}'.
"""
def on_request_headers(
self, headers: service_pb2.HttpHeaders, context: ServicerContext
) -> service_pb2.HeadersResponse:
"""Custom processor on request headers."""
return callout_server.update_header_mutation(
headers=headers,
update=[('header-request', 'request')],
clear_route_cache=True
)

def on_response_headers(
self, headers: service_pb2.HttpHeaders, context: ServicerContext
) -> service_pb2.HeadersResponse:
"""Custom processor on response headers."""
return callout_server.update_header_mutation(
headers=headers,
update=[('header-response', 'response')]
)

if __name__ == '__main__':
# Run the gRPC service
CalloutServerExample(port=443, insecure_port=8080, health_check_port=80).run()
Loading