Skip to content

Commit

Permalink
Add Python wrapper API for uploadBlob
Browse files Browse the repository at this point in the history
  • Loading branch information
ewertons committed Jul 5, 2016
1 parent d8c72f2 commit eae4b55
Show file tree
Hide file tree
Showing 15 changed files with 1,157 additions and 42 deletions.
64 changes: 64 additions & 0 deletions python/device/iothub_client_python/src/iothub_client_python.cpp
Expand Up @@ -823,6 +823,36 @@ ReceiveMessageCallback(
return boost::python::extract<IOTHUBMESSAGE_DISPOSITION_RESULT>(returnObject);
}

typedef struct
{
boost::python::object blobUploadCallback;
boost::python::object userContext;
} BlobUploadContext;

extern "C"
void
BlobUploadConfirmationCallback(
IOTHUB_CLIENT_FILE_UPLOAD_RESULT result,
void* userContextCallback
)
{
BlobUploadContext *blobUploadContext = (BlobUploadContext *)userContextCallback;
boost::python::object blobUploadCallback = blobUploadContext->blobUploadCallback;
boost::python::object userContext = blobUploadContext->userContext;
{
ScopedGILAcquire acquire;
try {
blobUploadCallback(result, userContext);
}
catch (const boost::python::error_already_set)
{
// Catch and ignore exception that is thrown in Python callback.
// There is nothing we can do about it here.
PyErr_Print();
}
}
delete blobUploadContext;
}

class IoTHubClient
{
Expand Down Expand Up @@ -1098,6 +1128,34 @@ class IoTHubClient
}
}

