PrimalHQ / primal-caching-service

Primal’s caching service for Nostr connects to the specified set of relays, collects all events in real time, stores them locally, and makes them available to nostr clients through a web socket-based API.
https://primal.net
MIT License
85 stars 13 forks source link

Running API Request examples #10

Closed noubre closed 12 months ago

noubre commented 1 year ago

Hi, I was able to get it running and pulling data from the relays. How do I use the API? I see the examples but how do we make the request? Is there a Julia function I have to call on the REQ examples?

Also, can we send requests to our own instance endpoint? what would the url be?

Thanks!

aaaa760 commented 7 months ago

Hi @noubre

I hope you're doing well. I came through the issue you mentioned here i am also able to pulling data from relays but can't understand how to use the API, which you've marked as completed. I'm facing a similar challenge and would greatly appreciate any tips or guidance you could share.

Thank you for your time!

noubre commented 7 months ago

Hi @aaaa760, I am doing well, I hope you are too. Once the data starts being pulled from all the relays, you can use the api from the Julia command line by calling any of the functions you see in the src/app.jl file. I'll try and find those example commands but you should be able to figure it out if you look at the app.jl file.

To use the api through it's websocket connection, use a websocket library to connect and send it a request formatted like the examples at the end of the README:

["REQ", "amelx49c18", {"cache": ["net_stats"]}] ["CLOSE", "amelx49c18"]

["REQ", "p0xren2axa", {"cache": ["feed", {"pubkey": "64-hex digits of pubkey id"}]}]

["REQ", "vqvv4vc6us", {"cache": ["thread_view", {"event_id": "64-hex digits of event id"}]}]

["REQ", "ay4if6pykg", {"cache": ["user_infos", {"pubkeys": ["64-hex digits of pubkey id"]}]}]

["REQ", "2t6z17orjp", {"cache": ["events", {"event_ids": ["64-hex digits of event id"]}]}]

["REQ", "1uddc0a2fv", {"cache": ["user_profile", {"pubkey": "64-hex digits of pubkey id"}]}]

You can just copy and paste those examples with the right variables replaced with their values right into your code. I can't find my working python example but it would be something like this:

import asyncio
import websockets
import json

uri = "wss://your_primal_caching_service_url"
limit = 100  # Change this to your desired number of requests.

request = ["REQ", "2t6z17orjp", {"cache": ["events", {"limit": limit}]}] # This is where you put the example requests

async def send_request():
  all_responses = []  # Create an empty list to store the responses.
  # Proceed to send the request.
  try:
    async with websockets.connect(uri) as ws:
      for _ in range(limit):
        await ws.send(json.dumps(request))
        print(f"> Sent: {request}")
        response = await ws.recv()
        pretty_response = json.dumps(json.loads(response), indent=4)
        all_responses.append(pretty_response)
        await asyncio.sleep(1)  # Wait a second before sending the next request.
  except Exception as e:
    print(f"An error occurred: {e}")
  print("Reached limit, closing connection.")
  return all_responses