Skip to content

Commit 42f6fc1

Browse files
cp2bostonLi Xu
authored andcommitted
RCB-530 Proxy (#72)
* RCB-530 Proxy Fix - Modified the requests code to include proxies if they are set in environment - Created a simple docker-compose file that can be used to test against a proxy * Minor comment * PEP8 fix on comment * Updated multi-part with proxy change * Tox related changes - Running py2 and py3 exposed a bug in sentiment.py, which is fixed - api.py updated - tox.ini added to tests directory * Cleaned up proxy code * Additional proxy cleanup * Final linefeed on docker-compose * Updated examples Dockerfile * Missed a module in Dockerfile * Mod to Jenkinsfile - force pull of latest docker image * Removed kitchen requirement * Fixed pep8 miscue * Fixed unclosed connection warning - In python3. _make_requests was using a local session, not the class level one
1 parent fede73f commit 42f6fc1

File tree

6 files changed

+65
-15
lines changed

6 files changed

+65
-15
lines changed

Jenkinsfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ node {
99
}
1010
stage("Test with Docker") {
1111
withEnv(["API_KEY=${env.ROSETTE_API_KEY}", "ALT_URL=${env.BINDING_TEST_URL}"]) {
12+
sh "docker pull rosetteapi/docker-python"
1213
sh "docker run --rm -e API_KEY=${API_KEY} -e ALT_URL=${ALT_URL} -v ${SOURCEDIR}:/source rosetteapi/docker-python"
1314
}
1415
}

docker-compose.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
application:
2+
image: rosetteapi/docker-python
3+
environment:
4+
- API_KEY=$API_KEY
5+
- HTTP_PROXY=http://squid:3128
6+
- HTTPS_PROXY=https://squid:3128
7+
volumes:
8+
- .:/source
9+
links:
10+
- proxy:squid
11+
12+
proxy:
13+
image: datadog/squid
14+
ports:
15+
- 3128:3128

examples/docker/Dockerfile

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,28 @@
1-
FROM python:2.7.11
2-
MAINTAINER Fiona Hasanaj
3-
ENV MAINTENANCE_DATE 03.28.2016
1+
FROM python
2+
MAINTAINER Chris Park
3+
LABEL SOURCE="https://github.com/rosette-api/python/blob/develop/examples/docker/Dockerfile"
4+
LABEL VERSION="1.7.1"
5+
ENV LANGUAGE=python
46

5-
# install necessary software
6-
RUN apt-get -y update && apt-get install -y vim && apt-get install -y git && pip install rosette_api
7+
ENV LANG en_US.UTF-8
8+
9+
RUN apt-get update && \
10+
apt-get -y install \
11+
wget \
12+
curl \
13+
libssl-dev \
14+
libffi-dev \
15+
git \
16+
vim && \
17+
apt-get clean && \
18+
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
19+
20+
# required for pip to run as non-root
21+
RUN mkdir -p /.cache && chmod 777 /.cache
22+
RUN mkdir -p /.local && chmod 777 /.local
23+
24+
RUN pip install --upgrade tox
25+
RUN pip install --upgrade autopep8 requests rosette_api
726

827
COPY run_python.sh /python/examples/run_python.sh
928
RUN chmod 755 /python/examples/run_python.sh

examples/sentiment.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env python
12
# -*- coding: utf-8 -*-
23
"""
34
Example code to call Rosette API to get the sentiment of a local file.
@@ -18,7 +19,7 @@ def run(key, alt_url='https://api.rosette.com/rest/v1/'):
1819
temp_file = tempfile.NamedTemporaryFile(suffix=".html")
1920
sentiment_file_data = "<html><head><title>New Ghostbusters Film</title></head><body><p>Original Ghostbuster Dan Aykroyd, who also co-wrote the 1984 Ghostbusters film, couldn’t be more pleased with the new all-female Ghostbusters cast, telling The Hollywood Reporter, “The Aykroyd family is delighted by this inheritance of the Ghostbusters torch by these most magnificent women in comedy.”</p></body></html>"
2021
message = sentiment_file_data
21-
temp_file.write(message)
22+
temp_file.write(message if isinstance(message, bytes) else message.encode())
2223
temp_file.seek(0)
2324

2425
# Create an API instance

rosette/api.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -524,12 +524,12 @@ def call(self, parameters):
524524
'application/json')}
525525
request = requests.Request(
526526
'POST', url, files=files, headers=headers, params=payload)
527-
session = requests.Session()
528-
prepared_request = session.prepare_request(request)
529-
resp = session.send(prepared_request)
530-
rdata = resp.content
531-
response_headers = {"responseHeaders": dict(resp.headers)}
532-
status = resp.status_code
527+
prepared_request = self.api.session.prepare_request(request)
528+
settings = self.api.session.merge_environment_settings(prepared_request.url, {}, {}, {}, {})
529+
response = self.api.session.send(prepared_request, **settings)
530+
rdata = response.content
531+
response_headers = {"responseHeaders": dict(response.headers)}
532+
status = response.status_code
533533
response = _ReturnObject(
534534
_my_loads(rdata, response_headers), status)
535535
else:
@@ -644,11 +644,12 @@ def _make_request(self, operation, url, data, headers):
644644

645645
request = requests.Request(
646646
operation, url, data=data, headers=headers, params=payload)
647-
session = requests.Session()
648-
prepared_request = session.prepare_request(request)
647+
prepared_request = self.session.prepare_request(request)
648+
# Take into account environment settings, e.g. HTTP_PROXY and HTTPS_PROXY
649+
settings = self.session.merge_environment_settings(prepared_request.url, {}, {}, {}, {})
649650

650651
try:
651-
response = session.send(prepared_request)
652+
response = self.session.send(prepared_request, **settings)
652653
status = response.status_code
653654
rdata = response.content
654655
dict_headers = dict(response.headers)

tests/tox.ini

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[tox]
2+
skipsdist = True
3+
envlist = py2, py3
4+
5+
[testenv]
6+
commands =
7+
pytest -s --pep8
8+
deps =
9+
pytest
10+
pytest-pep8
11+
httpretty
12+
epydoc
13+
requests

0 commit comments

Comments
 (0)