Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use center V2 for assets #243

Merged
merged 11 commits into from
Sep 18, 2023
36 changes: 12 additions & 24 deletions .github/workflows/check-update-widget-index.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
name: Check and update widget index

on:
workflow_run:
workflows: [Check widget translation]
types: [completed]
push:
branches: [master, dev]

jobs:
main:
name: Check and update widget index
runs-on: ${{ matrix.os }}
if: ${{ github.event.workflow_run.conclusion == 'success' }}
timeout-minutes: 15
strategy:
fail-fast: false
Expand All @@ -27,6 +24,13 @@ jobs:
with:
path: backend

- name: Extract branch name
run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
id: extract_branch

- name: Show branch name
run: echo "Branch name is ${{ steps.extract_branch.outputs.branch }}"

- id: 'auth'
uses: 'google-github-actions/auth@v1'
with:
Expand Down Expand Up @@ -58,25 +62,9 @@ jobs:
echo "SERVER_HOST=" >> "$GITHUB_ENV"
echo "SERVER_ORIGINS=" >> "$GITHUB_ENV"
echo "SERVER_SECRET_KEY=" >> "$GITHUB_ENV"

if [[ ${GITHUB_BASE_REF:-${GITHUB_REF#refs/heads/}} == 'master' ]]; then
echo "ENV_TAG=prod" >> "$GITHUB_ENV"
else
echo "ENV_TAG=dev" >> "$GITHUB_ENV"
echo "WEAVIATE_URL=${{ secrets.DEV_WEAVIATE_URL }}" >> "$GITHUB_ENV"
echo "WEAVIATE_API_KEY=" >> "$GITHUB_ENV"
echo "CHATDB_URL=${{ secrets.DEV_CHATDB_URL }}" >> "$GITHUB_ENV"
fi

- name: Set environment for branch
run: |
echo "SERVER_HOST=" >> "$GITHUB_ENV"
echo "SERVER_ORIGINS=" >> "$GITHUB_ENV"
echo "SERVER_SECRET_KEY=" >> "$GITHUB_ENV"
echo "CHATDB_URL=" >> "$GITHUB_ENV"
echo "WEAVIATE_URL=${{ secrets.WEAVIATE_URL }}" >> "$GITHUB_ENV"

if [[ ${GITHUB_BASE_REF:-${GITHUB_REF#refs/heads/}} == 'master' ]]; then
if [[ ${{ steps.extract_branch.outputs.branch }} == 'master' ]]; then
echo "ENV_TAG=prod" >> "$GITHUB_ENV"
echo "WEAVIATE_API_KEY=${{ secrets.PROD_WEAVIATE_API_KEY }}" >> "$GITHUB_ENV"
echo "CHATDB_URL=${{ secrets.PROD_CHATDB_URL }}" >> "$GITHUB_ENV"
Expand All @@ -91,10 +79,10 @@ jobs:
- name: Check and update widget index
run: |
GCP_SSH_CMD="gcloud compute ssh cacti-bastion-server --zone us-east1-b --ssh-key-file /tmp/gcp/google_compute_engine --quiet --tunnel-through-iap --ssh-flag"
if [[ ${GITHUB_BASE_REF:-${GITHUB_REF#refs/heads/}} == 'master' ]]; then
$GCP_SSH_CMD '-vvv -fN -L 8080:${{ secrets.PROD_WEAVIATE_INTERNAL_IP }}'
if [[ ${{ steps.extract_branch.outputs.branch }} == 'master' ]]; then
$GCP_SSH_CMD '-fN -L 8080:${{ secrets.PROD_WEAVIATE_INTERNAL_IP }}'
else
$GCP_SSH_CMD '-vvv -fN -L 8080:${{ secrets.DEV_WEAVIATE_INTERNAL_IP }}'
$GCP_SSH_CMD '-fN -L 8080:${{ secrets.DEV_WEAVIATE_INTERNAL_IP }}'
fi

cd backend
Expand Down
15 changes: 11 additions & 4 deletions .github/workflows/check-widget-translation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ jobs:
with:
path: backend

- name: Extract branch name
run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
id: extract_branch

- name: Show branch name
run: echo "Branch name is ${{ steps.extract_branch.outputs.branch }}"

- id: 'auth'
uses: 'google-github-actions/auth@v1'
with:
Expand Down Expand Up @@ -63,7 +70,7 @@ jobs:
echo "CHATDB_URL=" >> "$GITHUB_ENV"
echo "WEAVIATE_URL=${{ secrets.WEAVIATE_URL }}" >> "$GITHUB_ENV"

if [[ ${GITHUB_BASE_REF:-${GITHUB_REF#refs/heads/}} == 'master' ]]; then
if [[ ${{ steps.extract_branch.outputs.branch }} == 'master' ]]; then
echo "ENV_TAG=prod" >> "$GITHUB_ENV"
echo "WEAVIATE_API_KEY=${{ secrets.PROD_WEAVIATE_API_KEY }}" >> "$GITHUB_ENV"
echo "CHATDB_URL=${{ secrets.PROD_CHATDB_URL }}" >> "$GITHUB_ENV"
Expand All @@ -76,10 +83,10 @@ jobs:
- name: Check widget translation
run: |
GCP_SSH_CMD="gcloud compute ssh cacti-bastion-server --zone us-east1-b --ssh-key-file /tmp/gcp/google_compute_engine --quiet --tunnel-through-iap --ssh-flag"
if [[ ${GITHUB_BASE_REF:-${GITHUB_REF#refs/heads/}} == 'master' ]]; then
$GCP_SSH_CMD '-vvv -fN -L 8080:${{ secrets.PROD_WEAVIATE_INTERNAL_IP }}'
if [[ ${{ steps.extract_branch.outputs.branch }} == 'master' ]]; then
$GCP_SSH_CMD '-fN -L 8080:${{ secrets.PROD_WEAVIATE_INTERNAL_IP }}'
else
$GCP_SSH_CMD '-vvv -fN -L 8080:${{ secrets.DEV_WEAVIATE_INTERNAL_IP }}'
$GCP_SSH_CMD '-fN -L 8080:${{ secrets.DEV_WEAVIATE_INTERNAL_IP }}'
fi

cd backend
Expand Down
Empty file added integrations/__init__.py
Empty file.
54 changes: 38 additions & 16 deletions integrations/center.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
import requests
import web3

from fastapi.responses import Response

import env
import utils
import auth

from utils import ETH_MAINNET_CHAIN_ID, nft, FetchError, ExecError
import utils.timing as timing
Expand All @@ -26,8 +29,7 @@
#"polygon-mainnet",
]

API_URL = "https://api.center.dev/v1"
API_V2_URL = "https://api.center.dev/v2"
API_URL = "https://api.center.dev"

MAX_RESULTS = 12
PAGE_LIMIT = 12
Expand Down Expand Up @@ -126,6 +128,7 @@ class NFTAsset(ContainerMixin):
preview_image_url: str
description: str = ''
price: Optional[str] = None
attributes: Optional[ List ] = None

def container_name(self) -> str:
return 'display-nft-asset-container'
Expand Down Expand Up @@ -214,7 +217,7 @@ def fetch_nft_search(search_str: str) -> Generator[Union[NFTCollection, NFTAsset
))
count = 0
for network in NETWORKS:
url = f"{API_V2_URL}/{network}/search?{q}"
url = f"{API_URL}/v2/{network}/search?{q}"
timing.log('search_begin')
response = requests.get(url, headers=HEADERS)
try:
Expand Down Expand Up @@ -295,7 +298,7 @@ def fetch_nft_search_collection_by_trait(network: str, address: str, trait_name:
limit=limit,
offset=offset,
))
url = f"{API_URL}/{network}/{address}/assets/searchByTraits?{q}"
url = f"{API_URL}/v1/{network}/{address}/assets/searchByTraits?{q}"
response = requests.post(url, headers=headers, json=payload)
try:
response.raise_for_status()
Expand Down Expand Up @@ -337,15 +340,15 @@ def fetch_nft_search_collection_by_trait(network: str, address: str, trait_name:


def fetch_nft_collection(network: str, address: str) -> NFTCollection:
url = f"{API_V2_URL}/{network}/{address}/nft/metadata"
url = f"{API_URL}/v2/{network}/{address}/nft/metadata"
response = requests.get(url, headers=HEADERS)
response.raise_for_status()
obj = response.json()
num_assets = obj['totalSupply']
if num_assets == 0: # seems to return 0 incorrectly
# use the asset endpoint with dummy token
token_id = 1
url = f"{API_V2_URL}/{network}/{address}/nft/{token_id}/metadata"
url = f"{API_URL}/v2/{network}/{address}/nft/{token_id}/metadata"
response = requests.get(url, headers=HEADERS)
if response.status_code == 200:
token_obj = response.json()
Expand Down Expand Up @@ -375,7 +378,7 @@ def fetch_nft_collection_assets(network: str, address: str) -> NFTCollectionAsse
{"Address": address, "TokenID": token_id}
for token_id in token_ids[offset: offset + limit]
]}
url = f"{API_URL}/{network}/assets"
url = f"{API_URL}/v1/{network}/assets"
response = requests.post(url, headers=HEADERS, json=payload)
try:
response.raise_for_status()
Expand All @@ -400,6 +403,7 @@ def fetch_nft_collection_assets(network: str, address: str) -> NFTCollectionAsse
name=item['name'],
preview_image_url=item['mediumPreviewImageUrl'],
price=price,

)
if not _is_valid_asset(asset):
continue
Expand Down Expand Up @@ -434,7 +438,7 @@ def fetch_nft_collection_assets_for_sale(network: str, address: str) -> Generato
{"Address": address, "TokenID": token_id}
for token_id in token_ids[offset: offset + limit]
]}
url = f"{API_URL}/{network}/assets"
url = f"{API_URL}/v1/{network}/assets"
response = requests.post(url, headers=HEADERS, json=payload)
try:
response.raise_for_status()
Expand Down Expand Up @@ -482,7 +486,7 @@ def fetch_nft_collection_traits(network: str, address: str) -> NFTCollectionTrai
limit=limit,
offset=offset,
))
url = f"{API_URL}/{network}/{address}/traits?{q}"
url = f"{API_URL}/v1/{network}/{address}/traits?{q}"
response = requests.get(url, headers=HEADERS)
try:
response.raise_for_status()
Expand Down Expand Up @@ -525,7 +529,7 @@ def fetch_nft_collection_trait_values(network: str, address: str, trait: str) ->
limit=limit,
offset=offset,
))
url = f"{API_URL}/{network}/{address}/traits/{trait}?{q}"
url = f"{API_URL}/v1/{network}/{address}/traits/{trait}?{q}"
response = requests.get(url, headers=HEADERS)
try:
response.raise_for_status()
Expand Down Expand Up @@ -557,24 +561,26 @@ def fetch_nft_collection_trait_values(network: str, address: str, trait: str) ->


def fetch_nft_asset(network: str, address: str, token_id: str) -> NFTAsset:
url = f"{API_URL}/{network}/{address}/{token_id}"
url = f"{API_URL}/v2/{network}/{address}/nft/{token_id}/metadata"
response = requests.get(url, headers=HEADERS)
response.raise_for_status()
obj = response.json()
return NFTAsset(
network=network,
address=address,
token_id=token_id,
collection_name=obj['collectionName'],
name=obj['name'],
preview_image_url=obj['mediumPreviewImageUrl'],
collection_name=obj['collection']['name'],
name=obj['metadata']['name'],
preview_image_url=f"{API_URL}{obj['media']['small']}",
# image_url=f"{API_URL}{obj['media']['medium']}",
description=obj['metadata']['description'],
attributes=obj['metadata']['attributes'],
)


def fetch_nft_asset_traits(network: str, address: str, token_id: str) -> NFTAssetTraits:
price = _fetch_nft_asset_price_str(network, address, token_id)
url = f"{API_URL}/{network}/{address}/{token_id}"
url = f"{API_URL}/v1/{network}/{address}/{token_id}"
response = requests.get(url, headers=HEADERS)
response.raise_for_status()
obj = response.json()
Expand Down Expand Up @@ -622,7 +628,7 @@ def fetch_nfts_owned_by_address_or_domain(network: str, address_or_domain: str)
limit=limit,
offset=offset,
))
url = f"{API_V2_URL}/{normalized_network}/{address_or_domain}/nfts-owned?{q}"
url = f"{API_URL}/v2/{normalized_network}/{address_or_domain}/nfts-owned?{q}"

try:
response = requests.get(url, headers=HEADERS)
Expand Down Expand Up @@ -691,3 +697,19 @@ def _fetch_nft_asset_price_str(network: str, address: str, token_id: str) -> Opt
else:
price = None
return price

@auth.authenticate_user_id()
def fetch_center_image(response, network: str, address: str, token_id: str, size: str, user_id:str=None):

url = f"{API_URL}/v2/{network}/{address}/nft/{token_id}/render/{size}"
resp = requests.get(url, headers=HEADERS)

# Check if user is authenticated
if not user_id: return None

# return response.content
if resp.status_code != 200:
# Handle error appropriately, return a message, or another status code
return Response(content="Could not retrieve image", status_code=resp.status_code)

return Response(content=resp.content)
7 changes: 6 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
from dataclasses import dataclass
from typing import Any, Dict, Optional, Set

from fastapi import FastAPI, Request, Response, Body, WebSocket, WebSocketDisconnect
from fastapi import FastAPI, Request, WebSocket, WebSocketDisconnect
from fastapi.middleware.cors import CORSMiddleware
from starlette.middleware.sessions import SessionMiddleware
from fastapi.responses import StreamingResponse

import server
import chat
Expand All @@ -15,6 +16,7 @@
from app import chat as app_chat
from app import share as app_share

from integrations import center

app = FastAPI()

Expand Down Expand Up @@ -80,6 +82,9 @@ class ClientState:
async def api_nonce(request: Request):
return auth.api_nonce(request)

@app.get("/center_image/{network}/{address}/{token_id}/{size}")
async def fetch_nft_image(request: Request, network: str, address: str, token_id: str, size: str):
return center.fetch_center_image(request, network, address, token_id, size)

@app.post("/login")
async def api_login(request: Request, data: auth.AcceptJSON):
Expand Down