Set the user-agent on every request. Args: http - An instance of httplib2.Http or something that acts like it. user_agent: string, the value for the user-agent header. Returns: A modified instance of http that was passed in. Example: h = httplib2
(http, user_agent)
| 1827 | |
| 1828 | |
| 1829 | def set_user_agent(http, user_agent): |
| 1830 | """Set the user-agent on every request. |
| 1831 | |
| 1832 | Args: |
| 1833 | http - An instance of httplib2.Http |
| 1834 | or something that acts like it. |
| 1835 | user_agent: string, the value for the user-agent header. |
| 1836 | |
| 1837 | Returns: |
| 1838 | A modified instance of http that was passed in. |
| 1839 | |
| 1840 | Example: |
| 1841 | |
| 1842 | h = httplib2.Http() |
| 1843 | h = set_user_agent(h, "my-app-name/6.0") |
| 1844 | |
| 1845 | Most of the time the user-agent will be set doing auth, this is for the rare |
| 1846 | cases where you are accessing an unauthenticated endpoint. |
| 1847 | """ |
| 1848 | request_orig = http.request |
| 1849 | |
| 1850 | # The closure that will replace 'httplib2.Http.request'. |
| 1851 | def new_request( |
| 1852 | uri, |
| 1853 | method="GET", |
| 1854 | body=None, |
| 1855 | headers=None, |
| 1856 | redirections=httplib2.DEFAULT_MAX_REDIRECTS, |
| 1857 | connection_type=None, |
| 1858 | ): |
| 1859 | """Modify the request headers to add the user-agent.""" |
| 1860 | if headers is None: |
| 1861 | headers = {} |
| 1862 | if "user-agent" in headers: |
| 1863 | headers["user-agent"] = user_agent + " " + headers["user-agent"] |
| 1864 | else: |
| 1865 | headers["user-agent"] = user_agent |
| 1866 | resp, content = request_orig( |
| 1867 | uri, |
| 1868 | method=method, |
| 1869 | body=body, |
| 1870 | headers=headers, |
| 1871 | redirections=redirections, |
| 1872 | connection_type=connection_type, |
| 1873 | ) |
| 1874 | return resp, content |
| 1875 | |
| 1876 | http.request = new_request |
| 1877 | return http |
| 1878 | |
| 1879 | |
| 1880 | def tunnel_patch(http): |
no outgoing calls
searching dependent graphs…