from http.server import BaseHTTPRequestHandler,HTTPServer
import json,time
requests=[]
def pr(n):
 return dict(id=n,title='Rate limit trigger' if n==1 else 'Interactive pull request',state='OPEN',description='Detail accepted during the background pause.' if n==2 else 'Trigger',author=dict(uuid='{reviewer}',nickname='reviewer',display_name='Reviewer'),created_on='2026-11-10T09:00:00Z',updated_on='2026-11-10T10:00:00Z',source=dict(branch=dict(name='feature'),repository=dict(full_name='demo/web')),destination=dict(branch=dict(name='main'),repository=dict(full_name='demo/web')),links=dict(html=dict(href=f'https://bitbucket.org/demo/web/pull-requests/{n}')),participants=[],reviewers=[])
class Handler(BaseHTTPRequestHandler):
 def log_message(self,*a): pass
 def do_GET(self):
  global requests
  p=self.path.split('?')[0]
  status=200; body={}
  if p=='/reset': requests=[]
  elif p=='/requests': body=requests
  else:
   requests.append(dict(path=self.path,time=time.time()))
   if p.endswith('/pullrequests/1'):
    status=429;body={'error':{'message':'Synthetic host rate limit; Retry-After 600 seconds'}}
   elif p.endswith('/pullrequests/2'): body=pr(2)
   elif p.endswith('/pullrequests'): body={'values':[pr(1),pr(2)],'pagelen':50,'size':2}
   elif p.endswith('/comments'):
    body={'values':[dict(id=21,content={'raw':'Activity accepted during the background pause.'},user={'nickname':'reviewer','display_name':'Reviewer'},created_on='2026-11-10T10:00:00Z',deleted=False,pending=False,links={'html':{'href':'https://bitbucket.org/demo/web/pull-requests/2#comment-21'}})]}
   elif p.endswith('/conflicts'): body={'values':[]}
   elif '/permissions/' in p: body={'values':[{'permission':'admin','repository':{'full_name':'demo/web'}}]}
   elif p.endswith('/user'): body={'uuid':'{reviewer}','nickname':'reviewer','display_name':'Reviewer'}
   elif any(p.endswith('/'+x) for x in ['commits','statuses','diffstat','members']): body={'values':[]}
   elif p.endswith('/branching-model'): body={'development':{'name':'main'}}
   elif p.endswith('/demo/web'): body={'uuid':'{web}','full_name':'demo/web','name':'web','mainbranch':{'name':'main'},'links':{'html':{'href':'https://bitbucket.org/demo/web'}}}
   else: body={'values':[]}
  self.send_response(status)
  self.send_header('Content-Type','application/json')
  if status==429:self.send_header('Retry-After','600')
  self.end_headers();self.wfile.write(json.dumps(body).encode())
HTTPServer(('127.0.0.1',18995),Handler).serve_forever()
