예제 #1
0
def check_and_raise_error_from_linkedin_response(response):
    """Function to perform appropriate action based on the linkedin response given.

    :param response: A valid HTTP response to perform suitable action.

    :raises WrongArguments: Exception raised when either a 400 or 404 response is provided.

    :raises NotAuthenticated: Exception raised when a 401 response is provided.

    :raises PermissionDenied: Exception raised when a 403 response is provided.

    :raises NotSupported: Exception raised when a 405 response is provided.

    :raises RequestValidationError: Exception raised when an unexpected response is provided.

    """
    status_code = response.status_code
    if (status_code != status.HTTP_201_CREATED) or (status_code !=
                                                    status.HTTP_200_OK):
        json_response = response.json()
        error = f"LinkedIn error: {json_response['message']}"
        if status_code == status.HTTP_400_BAD_REQUEST:
            raise exc.WrongArguments(error)
        elif status_code == status.HTTP_401_UNAUTHORIZED:
            raise exc.NotAuthenticated(error)
        elif status_code == status.HTTP_403_FORBIDDEN:
            raise exc.PermissionDenied(error)
        elif status_code == status.HTTP_404_NOT_FOUND:
            raise exc.WrongArguments(error)
        elif status_code == status.HTTP_405_METHOD_NOT_ALLOWED:
            raise exc.NotSupported(error)
        else:
            raise exc.RequestValidationError(error)
예제 #2
0
def get_fb_page_graph():
    graph = facebook.GraphAPI(settings.FB_USER_ACCESS_TOKEN)
    pages = graph.get_object('me/accounts')['data']
    page_access_token = None
    try:
        page_list = list(
            filter(lambda page: page['id'] == settings.FB_PAGE_ID, pages))
    except KeyError:
        raise exc.WrongArguments('No ID associated with FB_USER_ACCESS_TOKEN.')
    if not page_list:
        raise exc.WrongArguments(
            'No matching ID in account data. Incorrect FB_PAGE_ID')
    page_access_token = page_list[0]['access_token']
    page_graph = facebook.GraphAPI(page_access_token)
    return page_graph
예제 #3
0
def get_and_authenticate_user(email, password):
    user = authenticate(username=email, password=password)
    if user is None:
        raise exc.WrongArguments(
            "Invalid username/password. Please try again!")

    return user
예제 #4
0
def get_fb_page_graph():
    graph = facebook.GraphAPI(settings.FB_USER_ACCESS_TOKEN)
    pages = graph.get_object('me/accounts')['data']
    page_access_token = None
    page_list = list(filter(lambda page: page['id'] == settings.FB_PAGE_ID, pages))
    if not page_list:
        raise exceptions.WrongArguments('Facebook Page access token could not be found')
    page_access_token = page_list[0]['access_token']
    page_graph = facebook.GraphAPI(page_access_token)
    return page_graph
예제 #5
0
def get_twitter_api_object(TWITTER_OAUTH):
    """Function to generate a twitter API object.

    :param TWITTER_OAUTH: A dictionary having essential twitter oauth tokens viz.
    consumer_key, consumer_secret, access_key, access_secret.

    :returns: twitter API object.

    :raises WrongArguments: Exception, when invalid twitter oauth token(s) are provided.

    """
    try:
        auth = tweepy.OAuthHandler(TWITTER_OAUTH['consumer_key'], TWITTER_OAUTH['consumer_secret'])
        auth.set_access_token(TWITTER_OAUTH['access_key'], TWITTER_OAUTH['access_secret'])
        twitter_api = tweepy.API(auth)
        return twitter_api
    except tweepy.error.TweepError as exc:
        raise exceptions.WrongArguments(str(exc))