void UploadToBlobAsync(
std::string destinationFileName,
std::string source,
size_t size,
boost::python::object& iotHubClientFileUploadCallback,
boost::python::object& userContext
)
{
if (!PyCallable_Check(iotHubClientFileUploadCallback.ptr()))
{
PyErr_SetString(PyExc_TypeError, "upload_to_blob expected type callable");
boost::python::throw_error_already_set();
return;
}
BlobUploadContext *blobUploadContext = new BlobUploadContext();
blobUploadContext->blobUploadCallback = iotHubClientFileUploadCallback;
blobUploadContext->userContext = userContext;

IOTHUB_CLIENT_RESULT result;
{
ScopedGILRelease release;
result = IoTHubClient_UploadToBlobAsync(iotHubClientHandle, destinationFileName.c_str(), (const unsigned char*)source.c_str(), size, BlobUploadConfirmationCallback, blobUploadContext);
}
if (result != IOTHUB_CLIENT_OK)
{
throw IoTHubClientError(__func__, result);
}
}
#ifdef SUPPORT___STR__
std::string str() const
{
Expand Down Expand Up @@ -1217,6 +1275,11 @@ BOOST_PYTHON_MODULE(IMPORT_NAME)
.value("MQTT", MQTT)
;

enum_<IOTHUB_CLIENT_FILE_UPLOAD_RESULT>("IoTHubClientFileUploadResult")
.value("OK", FILE_UPLOAD_OK)
.value("ERROR", FILE_UPLOAD_ERROR)
;

// classes
class_<IoTHubMap>("IoTHubMap")
.def(init<>())
Expand Down Expand Up @@ -1258,6 +1321,7 @@ BOOST_PYTHON_MODULE(IMPORT_NAME)
.def("set_option", &IoTHubClient::SetOption)
.def("get_send_status", &IoTHubClient::GetSendStatus)
.def("get_last_message_receive_time", &IoTHubClient::GetLastMessageReceiveTime)
.def("upload_blob_async", &IoTHubClient::UploadToBlobAsync)
// attributes
.def_readonly("protocol", &IoTHubClient::protocol)
// Python helpers
Expand Down
Expand Up @@ -195,6 +195,11 @@ IOTHUB_CLIENT_RESULT IoTHubClient_SetOption(IOTHUB_CLIENT_HANDLE iotHubClientHan
return IOTHUB_CLIENT_OK;
}

IOTHUB_CLIENT_RESULT IoTHubClient_UploadToBlobAsync(IOTHUB_CLIENT_HANDLE iotHubClientHandle, const char* destinationFileName, const unsigned char* source, size_t size, IOTHUB_CLIENT_FILE_UPLOAD_CALLBACK iotHubClientFileUploadCallback, void* context)
{
return IOTHUB_CLIENT_OK;
}

// "iothub_client_version.h"

const char* IoTHubClient_GetVersionString(void)
Expand Down
Expand Up @@ -109,6 +109,7 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<ModuleDefinitionFile>..\$(TargetName).def</ModuleDefinitionFile>
<AdditionalLibraryDirectories>$(PYTHON_PATH)\libs</AdditionalLibraryDirectories>
<AdditionalDependencies>crypt32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<CustomBuildStep>
<Command>Copy $(TargetPath) $(TargetDir)\$(TargetName).pyd</Command>
Expand All @@ -130,6 +131,7 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<ModuleDefinitionFile>..\$(TargetName).def</ModuleDefinitionFile>
<AdditionalLibraryDirectories>$(PYTHON_PATH)\libs</AdditionalLibraryDirectories>
<AdditionalDependencies>crypt32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<CustomBuildStep>
<Command>Copy $(TargetPath) $(TargetDir)\$(TargetName).pyd</Command>
Expand Down Expand Up @@ -158,6 +160,7 @@
<ModuleDefinitionFile>..\$(TargetName).def</ModuleDefinitionFile>
<AdditionalLibraryDirectories>$(PYTHON_PATH)\libs</AdditionalLibraryDirectories>
<LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>
<AdditionalDependencies>crypt32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<CustomBuildStep>
<Command>Copy $(TargetPath) $(TargetDir)\$(TargetName).pyd</Command>
Expand Down Expand Up @@ -186,6 +189,7 @@
<ModuleDefinitionFile>..\$(TargetName).def</ModuleDefinitionFile>
<AdditionalLibraryDirectories>$(PYTHON_PATH)\libs</AdditionalLibraryDirectories>
<LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>
<AdditionalDependencies>crypt32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<CustomBuildStep>
<Command>Copy $(TargetPath) $(TargetDir)\$(TargetName).pyd</Command>
Expand All @@ -198,26 +202,26 @@
<ImportGroup Label="ExtensionTargets">
<Import Project="..\packages\boost.1.60.0.0\build\native\boost.targets" Condition="Exists('..\packages\boost.1.60.0.0\build\native\boost.targets')" />
<Import Project="..\packages\boost_python.1.60.0.0\build\native\boost_python.targets" Condition="Exists('..\packages\boost_python.1.60.0.0\build\native\boost_python.targets')" />
<Import Project="..\packages\Microsoft.Azure.C.SharedUtility.1.0.10\build\native\Microsoft.Azure.C.SharedUtility.targets" Condition="Exists('..\packages\Microsoft.Azure.C.SharedUtility.1.0.10\build\native\Microsoft.Azure.C.SharedUtility.targets')" />
<Import Project="..\packages\Microsoft.Azure.IoTHub.IoTHubClient.1.0.10\build\native\Microsoft.Azure.IoTHub.IoTHubClient.targets" Condition="Exists('..\packages\Microsoft.Azure.IoTHub.IoTHubClient.1.0.10\build\native\Microsoft.Azure.IoTHub.IoTHubClient.targets')" />
<Import Project="..\packages\Microsoft.Azure.IoTHub.HttpTransport.1.0.10\build\native\Microsoft.Azure.IoTHub.HttpTransport.targets" Condition="Exists('..\packages\Microsoft.Azure.IoTHub.HttpTransport.1.0.10\build\native\Microsoft.Azure.IoTHub.HttpTransport.targets')" />
<Import Project="..\packages\Microsoft.Azure.uamqp.1.0.10\build\native\Microsoft.Azure.uamqp.targets" Condition="Exists('..\packages\Microsoft.Azure.uamqp.1.0.10\build\native\Microsoft.Azure.uamqp.targets')" />
<Import Project="..\packages\Microsoft.Azure.IoTHub.AmqpTransport.1.0.10\build\native\Microsoft.Azure.IoTHub.AmqpTransport.targets" Condition="Exists('..\packages\Microsoft.Azure.IoTHub.AmqpTransport.1.0.10\build\native\Microsoft.Azure.IoTHub.AmqpTransport.targets')" />
<Import Project="..\packages\Microsoft.Azure.umqtt.1.0.9\build\native\Microsoft.Azure.umqtt.targets" Condition="Exists('..\packages\Microsoft.Azure.umqtt.1.0.9\build\native\Microsoft.Azure.umqtt.targets')" />
<Import Project="..\packages\Microsoft.Azure.IoTHub.MqttTransport.1.0.10\build\native\Microsoft.Azure.IoTHub.MqttTransport.targets" Condition="Exists('..\packages\Microsoft.Azure.IoTHub.MqttTransport.1.0.10\build\native\Microsoft.Azure.IoTHub.MqttTransport.targets')" />
<Import Project="..\packages\Microsoft.Azure.C.SharedUtility.1.0.11\build\native\Microsoft.Azure.C.SharedUtility.targets" Condition="Exists('..\packages\Microsoft.Azure.C.SharedUtility.1.0.11\build\native\Microsoft.Azure.C.SharedUtility.targets')" />
<Import Project="..\packages\Microsoft.Azure.IoTHub.IoTHubClient.1.0.11\build\native\Microsoft.Azure.IoTHub.IoTHubClient.targets" Condition="Exists('..\packages\Microsoft.Azure.IoTHub.IoTHubClient.1.0.11\build\native\Microsoft.Azure.IoTHub.IoTHubClient.targets')" />
<Import Project="..\packages\Microsoft.Azure.IoTHub.HttpTransport.1.0.11\build\native\Microsoft.Azure.IoTHub.HttpTransport.targets" Condition="Exists('..\packages\Microsoft.Azure.IoTHub.HttpTransport.1.0.11\build\native\Microsoft.Azure.IoTHub.HttpTransport.targets')" />
<Import Project="..\packages\Microsoft.Azure.uamqp.1.0.11\build\native\Microsoft.Azure.uamqp.targets" Condition="Exists('..\packages\Microsoft.Azure.uamqp.1.0.11\build\native\Microsoft.Azure.uamqp.targets')" />
<Import Project="..\packages\Microsoft.Azure.IoTHub.AmqpTransport.1.0.11\build\native\Microsoft.Azure.IoTHub.AmqpTransport.targets" Condition="Exists('..\packages\Microsoft.Azure.IoTHub.AmqpTransport.1.0.11\build\native\Microsoft.Azure.IoTHub.AmqpTransport.targets')" />
<Import Project="..\packages\Microsoft.Azure.umqtt.1.0.10\build\native\Microsoft.Azure.umqtt.targets" Condition="Exists('..\packages\Microsoft.Azure.umqtt.1.0.10\build\native\Microsoft.Azure.umqtt.targets')" />
<Import Project="..\packages\Microsoft.Azure.IoTHub.MqttTransport.1.0.11\build\native\Microsoft.Azure.IoTHub.MqttTransport.targets" Condition="Exists('..\packages\Microsoft.Azure.IoTHub.MqttTransport.1.0.11\build\native\Microsoft.Azure.IoTHub.MqttTransport.targets')" />
</ImportGroup>
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\boost.1.60.0.0\build\native\boost.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\boost.1.60.0.0\build\native\boost.targets'))" />
<Error Condition="!Exists('..\packages\boost_python.1.60.0.0\build\native\boost_python.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\boost_python.1.60.0.0\build\native\boost_python.targets'))" />
<Error Condition="!Exists('..\packages\Microsoft.Azure.C.SharedUtility.1.0.10\build\native\Microsoft.Azure.C.SharedUtility.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Azure.C.SharedUtility.1.0.10\build\native\Microsoft.Azure.C.SharedUtility.targets'))" />
<Error Condition="!Exists('..\packages\Microsoft.Azure.IoTHub.IoTHubClient.1.0.10\build\native\Microsoft.Azure.IoTHub.IoTHubClient.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Azure.IoTHub.IoTHubClient.1.0.10\build\native\Microsoft.Azure.IoTHub.IoTHubClient.targets'))" />
<Error Condition="!Exists('..\packages\Microsoft.Azure.IoTHub.HttpTransport.1.0.10\build\native\Microsoft.Azure.IoTHub.HttpTransport.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Azure.IoTHub.HttpTransport.1.0.10\build\native\Microsoft.Azure.IoTHub.HttpTransport.targets'))" />
<Error Condition="!Exists('..\packages\Microsoft.Azure.uamqp.1.0.10\build\native\Microsoft.Azure.uamqp.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Azure.uamqp.1.0.10\build\native\Microsoft.Azure.uamqp.targets'))" />
<Error Condition="!Exists('..\packages\Microsoft.Azure.IoTHub.AmqpTransport.1.0.10\build\native\Microsoft.Azure.IoTHub.AmqpTransport.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Azure.IoTHub.AmqpTransport.1.0.10\build\native\Microsoft.Azure.IoTHub.AmqpTransport.targets'))" />
<Error Condition="!Exists('..\packages\Microsoft.Azure.umqtt.1.0.9\build\native\Microsoft.Azure.umqtt.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Azure.umqtt.1.0.9\build\native\Microsoft.Azure.umqtt.targets'))" />
<Error Condition="!Exists('..\packages\Microsoft.Azure.IoTHub.MqttTransport.1.0.10\build\native\Microsoft.Azure.IoTHub.MqttTransport.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Azure.IoTHub.MqttTransport.1.0.10\build\native\Microsoft.Azure.IoTHub.MqttTransport.targets'))" />
<Error Condition="!Exists('..\packages\Microsoft.Azure.C.SharedUtility.1.0.11\build\native\Microsoft.Azure.C.SharedUtility.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Azure.C.SharedUtility.1.0.11\build\native\Microsoft.Azure.C.SharedUtility.targets'))" />
<Error Condition="!Exists('..\packages\Microsoft.Azure.IoTHub.IoTHubClient.1.0.11\build\native\Microsoft.Azure.IoTHub.IoTHubClient.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Azure.IoTHub.IoTHubClient.1.0.11\build\native\Microsoft.Azure.IoTHub.IoTHubClient.targets'))" />
<Error Condition="!Exists('..\packages\Microsoft.Azure.IoTHub.HttpTransport.1.0.11\build\native\Microsoft.Azure.IoTHub.HttpTransport.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Azure.IoTHub.HttpTransport.1.0.11\build\native\Microsoft.Azure.IoTHub.HttpTransport.targets'))" />
<Error Condition="!Exists('..\packages\Microsoft.Azure.uamqp.1.0.11\build\native\Microsoft.Azure.uamqp.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Azure.uamqp.1.0.11\build\native\Microsoft.Azure.uamqp.targets'))" />
<Error Condition="!Exists('..\packages\Microsoft.Azure.IoTHub.AmqpTransport.1.0.11\build\native\Microsoft.Azure.IoTHub.AmqpTransport.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Azure.IoTHub.AmqpTransport.1.0.11\build\native\Microsoft.Azure.IoTHub.AmqpTransport.targets'))" />
<Error Condition="!Exists('..\packages\Microsoft.Azure.umqtt.1.0.10\build\native\Microsoft.Azure.umqtt.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Azure.umqtt.1.0.10\build\native\Microsoft.Azure.umqtt.targets'))" />
<Error Condition="!Exists('..\packages\Microsoft.Azure.IoTHub.MqttTransport.1.0.11\build\native\Microsoft.Azure.IoTHub.MqttTransport.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Azure.IoTHub.MqttTransport.1.0.11\build\native\Microsoft.Azure.IoTHub.MqttTransport.targets'))" />
</Target>
</Project>

0 comments on commit eae4b55

Please sign in to comment.