5

I have a function that makes a GET request to the JIRA REST API to pull down the JSON object of a JIRA ticket.

It uses the requests module. But I don't want to use it anymore because anytime I want someone else to run my script, they need to jump through fiery hoops to get the requests module because they are behind a corporate proxy and they don't have the time to put in the extra work.

So instead of asking people to do something they don't have the patience or time to do, I'd much rather replace the requests module with something else so that this script is more of an out-of-the-box solution.

Here's what I have now:

import requests
import pprint


def pull_jira_info(jira, user, pw, url):
    """
    Arguments:
        jira - the JIRA issue number
        url - the first part of the JIRA server url
        user - JIRA username
        pw - JIRA password
    """
    url += '/rest/api/2/issue/' + jira
    r = requests.get(url, auth=(user, pw), verify=False)
    jira_info = r.json()
    pprint.pprint(jira_info)
    return jira_info

My guess is that I can do a simple swap of the requests module with some other module (built into python) that does GET requests without having to change too much code.

Does anyone have a simple go-to for this that isn't the requests module? What would that look like when implemented in the current function I have?

Radical Edward
  • 115
  • 1
  • 11
  • 1
    You have to jump through hoops to install modules? That seems like the bigger issue that needs addressing here. (I know, you probably can't help it, but that sounds insane…) – deceze Apr 25 '18 at 14:23
  • Oh no, not me! Haha I mean, I was the one who implemented the requests module in the first place -- so I have no problem with that. It is the people who are trying to use my script. It seems these days, if it isn't an out-of-the-box and ready-to-use script, most people don't have the patience or ability to install dependencies to get it working. And with them being behind a corporate proxy, that adds another layer of work that they'd much rather not do. – Radical Edward Apr 25 '18 at 15:27

2 Answers2

3

Here's what I came up with. It works perfectly!

import base64
import json
import pprint
import urllib2


def auth_str(user, pw):
    """
    Arguments:
        user - (Required) jira username
        pw - (Required) jira password
    """
    auth = base64.b64encode('%s:%s' % (user, pw))
    return auth


def pull_jira(jira, user, pw, url):
    """
    Arguments:
        jira - (Required) jira issue number
        user - (Required) jira username
        pw - (Required) jira password
        url - (Required) jira server url
    """
    url += '/rest/api/2/issue/' + jira
    r = urllib2.Request(url)
    r.add_header('Authorization', 'Basic %s' % auth_str(user, pw))
    r.add_header('Content-Type', 'application/json')

    jira_data = json.load(urllib2.urlopen(r))
    pprint.pprint(jira_data)
    return jira_data

Yes, there are some things I could do to clean this up, but I just wanted to go ahead and post a working update showing how I replaced the requests module with urllib2.

Shadow
  • 8,749
  • 4
  • 47
  • 57
Radical Edward
  • 115
  • 1
  • 11
0

you could use the native Python urllib: What is the quickest way to HTTP GET in Python?.

but I would suggest to set up an internal Python packages repository: https://packaging.python.org/guides/hosting-your-own-index/. This will allow you to use any package you want including JIRA Python package: https://github.com/pycontribs/jira

Doron Cohen
  • 1,026
  • 8
  • 13