"Bad Request" when creating an SQLServerLinkedService - python

I am trying to create an SQL Server Linked Service for Azure Data Factory from within Python. All the dependencies are imported and the data factory gets created successfully. It is failing on creating the Linked Service.
Here is my pseudo-code running from Python:
ls_name = 'Blah'
properties = SqlServerLinkedService(
connection_string="blahblah.database.windows.net",
connect_via={"referenceName": "AutoResolveIntegrationRuntime", "type": "IntegrationRuntimeReference"},
user_name="my-user-name",
password=SecureString("my-plaintext-password")
)
ls = adf_client.linked_services.create_or_update( resource_group_name=rg_name, factory_name=df_name, linked_service_name=ls_name, linked_service=properties)
For one, if I keep the format of the SecureString the way it is above, I get error about 2 positional arguments where only 1 is required, if i take out the password line altogether, I get "Bad Request" when create_or_update function runs at the bottom. They Microsoft Python SDK documentation is terribly lacking. Can someone help me figure out how to code this correctly?
File "/usr/local/lib/python3.8/dist-packages/azure/mgmt/datafactory/operations/_linked_services_operations.py", line 190, in create_or_update
raise HttpResponseError(response=response, error_format=ARMErrorFormat)
azure.core.exceptions.HttpResponseError: Operation returned an invalid status 'Bad Request'

We experienced the same issue when creating Azure Data Factory linked services; when creating a linked service you need to create a LinkedServiceResource:
ls_name = 'Blah'
properties = LinkedServiceResource(properties=SqlServerLinkedService(
connection_string="blahblah.database.windows.net",
connect_via={"referenceName": "AutoResolveIntegrationRuntime", "type": "IntegrationRuntimeReference"},
user_name="my-user-name",
password=SecureString("my-plaintext-password")
))
ls = adf_client.linked_services.create_or_update( resource_group_name=rg_name, factory_name=df_name, linked_service_name=ls_name, linked_service=properties)

Related

How to pass request parameter to httpintegration for Api Gateway using CDK python

I have an issues while passing
request_parameters = {"method.request.header.User":"context.authorizer.User"}
Into HttpIntegation:
httpintegration = _apigw.HttpIntegration(url_auth + '/logout', proxy=True, options=request_parameters({"integration.request.header.User":"$context.authorizer.User"}))
I got :
TypeError: 'dict' object is not callable Subprocess exited with error
1 Using: cdk python.
Please refer to the Python example below. I suppose the IntegrationOptions class needs to be used.
CDK and its documentation are not reliable and there are practically no usable examples with so many defects. I am afraid CDK is not the level for general use.
cdk_api_method: aws_apigateway.Method = cdk_api_resource.add_method(
http_method=str.upper(verb),
integration=aws_apigateway.HttpIntegration(
url=url,
http_method=str.upper(verb),
proxy=True,
options=aws_apigateway.IntegrationOptions(
request_parameters=method_parameter_set['integration_request_parameters']
if 'integration_request_parameters' in method_parameter_set else None
)
)
)
"integration_request_parameters": {
"integration.request.header.{}".format(HTTP_HEADER_X_VALIDATION_CLIENT_NAME): "'{}'".format(HTTP_HEADER_X_VALIDATION_CLIENT_VALUE)
},

Mocking a LDAP Server in Python (ldap3)

