Grunt task for uploading a file to CouchDB using request - coffeescript

In my Gruntfile.coffee, I have set up a task:
grunt.registerTask 'couch_upload', 'CouchDB upload', ->
done = #async()
assets = ['combined.min.js', 'screen.min.css']
ddoc = 'http://localhost:5984/mydb/_design/app'
request = require 'request'
fs = require 'fs'
grunt.log.writeln 'Uploading assets...'
upload = (asset) ->
# get the revision of the design document
request.get {json: yes, url: ddoc}, (err, resp, body) ->
rev = body._rev
# stream the file and pipe it, so it has the correct Content-Type
fs.createReadStream('build/' + asset).pipe request.put
auth: user: 'myusername', password: 'mypass'
headers: 'If-Match': rev
url: [ddoc, asset].join '/'
, -> grunt.log.write ' ' + asset
upload asset for asset in assets
done()
but I cannot see anything uploaded, neither on Grunt's log, nor Couch's log.
Can you tell if I'm doing something wrong?

Sounds like you need to open a feature request to CouchDB-Lucene: https://github.com/rnewson/couchdb-lucene/issues — it doesn’t look like it has that feature enabled yet, if it is at all possible.

Related

Item does not have a file (error 500) when uploading a PNG file to a portal using add item method

I am trying to setup a POST request method using the "Add Item" operation within a REST API for my portal so that the PNG images can be add and later updated. Currently my script uploads the item to the portal but when i try to download the file or share it then it opens a page saying "Item does not have a file . Error 500" I assume its something to do with how i am building the POST request. How should i send the file over the POST request so that i can later download and update the file . Here is my current code:
def add_item(username,files,type,title):
"""
Add an item to the portal
Input:
- file: path of the the file to be uploaded
- username: username of user uploads the item
Return:
- Flag with a list of messages
"""
# set request header authorization type
header = {
'Authorization': f'Bearer {token}'
}
# build url
u_rl = f"{portal}/sharing/rest/content/users/{username}/addItem"
# search for id
req = requests.post(url=u_rl, headers=header,files=files,data={ "type":type,"title":title,"f": "json"})
response = json.loads(req.text)
if 'error' in response.keys():
error = response["error"]
raise Exception(
f"Error message: {error['message']}", f"More details: {','.join(error['details'])}")
return response["success"] if 'success' in response.keys() else False
if __name__ == "__main__":
user = input("input your USERNAME: ")
password = getpass.getpass("input your PASSWORD: ")
portal = "https://cvc.portal"
token = generate_token(user, password, portal=portal)
files = {'upload_file': open(r'C:\Users\test.png','rb')}
type='Image',
title='An image'
add_item(user,files,type,title

Deployment Azur function from Business Central

I try to deploy the Azure function by using Rest API and zip-archive of solution.
It works properly in Postman.
I've found advice on how to upload mp3 files and develop a solution for my task.
But when I try to create a payload for request by AL-code for Business Central (file have been uploaded to instr):
CR := 13;
LF := 10;
NewLine += '' + CR + LF;
httpHeader.Clear();
TempBlob.CreateOutStream(PayloadOutStream);
PayloadOutStream.WriteText('--boundary' + NewLine);
PayloadOutStream.WriteText(StrSubstNo('Content-Disposition: form-data; name="file"; filename="%1"', filename) + NewLine);
PayloadOutStream.WriteText('Content-Type: application/zip' + NewLine);
PayloadOutStream.WriteText(NewLine);
CopyStream(PayloadOutStream, InStr);
PayloadOutStream.WriteText(NewLine);
PayloadOutStream.WriteText('--boundary');
PayloadOutStream.WriteText(NewLine);
TempBlob.CreateInStream(PayloadInStream);
Content.WriteFrom(PayloadInStream);
Content.GetHeaders(httpHeader);
if httpHeader.Contains('Content-Type') then httpHeader.Remove('Content-Type');
httpHeader.Add('Content-Type', 'multipart/form-data;boundary=boundary');
httpRequest := CreateHttpRequestMessage(Content, 'Post', RequestURI);
Client.Clear();
Client.DefaultRequestHeaders.Add('Authorization', StrSubstNo('Bearer %1', token));
if Client.Send(httpRequest, httpResponse) then begin
httpResponse.Content().ReadAs(responseText);
Message(responseText);
end
else
Error(RequestErrorMsg);
I received an error in the response message from the deployment process like this:
{"Message":"An error has occurred.","ExceptionMessage":"Number of entries expected in End Of Central Directory does not correspond to number of entries in Central Directory.","ExceptionType":"System.IO.InvalidDataException","StackTrace":" at System.IO.Compression.ZipArchive.ReadCentralDirectory()\r\n at System.IO.Compression.ZipArchive.get_Entries()\r\n at Kudu.Core.Infrastructure.ZipArchiveExtensions.Extract(ZipArchive archive, String directoryName, ITracer tracer, Boolean doNotPreserveFileTime) in C:\\Kudu Files\\Private\\src\\master\\Kudu.Core\\Infrastructure\\ZipArchiveExtensions.cs:line 114\r\n at Kudu.Services.Deployment.PushDeploymentController.<>c__DisplayClass21_0.<LocalZipFetch>b__1() in C:\\Kudu Files\\Private\\src\\master\\Kudu.Services\\Deployment\\PushDeploymentController.cs:line 746\r\n at System.Threading.Tasks.Task.InnerInvoke()\r\n at System.Threading.Tasks.Task.Execute()......
I believe, something is wrong when I build the payload. Could you give me advice on how I have to build the body of request for my case?

How to in "def request()" return the response directly

like title, I wanna in "def request()" to process data, and return a response directly;
I don't wanna flow through the target server;
this way is feasible? thanks!!!
Here's an example on how to do that:
"""Send a reply from the proxy without sending any data to the remote server."""
from mitmproxy import http
def request(flow: http.HTTPFlow) -> None:
if flow.request.pretty_url == "http://example.com/path":
flow.response = http.Response.make(
200, # (optional) status code
b"Hello World", # (optional) content
{"Content-Type": "text/html"} # (optional) headers
)
Source: https://github.com/mitmproxy/mitmproxy/blob/main/examples/addons/http-reply-from-proxy.py

Watson Discovery Service Python add document Error: Invalid Content-Type. Expected 'multipart/form-data'

Import Watson Developer Cloud Python SDK
from watson_developer_cloud import DiscoveryV1
Get the pdf from Slack doc_url which is the private URL
r = requests.get(doc_url, headers={'Authorization': 'Bearer {}'.format(slack_token) })
logging.debug("read_pdf headers %s " %r.headers )
logging.debug("read_pdf content-type %s " %r.headers['content-type'] )
Save the file in the cloud file system temporarily
with open(doc_name, 'wb' ) as f:
f.write(r.content)
filepath = os.path.join(os.getcwd(), '.', doc_name )
logging.debug('filepath %s' %filepath)
logging.debug('filepath assertion %s' %os.path.isfile(filepath) )
Create a Discovery instance
discovery = DiscoveryV1(
username=DS_USERNAME,
password=DS_PASSWORD,
version="2017-10-16"
)
Add pdf document in Discovery instance
with open(filepath, 'rb') as fileinfo:
add_doc = discovery.add_document(ENVIRONMENT_ID, COLLECTION_ID, file_content_type=r.headers['content-type'])
Log files
read_pdf headers {'Content-Type': 'application/pdf', 'Content-Length': '149814'
WatsonApiException: Error: Invalid Content-Type. Expected 'multipart/form-data', got 'application/octet-stream', Code: 400 , X-dp-watson-tran-id: gateway02-732476861 , X-global-transaction-id: ffea405d5ba1ad632ba8b5bd
Developer code examples are commented out in Github.
https://github.com/watson-developer-cloud/python-sdk/blob/master/examples/discovery_v1.py
Oh, my. That is a miserable error message.
What is missing from the call to discovery.add_document() is the file parameter. Can you try adding file=fileinfo like this:
with open(filepath, 'rb') as fileinfo:
add_doc = discovery.add_document(ENVIRONMENT_ID,
COLLECTION_ID,
file=fileinfo,
file_content_type=r.headers['content-type'])
For reference, here is some Python code that works and is doing something very similar to what it looks like you are aiming for.

What's going wrong when I try to create a review comment through Github's v3 API?

I'm trying to create a review commit through Github's v3 API and am not succeeding. Consider this repository. There's a single pull request and for the purposes of this question let's say I want to leave a 'changes requested' review on that PR. Here's the code I've got:
#!/usr/bin/env python3
import requests
import json
TOKEN='YOUR_TOKEN_HERE'
REPO = "blt/experiment-repo"
PR_NUM = 1
COMMIT_SHA_1 = "4160bee478c3c985eaaa35f161cc922fe20b354a"
COMMIT_SHA_2 = "df9d13a2e35f9b6c228e1f30ea30585ed85af26a"
def main():
pr_comment_headers = {
'user-agent': 'benedikt/0.0.1',
'Authorization': 'token %s' % TOKEN,
# Accept header per
# https://developer.github.com/changes/2016-12-16-review-requests-api/
'Accept': 'application/vnd.github.black-cat-preview+json',
}
msg = "BLEEP BLOOP I AM A ROBOT"
payload = { 'commit_id': COMMIT_SHA_2,
'body': msg,
'event': "REQUEST_CHANGES" }
# Per https://developer.github.com/v3/pulls/reviews/#create-a-pull-request-review
review_url = "https://api.github.com/repos/%s/pulls/%s/reviews" % (REPO, PR_NUM)
res = requests.post(review_url, headers = pr_comment_headers,
json = json.dumps(payload))
print(res)
print(res.text)
if __name__ == '__main__':
main()
I've marked in code comments where I've discovered the API endpoints to hit and with what payloads. Excepting, I must have goofed somewhere because when I run the above program I receive:
<Response [422]>
{"message":"Validation Failed","errors":["Variable commitOID of type GitObjectID was provided invalid value","Variable event of type PullRequestReviewEvent was provided invalid value"],"documentation_url":"https://developer.github.com/v3/pulls/reviews/#create-a-pull-request-review"}
I've verified that the commit SHAs are the exact ones that Github shows and REQUEST_CHANGES is the string in the documentation.
What am I missing?
I think you need to let requests encode the request body instead of encoding it yourself with json.dumps(), something like this: requests.post(..., json=payload)