<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def fetch(self, method, url, data=None, expected_status_code=None):
 """Prepare the headers, encode data, call API and provide data it returns """

        kwargs = self.prepare_request(method, url, data)
        log.debug(json.dumps(kwargs))
        response = getattr(requests, method.lower())(url, **kwargs)
        log.debug(json.dumps(response.content))
        if response.status_code >= 400:
            response.raise_for_status()
        if (expected_status_code
                and response.status_code != expected_status_code):
            raise NotExpectedStatusCode(self._get_error_reason(response))
        return response