

from swh.web.ui.back import http, api_query
from swh.core.json import SWHJSONDecoder

import json


def search(base_url, hashes):
    """Search a content with given hashes.

    Args:
         hashes, dictionary of hash indexed by key, sha1, sha256, etc...

    Returns:
         None if no content is found.
         An enriched content if the content is found.

    Raises:
         OSError (no route to host), etc... Network issues in general
    """
    def unserialize_result(res):
        if res.ok:
            output = res.content.decode('utf-8')
            if output:
                h_res = json.loads(output, cls=SWHJSONDecoder)
                if h_res:
                    return h_res['found']
                return None
            return False
        return False

    q = api_query.api_storage_content_present({'content': hashes})
    return http.execute(base_url, q, result_fn=unserialize_result)



from swh.web.ui.back import http, api_query
from swh.core.json import SWHJSONDecoder

import json


def search(base_url, hashes):
    """Search a content with given hashes.

    Args:
         hashes, dictionary of hash indexed by key, sha1, sha256, etc...

    Returns:
         None if no content is found.
         An enriched content if the content is found.

    Raises:
         OSError (no route to host), etc... Network issues in general
    """
    def unserialize_result(res):
        if res.ok:
            output = res.content.decode('utf-8')
            return json.loads(output, cls=SWHJSONDecoder) if output else False
        return False

    q = api_query.api_storage_content_present({'content': hashes})
    return http.execute(base_url, q, result_fn=unserialize_result)

Refactor - Simplify the call to api
Refactor - Simplify the call to api

  
  
  from swh.web.ui.back import http, api_query
  from swh.core.json import SWHJSONDecoder
  
  import json
  
  
  def search(base_url, hashes):
      """Search a content with given hashes.
  
      Args:
           hashes, dictionary of hash indexed by key, sha1, sha256, etc...
  
      Returns:
           None if no content is found.
           An enriched content if the content is found.
  
      Raises:
           OSError (no route to host), etc... Network issues in general
      """
      def unserialize_result(res):
          if res.ok:
              output = res.content.decode('utf-8')
-             if output:
-                 h_res = json.loads(output, cls=SWHJSONDecoder)
+             return json.loads(output, cls=SWHJSONDecoder) if output else False
-                 if h_res:
-                     return h_res['found']
-                 return None
-             return False
          return False
  
      q = api_query.api_storage_content_present({'content': hashes})
      return http.execute(base_url, q, result_fn=unserialize_result)
  
Refactor - Simplify the call to api
## Code Before:


from swh.web.ui.back import http, api_query
from swh.core.json import SWHJSONDecoder

import json


def search(base_url, hashes):
    """Search a content with given hashes.

    Args:
         hashes, dictionary of hash indexed by key, sha1, sha256, etc...

    Returns:
         None if no content is found.
         An enriched content if the content is found.

    Raises:
         OSError (no route to host), etc... Network issues in general
    """
    def unserialize_result(res):
        if res.ok:
            output = res.content.decode('utf-8')
            if output:
                h_res = json.loads(output, cls=SWHJSONDecoder)
                if h_res:
                    return h_res['found']
                return None
            return False
        return False

    q = api_query.api_storage_content_present({'content': hashes})
    return http.execute(base_url, q, result_fn=unserialize_result)

## Instruction:
Refactor - Simplify the call to api
## Code After:


from swh.web.ui.back import http, api_query
from swh.core.json import SWHJSONDecoder

import json


def search(base_url, hashes):
    """Search a content with given hashes.

    Args:
         hashes, dictionary of hash indexed by key, sha1, sha256, etc...

    Returns:
         None if no content is found.
         An enriched content if the content is found.

    Raises:
         OSError (no route to host), etc... Network issues in general
    """
    def unserialize_result(res):
        if res.ok:
            output = res.content.decode('utf-8')
            return json.loads(output, cls=SWHJSONDecoder) if output else False
        return False

    q = api_query.api_storage_content_present({'content': hashes})
    return http.execute(base_url, q, result_fn=unserialize_result)
