i am trying to run a piece of code in python which uses Cosmos DB from Microsoft Azure. I am currently using gremlinpython 3.2.6 and the latest version of Cosmos (default on microsoft azure) but there seems to be some compatibility issues between the two.
When i run my code i get the following error;
GremlinServerError: 498:
ActivityId : 5c05bb15-3aa1-41b8-9c10-ab3015152eab
ExceptionType : GraphMalformedException
ExceptionMessage :
Gremlin Malformed Request: GraphSON v3 IO is not supported.
GremlinRequestId : 5c05bb15-3aa1-41b8-9c10-ab3015152eab
Context : global
GraphInterOpStatusCode : MalformedRequest
HResult : 0x80131500
I have read that I should try using GraphSON v2 instead of V3 but don't know how, can anyone help?
welcome to this community. You just need to ensure that you use the schema of the GraphSON v2, since it is the version supported in Azure Cosmos DB. Check the json you are using and ensure that follows the supported schema. You have some examples in this link.
Using C#, If you put your connection config in Startup.cs you can configure it like this:
services.AddSingleton<GremlinClient>(
(serviceProvider) =>
{
var gremlinServer = new GremlinServer(
hostname: "<account>.gremlin.cosmosdb.azure.com",
port: <port>,
enableSsl: true,
username: "/dbs/<db>/colls/<collection>",
password: ""
);
var connectionPoolSettings = new ConnectionPoolSettings
{
MaxInProcessPerConnection = 32,
PoolSize = 4,
ReconnectionAttempts = 3,
ReconnectionBaseDelay = TimeSpan.FromSeconds(1),
};
var mimeType = "application/vnd.gremlin-v2.0+json";
return new GremlinClient
(
gremlinServer: gremlinServer,
graphSONReader: new GraphSON2Reader(),
graphSONWriter: new GraphSON2Writer(),
mimeType: mimeType,
connectionPoolSettings: connectionPoolSettings
);
}
);
Otherwise you should create the gremlin client with the following reader, writer and mimeType:
var mimeType = "application/vnd.gremlin-v2.0+json";
var client = new GremlinClient
(
gremlinServer: <your server>,
graphSONReader: new GraphSON2Reader(),
graphSONWriter: new GraphSON2Writer(),
mimeType: mimeType,
connectionPoolSettings: <your connection pool>
);
By default gremlin_python uses the GraphSONSerializersV3d0, so you have to explicitly pass the GraphSONSerializersV2d0 when creating the client:
from gremlin_python.driver import client, serializer
client.Client(
message_serializer=serializer.GraphSONSerializersV2d0(),
password="...",
traversal_source='g',
url='wss://...:443/',
username="/dbs/.../colls/...",
)
Provide it as mime type when you create client
var client = new GremlinClient(gremlinServer:gremlinServer,mimeType:GremlinClient.GraphSON2MimeType)
You need to downgrade the version to the supported connector version. This applies to all programming languages. For python as of this writing, it is 3.2.7.
Related
A FastAPI-based API written in Python has been deployed as an Azure App Service. The API needs to read and write data from CosmosDB, and I attempted to use Managed Identity for this purpose, but encountered an error, stating Unrecognized credential type
These are the key steps that I took towards that goal
Step One: I used Terraform to configure the managed identity for Azure App Service, and assigned the 'contributor' role to the identity so that it can access and write data to CosmosDB. The role assignment was carried out in the file where the Azure App Service is provisioned.
resource "azurerm_linux_web_app" "this" {
name = var.appname
location = var.location
resource_group_name = var.rg_name
service_plan_id = azurerm_service_plan.this.id
app_settings = {
"PROD" = false
"DOCKER_ENABLE_CI" = true
"DOCKER_REGISTRY_SERVER_URL" = data.azurerm_container_registry.this.login_server
"WEBSITE_HTTPLOGGING_RETENTION_DAYS" = "30"
"WEBSITE_ENABLE_APP_SERVICE_STORAGE" = false
}
lifecycle {
ignore_changes = [
app_settings["WEBSITE_HTTPLOGGING_RETENTION_DAYS"]
]
}
https_only = true
identity {
type = "SystemAssigned"
}
data "azurerm_cosmosdb_account" "this" {
name = var.cosmosdb_account_name
resource_group_name = var.cosmosdb_resource_group_name
}
// built-in role that allow the app-service to read and write to an Azure Cosmos DB
resource "azurerm_role_assignment" "cosmosdbContributor" {
scope = data.azurerm_cosmosdb_account.this.id
principal_id = azurerm_linux_web_app.this.identity.0.principal_id
role_definition_name = "Contributor"
}
Step Two: I used the managed identity library to fetch the necessary credentials in the Python code.
from azure.identity import ManagedIdentityCredential
from azure.cosmos.cosmos_client import CosmosClient
client = CosmosClient(get_endpoint(),credential=ManagedIdentityCredential())
client = self._get_or_create_client()
database = client.get_database_client(DB_NAME)
container = database.get_container_client(CONTAINER_NAME)
container.query_items(query)
I received the following error when running the code locally and from Azure (the error can be viewed from the Log stream of the Azure App Service):
raise TypeError(
TypeError: Unrecognized credential type. Please supply the master key as str, or a dictionary or resource tokens, or a list of permissions.
Any help or discussion is welcome
If you are using the Python SDK, you can directly do this ,check the sample here
aad_credentials = ClientSecretCredential(
tenant_id="<azure-ad-tenant-id>",
client_id="<client-application-id>",
client_secret="<client-application-secret>")
client = CosmosClient("<account-endpoint>", aad_credentials)
I'm using firebase to upload images using python and pyrebase,
the post request is:
#router.post('/', response_model=schemas.Product)
def add_product(
file: UploadFile = File(),
db: Session = Depends(get_db)
):
try:
firebase = pyrebase.initialize_app(settings.config_firebase)
storage = firebase.storage()
path_on_cloud = "products/"+file.filename
photo_infos = storage.child(path_on_cloud).put(file.file)
print(firebase) # op 1
print(storage) # op 2
print(path_on_cloud) # op 3
print(photo_infos) # op 4
first output (op1)
<pyrebase.pyrebase.Firebase object at 0x000001889A691790>
which means that the connection works fine
2nd:
<pyrebase.pyrebase.Storage object at 0x000001889A76A8B0>
3rd
products/baqlawa.png
4th: and here is the problem
None
the storage rules in firebase are:
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if false;
}
}
}
Note: the reading request of files works fine, the problem is in uploading, and I did not using any authentication requirement.
Other thing I want to add is that in firebase console the files are added but as application/octet-stream and not jpeg or png
thank you in advance.
I want to use python client to create a Nessus Security Scanner and check the status by getStatus and get the result by getReport method. While, I have read these helps by php(SoftLayer API Nessus Scan Status / Report via PHP). But how can i use these by python client?
When I call setInitParameter(scan_id) in by python, the exception as flows:
SoftLayerAPIError(Client): Function ("setInitParameter") is not a valid method for this service
i recomend you to read documentation of the client first:
https://github.com/softlayer/softlayer-python
https://softlayer-api-python-client.readthedocs.io/en/latest/
the init parameters are set like this:
clientService.getObject(id=myInitParameter)
here you can find more examples using the client:
https://softlayer.github.io/python/
Here you can find additional documentation:
http://sldn.softlayer.com/blog
And renember that with the Softlayer's python client unlike the php client the data are sending in json format so the request:
$client = SoftLayer_SoapClient::getClient('SoftLayer_Account', null, $apiUsername, $apiKey);
$accountInfo = $client->getObject();
$hardware = $client->getHardware();
foreach ($hardware as $server){
$scanclient = SoftLayer_SoapClient::getClient('SoftLayer_Network_Security_Scanner_Request', '', $apiUsername, $apiKey)
$scantemplate = new stdClass();
$scantemplate->accountId = $accountInfo->id;
$scantemplate->hardwareId = $server->id;
$scantemplate->ipAddress = $server->primaryIpAddress;
try{
// Successfully creates new scan
$scan = $scanclient->createObject($scantemplate);
} catch (Exception $e){
echo $e->getMessage() . "\n\r";
}
would be like this:
clientAccount = client['SoftLayer_Account']
accountInfo = clientAccount.getObject() #for this case we do not need init parameter
hardware = clientAccount.getHardware() #for this case we do not need init parameter
for server in hardware:
scanclient = client['SoftLayer_Network_Security_Scanner_Request']
scantemplate = {
"accountId": accountInfo["id"],
"hardwareId": server["id"],
"ipAddress": server["primaryIpAddress"]
}
scanclient.createObject(scantemplate)
How to use python as a backend for an Android App that is built using C#? The Python Backend is written using the Flask framework. The Android app is built using xamarin.
No matter what type of technology your server or the client use if they can communicate with each other using some sort of standard "protocol".
There are many ways to communicate both sides (client and server) like sockets, xml, json, etc. They just need to understand each other.
In your particular case I suggest to build a REST or RESTful API (https://flask-restful.readthedocs.org/en/0.3.3/) on the server and a REST client library on the client.
There are many ways and libraries to call REST APIs from C#:
The built-in method would be using HttpWebRequest as you can see on this link:
private async Task<JsonValue> FetchWeatherAsync (string url)
{
// Create an HTTP web request using the URL:
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create (new Uri (url));
request.ContentType = "application/json";
request.Method = "GET";
// Send the request to the server and wait for the response:
using (WebResponse response = await request.GetResponseAsync ())
{
// Get a stream representation of the HTTP web response:
using (Stream stream = response.GetResponseStream ())
{
// Use this stream to build a JSON document object:
JsonValue jsonDoc = await Task.Run (() => JsonObject.Load (stream));
Console.Out.WriteLine("Response: {0}", jsonDoc.ToString ());
// Return the JSON document:
return jsonDoc;
}
}
}
But I don´t recommend it if you don´t want your app to be full of crap (boiler plate code) everywhere.
A helper library could be, for example, RESTSharp. It allows you to build REST calls easily and cast the response to your typed objects. Here´s and example:
var client = new RestClient("http://example.com");
// client.Authenticator = new HttpBasicAuthenticator(username, password);
var request = new RestRequest("resource/{id}", Method.POST);
request.AddParameter("name", "value"); // adds to POST or URL querystring based on Method
request.AddUrlSegment("id", "123"); // replaces matching token in request.Resource
// easily add HTTP Headers
request.AddHeader("header", "value");
// add files to upload (works with compatible verbs)
request.AddFile(path);
// execute the request
RestResponse response = client.Execute(request);
var content = response.Content; // raw content as string
// or automatically deserialize result
// return content type is sniffed but can be explicitly set via RestClient.AddHandler();
RestResponse<Person> response2 = client.Execute<Person>(request);
var name = response2.Data.Name;
// easy async support
client.ExecuteAsync(request, response => {
Console.WriteLine(response.Content);
});
// async with deserialization
var asyncHandle = client.ExecuteAsync<Person>(request, response => {
Console.WriteLine(response.Data.Name);
});
// abort the request on demand
asyncHandle.Abort();
You can search "C# REST client" on google and judge by yourself. But IMHO, the easier and nicer to code REST client I´ve ever used is Refit.
Why? you define API calls and responses with just an interface. No coding required at all! Even more, all your API calls will be async by default, something needed for mobile apps to be responsive. From the author´s readme:
public interface IGitHubApi
{
[Get("/users/{user}")]
Task<User> GetUser(string user);
}
var gitHubApi = RestService.For<IGitHubApi>("https://api.github.com");
var octocat = await gitHubApi.GetUser("octocat");
I´ve used this library on Xamarin Android/iOS projects and it works well. No issues at all.
Hope it helps
I can see the statistics from Mongo shell as
db.stats()
or
db.collection_name.stats()
How do I view statistics of a database, or of a collection, from PHP and Python.
EDIT:
I have done it in PHP but still cant do it in Python.
Help?
This is how you do it in Python if you are using the PyMongo driver:
connection = pymongo.Connection(host = "127.0.0.1", port = 27017)
db = connection["test_db"]
test_collection = db["test_collection"]
db.command("dbstats") # prints database stats for "test_db"
db.command("collstats", "test_collection") # prints collection-level stats for "test_collection" under "test_db".
References:
db.command()
MongoDB: how to get db.stats() from API
This is how you do it in PHP
$con= new Mongo()
$stats=$con->dbName->command(array('dbStats' => 1)); // for db.stats()
$stats=$con->dbName->command(array('collStats' => 'collection_name')); // for db.collection_name.stats()
But how to do this in python?
The easiest way I have found to do this with a Mongoengine model was this:
import mongoengine
from models import MyModel
connection = mongoengine.connection.get_connection()
db = connection[MyModel._get_db().name]
stats = db.command("collstats", MyModel._get_collection_name())
This should allow transparent changes in the collection and database using mongoengine's config settings.
This is PHP code to execute dbStats command with new MongoDB driver:
$mongo = new \MongoDB\Driver\Manager('mongodb://localhost:27017');
$cmdstats = new \MongoDB\Driver\Command(['dbStats' => 1]);
$dbstats = $mongo->executeCommand('databaseName', $cmdstats);
$dbstats->setTypeMap(array(
'array' => 'array',
'document' => 'array',
'root' => 'array'
));
// There must be only one item in $dbstats
foreach ($dbstats as $dbs)
{
echo($dbs['dataSize']);
}