I am currently trying to mock the LDAP server we are using in the company. For connections to it in our web app we use python ldap3, so I decided to use the mocking feature of ldap3 (which is documented here: https://ldap3.readthedocs.io/mocking.html). However, it can't seem to work for some reason and the resources online on this topic are scarce.
This is my current code:
//imports and environment variables for the LDAP servers
...
_LDAP_SERVER = Server(host=LDAP.host, port=int(LDAP.port), get_info='ALL')
server = _LDAP_SERVER
#connection to real server
_CONNECTION = Connection(
server,
LDAP.manager_dn, LDAP.manager_password,
auto_bind=True, client_strategy=RESTARTABLE
)
#extracting the json files
server.info.to_file('my_real_server_info.json')
server.schema.to_file('my_real_server_schema.json')
#getting the real server entries - everything works to this point
if _CONNECTION.search(LDAP.root_dn, _ALL_USERS_SEARCH_FILTER, attributes=_SEARCH_ATTRIBUTES):
_CONNECTION.response_to_file('my_real_server_entries.json', raw=True)
_CONNECTION.unbind()
#creating the mock server per guidelines
mock_server = Server.from_definition('Mock Server', 'my_real_server_info.json', 'my_real_server_schema.json')
#making a new fake connection
fake_connection = Connection(mock_server, user='CN=testuser, CN=users, DC=company, DC=com', password='fakepassword',
client_strategy=MOCK_SYNC)
fake_connection.strategy.add_entry('CN=selen001,CN=users, DC=company,DC=com', {
"cn": "selen001", #username
"displayName": "Admin, selenium",
"mail": "selenium#COMPANY.COM",
}
)
fake_connection.strategy.add_entry('CN=selen002,CN=users,DC=company,DC=int', {
"cn": "selen002", #username
"displayName": "User, selenium",
"mail": "selenium2#COMPANY.COM",
}
)
fake_connection.bind()
#I want to test if it works, but I can't get any results
if fake_connection.search('DC=company,DC=com', _ALL_USERS_SEARCH_FILTER, attributes=_SEARCH_ATTRIBUTES):
fake_connection.response_to_file('my_real_server_entries1337.json', raw=True)
So to summarise: (1) Connection to Real Server, (2) get its schema and info, (3) generate its entities, (4) create a mock server and a fake connection with fake user, (5) add fake users, (6) test if it works (I can't get a result out of this, which leads me to think that there is an error somewhere, even though I followed the code closely..).
Thank you.

IBM Phoneme Detection in Python

I'm trying to use the watson developer cloud python library to interface with the IBM Speech to Text API in an effort to detect which phonemes or syllables exist in some text. But I'm running into some issues relating to a required parameter called customization_id, and I'm hoping someone might be able to provide more context on what value to pass in. I wasn't able to understand after reading the docs. Here is a code snippet:
from watson_developer_cloud import TextToSpeechV1, WatsonApiException
API_KEY = "<redacted>"
URL = "https://gateway-wdc.watsonplatform.net/text-to-speech/api"
client = TextToSpeechV1(iam_apikey=API_KEY, url=URL)
try:
#response = client.get_word(customization_id="1", word="HELLO WORLD")
#> Malformed GUID: '1'
#response = client.get_word(word="HELLO WORLD")
#> get_word() missing 1 required positional argument: 'customization_id'
#response = client.get_word(customization_id=None, word="HELLO WORLD")
#> ValueError: customization_id must be provided
#response = client.get_word(customization_id="GA", word="HELLO WORLD")
#> ERROR 400: Malformed GUID: 'GA'
# WHAT VALUE TO USE FOR CUSTOMIZATION_ID ??? ...
response = client.get_word(customization_id="_______", word="HELLO WORLD") #>
print("RESPONSE")
print(type(response))
except WatsonApiException as ex:
print(f"ERROR {str(ex.code)}: {ex.message}")
EDIT: It is possible the expected value is the identifier of a new custom voice model. I've started investigating that strategy here, but unfortunately I'm also running into issues on that one as well. The approach might be something like:
# ...
voice_model_response = client.create_voice_model(
name="My Custom Model",
language=LANG,
description="to get a valid 'customization_id' value..."
).get_result()
customization_id = voice_model_response["customization_id"]
response = client.get_word(customization_id=customization_id, word="HELLO WORLD")
# ...
I think you have misread the documentation for the Speech to Text service.
You can create a customisation to modify how a corpus uses pronunciation to detect words
https://cloud.ibm.com/apidocs/speech-to-text?code=python#add-custom-words
But to do that you need to create a customisation, which you can’t do with a lite account.
https://cloud.ibm.com/apidocs/speech-to-text?code=python#create-a-custom-language-model
You can use the API to list which customisations you have already created.
https://cloud.ibm.com/apidocs/speech-to-text?code=python#list-custom-language-models
It turns out I was using the wrong URL and wrong API Key. After fixing the URL to really be "https://gateway-wdc.watsonplatform.net/text-to-speech/api" and creating and upgrading a new standard-level text to speech service and using that service's API Key, I was able to implement the two-step process I mentioned in the updated part of my question.

geopy openmapquest for Python throws GeocoderInsufficientPrivileges error

I am running the following:
import geopy
geolocator = geopy.geocoders.OpenMapQuest(api_key='my_key_here')
location1 = geolocator.geocode('Madrid')
where my_key_here is my consumer key for mapquest, and I get the following error:
GeocoderInsufficientPrivileges: HTTP Error 403: Forbidden
Not sure what I am doing wrong.
Thanks!
I've also tried the same with the same result. After checking the Library, I found out, that the error is referring to the line, where the request ist build and it seems, that the API Key is not transmitted. If you add no key in the init statement, the api_key='' so I tried to change the line 66 in my own Library of the file: https://github.com/geopy/geopy/blob/master/geopy/geocoders/openmapquest.py to my key.
Still no success! The key itself works, I've tested it with calling the URL that is also called in the Library:
http://open.mapquestapi.com/nominatim/v1/search.php?key="MY_KEY"&format=json&json_callback=renderBasicSearchNarrative&q=westminster+abbey
no idea why this isn't working…
Cheers.kg
I made slight progress with fixing this one. I was able to get the query written correctly, but its the json parsing that kind of have me stumped. Maybe someone knows. I know the url is being sent correctly (I checked it in the browser and it returned a json object). Maybe someone knows how to parse the returned json object to get it to finally work.
Anyways, I had to go in the openmapquest.py source code, and starting from line 66, I made the following modifications:
self.api_key = api_key
self.api = "http://www.mapquestapi.com/geocoding/v1/address?"
def geocode(self, query, exactly_one=True, timeout=None): # pylint: disable=W0221
"""
Geocode a location query.
:param string query: The address or query you wish to geocode.
:param bool exactly_one: Return one result or a list of results, if
available.
:param int timeout: Time, in seconds, to wait for the geocoding service
to respond before raising a :class:`geopy.exc.GeocoderTimedOut`
exception. Set this only if you wish to override, on this call
only, the value set during the geocoder's initialization.
.. versionadded:: 0.97
"""
params = {
'key': self.api_key,
'location': self.format_string % query
}
if exactly_one:
params['maxResults'] = 1
url = "&".join((self.api, urlencode(params)))
print url # Print the URL just to make sure it's produced correctly
Now the task remains to get the _parse_json function working.

Published function on picloud throws an error

I am trying to send a file to a function on picloud via REST with poster python library on google app engine (An HTML JPEG image upload). But the function throws this error :
{"error": {"msg": "Function arguments (POST data) are not valid JSON", "code": 446, "data": "{'parameter': u' filename'}", "retry": false}}
I have set the function's output encoding to raw, also I've followed the docs to detail.
Here are some of the function details.
Function name: detector(name,ifile)
takes two arguments,an image file and its name
Here is a relevant part of the code:
#all needed classes and libraries have been imported,urlfetch, poster,MultipartParam class ect.
#here we go!
params=[]
params.append(MultipartParam("Imagename",filename="anyname.jpg",filetype="application/octet-stream",value=some_file_uploaded_via_html_form))
#http_headers has been defined with appropriate autorization credentials
datagen,headers=multipart.encode(params)
data=str().join(datagen)
result=urlfetch.fetch(url=my_defined_function_url,payload=data,method=urlfetch.POST,headers=http_headers)
print result.content
When I add the following lines,in order to include the real function arguments
params.append(MultipartParam('name',value=filename_variable)
params.append(MultipartParam('ifile',value=some_file_uploaded_via_html_form)
I get the error
{"error": {"msg": " charset is defined multiple times", "code": 445, "retry": false}}
I have also tried wrapping the parameters in a dictionary, rather than separate MultipartParam instances
Please help.
You got a space in your argument:
params.append(MultipartParam('ifile',value=some_file_uploaded_via_html _form)
This should likely be:
params.append(MultipartParam('ifile',value=some_file_uploaded_via_html_form)
Note that some_file_uploaded_via_html_form is one word.

Categories

Resources