add profile
This commit is contained in:
		
							parent
							
								
									5a565a49ee
								
							
						
					
					
						commit
						1604420796
					
				
							
								
								
									
										46
									
								
								common_lib/services/profile.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								common_lib/services/profile.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,46 @@
 | 
			
		||||
import httpx
 | 
			
		||||
from uuid import UUID
 | 
			
		||||
from fastapi import HTTPException, status
 | 
			
		||||
from typing import List, Dict
 | 
			
		||||
from config import settings
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def get_profile_by_user_id(user_id: UUID, token: str) -> dict:
 | 
			
		||||
    try:
 | 
			
		||||
        async with httpx.AsyncClient(timeout=5.0, verify=False) as client:
 | 
			
		||||
            response = await client.get(
 | 
			
		||||
                f"{settings.PROFILE_SERVICE}/user_id/{user_id}",
 | 
			
		||||
                headers={"Authorization": f"Bearer {token}"}
 | 
			
		||||
            )
 | 
			
		||||
 | 
			
		||||
            if response.status_code != 200:
 | 
			
		||||
                raise HTTPException(
 | 
			
		||||
                    status_code=response.status_code,
 | 
			
		||||
                    detail=f"profile_service: {response.text}"
 | 
			
		||||
                )
 | 
			
		||||
 | 
			
		||||
            return response.json()
 | 
			
		||||
 | 
			
		||||
    except httpx.RequestError as e:
 | 
			
		||||
        raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail=f"profile_service unreachable: {str(e)}")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def get_profiles_by_user_ids(user_ids: List[UUID], token: str) -> Dict[str, dict]:
 | 
			
		||||
    try:
 | 
			
		||||
        async with httpx.AsyncClient(timeout=5.0, verify=False) as client:
 | 
			
		||||
            response = await client.post(
 | 
			
		||||
                f"{settings.PROFILE_SERVICE}/user_ids/internal",
 | 
			
		||||
                headers={"Authorization": f"Bearer {token}"},
 | 
			
		||||
                json={"user_ids": user_ids}
 | 
			
		||||
            )
 | 
			
		||||
 | 
			
		||||
            if response.status_code != 200:
 | 
			
		||||
                raise HTTPException(
 | 
			
		||||
                    status_code=response.status_code,
 | 
			
		||||
                    detail=f"profile_service: {response.text}"
 | 
			
		||||
                )
 | 
			
		||||
 | 
			
		||||
            return response.json()
 | 
			
		||||
 | 
			
		||||
    except httpx.RequestError as e:
 | 
			
		||||
        raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail=f"profile_service unreachable: {str(e)}")
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user