Smartling / api-sdk-python

SDK for integrating with the Smartling API. The Smartling API allows developers to upload language specific resource files and download the translations of those files for easy integration within their application.
http://docs.smartling.com
14 stars 17 forks source link

File API v2: Would like to be able to easily set custom File URI #14

Closed tcg closed 7 years ago

tcg commented 8 years ago

Perhaps I'm missing something?

Using the examples provided, it appears that the upload method is always using the absolute file path (from on local disk) for the File URI. That may be fine for most cases...?

I had the need to set it explicitly, as not all code dealing with the same file (to upload/overwrite it) is on the same machine.

It seems that an easy way to override that value is missing from upload. So, finding that it passes kwargs to the commandUpload method, I changed it to something like this (mostly the if file_uri bit) :

    def commandUpload(self, filePath, fileType, directives={}, **kw):
        """ http://docs.smartling.com/pages/API/v2/FileAPI/Upload-File/ """
        params = {
            Params.FILE_URI: filePath,
            Params.FILE_TYPE: fileType,
            Params.FILE_PATH: filePath
        }
        if 'file_uri' in kw:
            # Set specific file_uri for upload, if provided:
            params[Params.FILE_URI] = kw['file_uri']
            # Remove this kwarg before passing along,
            # because it's invalid later:
            kw.pop('file_uri')

        for k, v in kw.items():
            params[k] = v

        self.processDirectives(params, directives)

        url = self.urlHelper.getUrl(self.urlHelper.UPLOAD)
        return self.uploadMultipart(url, params)

I was hesitant to file a PR, in case you had other specific plans for implementing this, and because I didn't write tests/validators for the behavior, nor did I add documentation for the kwarg param.

Hashtag "worksforme" (shrug).

anatolija commented 7 years ago

No code change is necessary to set custom file uri add key argument fileUri: fapi.upload(path, FILE_TYPE, fileUri=customFileUri, authorize=True) will add that to SimpleExample.py