Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions docs/source/topics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -778,11 +778,11 @@ Cors can be set on the route level or on the Goblet application level. Setting `
def home():
return "cors headers"

Use the `CORSConfig` class to set customized cors headers from the `goblet.resources.routes` class.
Use the `CORSConfig` class to set customized cors headers from the `goblet.handlers.routes` class.

.. code:: python

from goblet.resources.routes import CORSConfig
from goblet.handlers.routes import CORSConfig

@app.route('/custom_cors', cors=CORSConfig(allow_origin='localhost'))
def custom_cors():
Expand Down
4 changes: 3 additions & 1 deletion examples/example_bq_spark_stored_procedure/additional.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import logging

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


def additional_func():
logger.info("additional_func")
logger.info("additional_func")
1 change: 1 addition & 0 deletions examples/example_bq_spark_stored_procedure/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
from goblet import Goblet, goblet_entrypoint

# from spark import spark_handler

app = Goblet(function_name="create-bq-spark-stored-procedure")
Expand Down
14 changes: 7 additions & 7 deletions examples/example_bq_spark_stored_procedure/spark.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
def spark_handler():
from pyspark.sql import SparkSession
import pyspark.sql.functions as F

spark = SparkSession.builder.appName("spark-bigquery-demo").getOrCreate()

# Load data from BigQuery.
texts = spark.read.format("bigquery") \
.option("table", "tutorial.poc") \
.load()
texts = spark.read.format("bigquery").option("table", "tutorial.poc").load()
texts.createOrReplaceTempView("words")

# Perform word count.
Expand All @@ -15,9 +14,10 @@ def spark_handler():
text_count.printSchema()

# Saving the data to BigQuery
text_count.write.mode("append").format("bigquery") \
.option("writeMethod", "direct") \
.save("tutorial.wordcount_output")
text_count.write.mode("append").format("bigquery").option(
"writeMethod", "direct"
).save("tutorial.wordcount_output")


if __name__ == "__main__":
spark_handler()
spark_handler()
7 changes: 7 additions & 0 deletions examples/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
LogMatchCondition,
CustomMetricCondition,
)
from goblet.handlers.routes import CORSConfig
import asyncio
import logging

Expand Down Expand Up @@ -174,6 +175,12 @@ def response():
)


# Example CORS

@app.route('/custom_cors', cors=CORSConfig(allow_origin='localhost', allow_methods=["GET"], extra_headers={"X-TEST":"X-HEADER-VALUE"}))
def custom_cors():
return jsonify('localhost is allowed with GET method')

# Scheduled job
@app.schedule("5 * * * *")
def scheduled_job():
Expand Down
2 changes: 1 addition & 1 deletion goblet/__version__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION = (0, 12, 5)
VERSION = (0, 12, 6)


__version__ = ".".join(map(str, VERSION))
10 changes: 10 additions & 0 deletions goblet/handlers/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,9 +465,11 @@ def __init__(
self,
allow_origin="*",
allow_headers=None,
allow_methods=None,
expose_headers=None,
max_age=None,
allow_credentials=None,
extra_headers=None,
):
self.allow_origin = allow_origin

Expand All @@ -483,6 +485,8 @@ def __init__(

self._max_age = max_age
self._allow_credentials = allow_credentials
self._allow_methods = allow_methods
self._extra_headers = extra_headers

@property
def allow_headers(self):
Expand All @@ -501,6 +505,12 @@ def get_access_control_headers(self):
headers.update({"Access-Control-Max-Age": str(self._max_age)})
if self._allow_credentials is True:
headers.update({"Access-Control-Allow-Credentials": "true"})
if self._allow_methods:
headers.update(
{"Access-Control-Allow-Methods": ",".join(self._allow_methods)}
)
if self._extra_headers:
headers.update(self._extra_headers)

return headers

Expand Down
11 changes: 10 additions & 1 deletion goblet/tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,14 @@ def mock_function2():
def mock_function_override():
return Response("200")

@app.route("/test3", cors=CORSConfig(allow_origin="localhost"))
@app.route(
"/test3",
cors=CORSConfig(
allow_origin="localhost",
allow_methods=["GET", "PUT"],
extra_headers={"X-TEST": "X-VALUE"},
),
)
def mock_function3():
return jsonify("200")

Expand Down Expand Up @@ -173,6 +180,8 @@ def mock_function3():
mock_event3.json = {}
resp3 = app(mock_event3, None)
assert resp3[2]["Access-Control-Allow-Origin"] == "localhost"
assert resp3[2]["Access-Control-Allow-Methods"] == "GET,PUT"
assert resp3[2]["X-TEST"] == "X-VALUE"

def test_cors_options(self):
app = Goblet(function_name="goblet_cors")
Expand Down