4 Replies Latest reply on Jul 3, 2019 10:38 AM by Praveen Gollu-Oracle

    Python Scripts to Invoke Oracle Cloud REST API's

    Praveen Gollu-Oracle

      Hi Experts,

       

      I am new to Python. I am trying to invoke Oracle Cloud REST API's for integrations. Any experts who can through some light the below

       

      1. Prerequisites for invoking REST API's (Library's to be Imported )

      2. Authentication prerequisites

      3. Sample scripts for invoking REST API's

       

      Any information on this are highly appreciated.

       

      Thanks,

      Praveen Gollu

        • 1. Re: Python Scripts to Invoke Oracle Cloud REST API's
          Dmytro Shurkov-Oracle

          Hello,

          requests is enough for me

          You can go thru Basic authorization(auth=(user,pass)) or use something different like oauth2 etc. by putting of token into header.

          for instance:

           

          import requests

           

          response = requests.get(url, [headers=headers], [auth=(user,pass)], [params=params])

           

          response.status_code

           

          response.json()

          • 2. Re: Python Scripts to Invoke Oracle Cloud REST API's
            Praveen Gollu-Oracle

            Hi Dmytro,

             

            Thanks for the information. As suggested, I tried below logic. Facing the issue.

            -------------------------------------------------------------------------------------------------------------------------------------------

            import requests

            url = 'http://abcd-1234.xyz.oraclecloud.com/fscmRestApi/resources/11.13.18.05/projects' -- Dummy url in the post

            user = 'abc.xyz' -- Dummy username in the post

            password = 'Welcome' -- Dummy password in the post

            auth = [user,password]

            response = requests.get(url,auth)

            if response.status_code == 200:

                print('We are able to connect to REST API')

            else:

                print('we are not able to connect to REST API')

             

            -------------------------------------------------------------------------------------------------------------------------------------------

             

            I am facing below error. Any suggestions on the same are highly appreciated.

             

            Traceback (most recent call last):

              File "D:\Praveen\Projects\Python\PythonScripts\REST_API.py", line 7, in <module>

                response = requests.get(url,auth)
              File "D:\Praveen\Projects\Python\lib\site-packages\requests\api.py", line 75, in get
                return request('get', url, params=params, **kwargs)
              File "D:\Praveen\Projects\Python\lib\site-packages\requests\api.py", line 60, in request
                return session.request(method=method, url=url, **kwargs)
              File "D:\Praveen\Projects\Python\lib\site-packages\requests\sessions.py", line 519, in request
                prep = self.prepare_request(req)
              File "D:\Praveen\Projects\Python\lib\site-packages\requests\sessions.py", line 462, in prepare_request
                hooks=merge_hooks(request.hooks, self.hooks),
              File "D:\Praveen\Projects\Python\lib\site-packages\requests\models.py", line 313, in prepare
                self.prepare_url(url, params)
              File "D:\Praveen\Projects\Python\lib\site-packages\requests\models.py", line 431, in prepare_url
                enc_params = self._encode_params(params)
              File "D:\Praveen\Projects\Python\lib\site-packages\requests\models.py", line 97, in _encode_params
                for k, vs in to_key_val_list(data):
            ValueError: too many values to unpack (expected 2)
            [Finished in 0.796s]
            • 3. Re: Python Scripts to Invoke Oracle Cloud REST API's
              Dmytro Shurkov-Oracle

              this line is incorrect:

              response = requests.get(url,auth)

               

              You need write it as:

               

              response = requests.get(url,auth=auth)

               

              url is first mandatory parameter, others  - non-mandatory (**kwargs) but you need to specify for what parameter(e.g. auth or headers) you are putting your variable (auth list).

               

              p.s. according to docs, auth should be tuple but you are using list. Just to keep things easier and avoid some issues, plz modify this part as well.

               

              If you behind the proxy, take a look here:

              https://2.python-requests.org/en/master/user/advanced/#id10

              • 4. Re: Python Scripts to Invoke Oracle Cloud REST API's
                Praveen Gollu-Oracle

                Hi Experts,

                Thanks for your support and help. I am able to successfully invoke the REST API using Python script. Hope this will be helpful for the new developers to the python.

                 

                Once again thanks for all your support.

                 

                 

                 

                import requests
                import json

                url = '<SERVER URL>/fscmRestApi/resources/11.13.18.05/projects'
                user = 'USERNAME'
                password = 'PASSWORD'
                auth = [user,password]
                ##response = requests.get(url,auth = (user,password))
                response = requests.get(url, auth = (user,password))
                if response.status_code == 200:
                    print('We are able to connect to GET REST API')
                else:
                    print('we are not able to connect to GET REST API')

                data = response.text
                parsed=json.loads(data)
                for product in parsed['items']:
                    project_name = product['ProjectName']
                    project_description = product['ProjectDescription']
                    project_id = str(product['ProjectId'])
                    project_data = {'ProjectDescription': project_description + 'RESTTEST'}
                    project_url = '<SERVERURL>/fscmRestApi/resources/11.13.18.05/projects' + '/' + project_id
                    print(project_name)
                    patch_response = requests.patch(url = project_url,auth=(user,password),json = {'ProjectDescription': project_description + 'RESTTEST'})
                    if patch_response.status_code == 200:
                        print('We are able to connect to PATCH REST API')
                    else:
                        print('we are not able to connect to PATCH REST API')