0

Hello I have a RobotFramework library for requests /usr/local/lib/python2.7/dist-packages/RequestsLibrary/RequestsKeywords.py

that is missing some code to expose the Digest authentication method in the Python.Requests library. What I want to do is instead of hacking it into /usr/local/lib/python2.7/dist-packages/RequestsLibrary/RequestsKeywords.py

I would like to add if to the code directory i am in and just have it append to the class that is imported

class=RequestsKeywords

In C# etc I would use a partial class but there doesn't seem to be that kind of thing in Python any idea how to procede

here is the code i want to "APPEND" into the /usr/local/lib/python2.7/dist-packages/RequestsLibrary/RequestsKeywords.py library

def create_digest_session(self, alias, url, auth, headers={}, cookies=None, timeout=None, proxies=None, verify=False):

   """ Create Session: create a HTTP session to a server

   `url` Base url of the server

   `alias` Robot Framework alias to identify the session

   `headers` Dictionary of default headers

   `auth` List of username & password for HTTP Digest Auth

   `timeout` connection timeout

   `proxies` proxy server url

   `verify` set to True if Requests should verify the certificate
   """
   digest_auth = requests.auth.HTTPDigestAuth(*auth) if auth else None
   return self._create_session(alias, url, headers, cookie, digest_auth, timeout, proxies, verify)

any suggestions

So my test would look like this below and the python code you specified with the sys.path.append would be in the file RequestsKeywordsLocal.py correct?

*** Settings ***
Suite Teardown    Delete All Sessions
Library           Collections
Library           String
Library           /usr/local/lib/python2.7/dist-packages/RequestsLibrary/RequestsKeywords.py
Library           ./RequestsKeywordsLocal.py 
Library           ./localKeywords.py 
Library           OperatingSystem
Library           BuiltIn

*** Variables ***
${HEADER1}        Content-Type: text/xml; charset=UTF-8
${IP}             172.168.101.139
${UN}             username
${PW}             password

*** Test Cases ***
Check IP valid
    Valid Ip   ${IP}

Get Valid Hostname
    ${host}=    Valid Hostname   ${IP}
    Should Not Be Equal As Strings  ${host}  "noname"

Get With DigestAuth
    [Tags]    get
    Log    Variables
    ${auth}=    Create List    username    password
    Create Digest Session    TerraceQ    https://${IP}    auth=${auth}
    ${resp}=    Get    TerraceQ    /views
    Should Be Equal As Strings    ${resp.status_code}    200
    Should Contain  ${resp.content}  views
RobM
  • 747
  • 5
  • 12

2 Answers2

2

You can either subclass:

class MyNewXThing(XThing):
    def my_added_method(self, x, y, z):
        # ...

And use MyNewXThing instead of XThing throughout your code. Or:

def my_added_method(self, x, y, z):
    # ...
XThing.my_added_method = my_added_method

The first option is more flexible, as it does not change XThing for any other code.

matsjoyce
  • 5,744
  • 6
  • 31
  • 38
1

You could import the class, inherent and add the required function

sys.path.append('/usr/local/lib/python2.7/dist-packages/RequestsLibrary/')
import RequestsKeywords as RequestsKeywords


class RequestsKeywords_local(RequestsKeywords):

    def create_digest_session(self, alias, url, auth, headers={}, cookies=None, timeout=None, proxies=None, verify=False):

       """ Create Session: create a HTTP session to a server

       `url` Base url of the server

       `alias` Robot Framework alias to identify the session

       `headers` Dictionary of default headers

       `auth` List of username & password for HTTP Digest Auth

       `timeout` connection timeout

       `proxies` proxy server url

       `verify` set to True if Requests should verify the certificate
       """
       digest_auth = requests.auth.HTTPDigestAuth(*auth) if auth else None
       return self._create_session(alias, url, headers, cookie, digest_auth, timeout, proxies, verify)

    def __init__(self,**kwargs):

        RequestsKeywords.__init__(self,**kwargs)
Ed Smith
  • 12,716
  • 2
  • 43
  • 55
  • hey thanks guys Ill try all those see what works.. Mat I still don't quite understand the first example would look like what Ed posted right ? the second method would lo like this right ...just have the pass RequestsKeywords.create_digest_session = create_digest_session after my method right ? def create_digest_session(self, alias, url, auth, headers={}, cookies=None,timeout=None) pass RequestsKeywords.create_digest_session = create_digest_session – RobM Apr 03 '15 at 17:58
  • @matsjoyce first example is the same, it is a general form of the above example. For the second example you would use your function as you have it (not with pass) and as you said, RequestsKeywords.create_digest_session = create_digest_session. – Ed Smith Apr 03 '15 at 19:13
  • I am getting an error with the inint it looks like that I do not understand [ ERROR ] Error in file '/home/robm/code/python/robotframework/tests/requests/tests/validateDUT.txt': Importing test library '/home/robm/code/python/robotframework/tests/requests/tests/RequestsKeywordsLocal.py' failed: TypeError: Error when calling the metaclass bases module.__init__() takes at most 2 arguments (3 given) – RobM Apr 07 '15 at 21:23