crflynn / pypistats.org

PyPI downloads analytics dashboard
https://pypistats.org/
138 stars 10 forks source link

CORS Headers Issue #41

Open ibrahimyaacob92 opened 3 years ago

ibrahimyaacob92 commented 3 years ago

Problem with CORS as mentioned below.

image

craigahobbs commented 2 weeks ago

It's been two years since I submitted my pull request to fix this issue. I never received a response, so I'm deleting it. It would be interesting to get a response from @crflynn as to why. It seems like a no-brainer to me.

Here's the diff of the fix if anyone wants to revive it.

diff --git a/pypistats/views/api.py b/pypistats/views/api.py
index 6ec8a2b..2878ed5 100644
--- a/pypistats/views/api.py
+++ b/pypistats/views/api.py
@@ -16,6 +16,13 @@ from pypistats.models.download import SystemDownloadCount
 blueprint = Blueprint("api", __name__, url_prefix="/api")

+def jsonify_with_cors_headers(response):
+    """JSON-ify a response and add CORS headers"""
+    response_jsonified = jsonify(response)
+    response_jsonified.headers["access-control-allow-origin"] = "*"
+    return response_jsonified
+
+
 @blueprint.route("/")
 def api():
     """Get API documentation."""
@@ -47,7 +54,7 @@ def api_downloads_recent(package):
     else:
         abort(404)

-    return jsonify(response)
+    return jsonify_with_cors_headers(response)

 @blueprint.route("/packages/<package>/overall")
@@ -82,7 +89,7 @@ def api_downloads_overall(package):
     else:
         abort(404)

-    return jsonify(response)
+    return jsonify_with_cors_headers(response)

 @blueprint.route("/packages/<package>/python_major")
@@ -120,7 +127,7 @@ def generic_downloads(model, package, arg, name):
     else:
         abort(404)

-    return jsonify(response)
+    return jsonify_with_cors_headers(response)

 # TODO