#62 Class for communicating with MBS
Merged 6 years ago by clime. Opened 6 years ago by frostyx.
copr/ frostyx/copr mbs-communication-class  into  master

@@ -1,6 +1,8 @@ 

  import os

  import time

  import base64

+ import json

+ import requests

  import modulemd

  from sqlalchemy import and_

  from coprs import models
@@ -116,3 +118,38 @@ 

  

      def generate(self):

          return self.mmd.dumps()

+ 

+ 

+ class MBSProxy(object):

+     def __init__(self, mbs_url, user_name=None):

+         self.url = mbs_url

+         self.user = user_name

+ 

+     def post(self, json=None, data=None, files=None):

+         request = requests.post(self.url, verify=False,

+                                 json=json, data=data, files=files)

+         return MBSResponse(request)

+ 

+     def build_module(self, owner, project, nsv, modulemd):

+         return self.post(

+             data={"owner": self.user, "copr_owner": owner, "copr_project": project},

+             files={"yaml": ("{}.yaml".format(nsv), modulemd)},

+         )

+ 

+ 

+ class MBSResponse(object):

+     def __init__(self, response):

+         self.response = response

+ 

+     @property

+     def failed(self):

+         return self.response.status_code != 201

+ 

+     @property

+     def message(self):

+         if self.response.status_code in [500, 403, 404]:

+             return "Error from MBS: {} - {}".format(self.response.status_code, self.response.reason)

+         resp = json.loads(self.response.content)

+         if self.response.status_code != 201:

+             return "Error from MBS: {}".format(resp["message"])

+         return "Created module {}-{}-{}".format(resp["name"], resp["stream"], resp["version"])

@@ -1,7 +1,8 @@ 

  import yaml

+ from munch import Munch

  from mock import patch, ANY

  from tests.coprs_test_case import CoprsTestCase

- from coprs.logic.modules_logic import ModulemdGenerator

+ from coprs.logic.modules_logic import ModulemdGenerator, MBSResponse, MBSProxy

  

  

  class TestModulemdGenerator(CoprsTestCase):
@@ -86,3 +87,37 @@ 

          generator.add_api(["foo", "bar", "baz"])

          generator.add_filter(["foo", "bar"])

          yaml.load(generator.generate())

+ 

+ 

+ class TestMBSResponse(CoprsTestCase):

+     def test_status(self):

+         assert MBSResponse(Munch(status_code=500)).failed is True

+         assert MBSResponse(Munch(status_code=409)).failed is True

+         assert MBSResponse(Munch(status_code=201)).failed is False

+ 

+     def test_message(self):

+         req1 = Munch(status_code=500, reason="foo reason")

+         res1 = MBSResponse(req1)

+         assert res1.message == "Error from MBS: 500 - foo reason"

+ 

+         req2 = Munch(status_code=409, content='{"message": "foo message"}')

+         res2 = MBSResponse(req2)

+         assert res2.message == "Error from MBS: foo message"

+ 

+         con3 = '{"name": "testmodule", "stream": "master", "version": 123}'

+         req3 = Munch(status_code=201, content=con3)

+         res3 = MBSResponse(req3)

+         assert res3.message == "Created module testmodule-master-123"

+ 

+ 

+ class TestMBSProxy(CoprsTestCase):

+ 

+     @patch("requests.post")

+     def test_post(self, post_mock):

+         url = "http://some-module-build-service.org"

+         proxy = MBSProxy(url)

+         response = proxy.post(None, None, None)

+         post_mock.assert_called()

+         args, kwargs = post_mock.call_args

+         assert args[0] == url

+         assert isinstance(response, MBSResponse)

We need to communicate with MBS on several places

  1. In API: copr-cli -> frontend API -> MBS
  2. When creating module via UI: UI -> generating a yaml -> MBS | PR#63

The common code for communicating with MBS should not be duplicated, but rather extracted to separate file and used where needed.

This PR creates a MBSProxy for sending requests to the MBS and MBSResponse for wrapping-up it's response and adding a little logic to it (it is not trivial to get a desired message text). Their usage is showed in the tests.

P.S. After reviewing and merging this PR, it should be changed API code to use this classes

Pull-Request has been merged by clime

6 years ago