Created
January 16, 2024 03:17
-
-
Save ranfysvalle02/ee76acdd7b17cfca13a6f9d3082c40a6 to your computer and use it in GitHub Desktop.
MongoDB + OSO Cloud
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from oso_cloud import Oso | |
import pymongo | |
api_key = "<api-key-goes-here>" | |
oso = Oso(url="https://cloud.osohq.com", api_key=api_key) | |
def test_oso(): | |
actor = {"type": "User", "id": "1233"} | |
resource = {"type": "Repository", "id": "456"} | |
if not oso.authorize(actor, "view", resource): | |
# Handle authorization failure | |
return "unauthorized by oso" | |
return "authorized by oso" | |
print( | |
test_oso() | |
) | |
def query_collection(coll,query,projection): | |
# Execute the query | |
results = coll.find(query,projection) | |
results_array = list(results) | |
print(results_array) | |
if test_oso() == "authorized by oso": | |
client = pymongo.MongoClient("mongodb+srv://<user>:<pwd>@shared.x.mongodb.net/test") | |
db = client["database"] | |
collection = db["collection"] | |
# Define your query conditions using $match | |
query = { | |
#"$match": { | |
#"field_name": "value_to_match", # Replace with your actual field and value | |
# Add more conditions as needed | |
#} | |
} | |
query_collection(collection,query,{"_id":0,"text":1}) | |
else: | |
# Handle authorization failure - connect to public repository | |
client = pymongo.MongoClient("mongodb+srv://<user>:<pwd>@shared.x.mongodb.net/test") | |
db = client["database"] | |
collection = db["collection"] | |
# Define your query conditions using $match | |
query = { | |
#"$match": { | |
#"field_name": "value_to_match", # Replace with your actual field and value | |
# Add more conditions as needed | |
#} | |
} | |
query_collection(collection,query,{"_id":0}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment