open Camlet_domain let error ~category ~code ~message = App_error.make ~category ~code ~message type kind = Issue | State | Comment let label = function | Issue -> "issue" | State -> "state" | Comment -> "comment" let code kind suffix = "issue.view_" ^ label kind ^ suffix let decode_response ~kind ~decode response = match response with | response when response.Camlet_adapter_http.Http_client.status = 200 -> if Camlet_adapter_http.Http_client.has_media_type response "application/json" then decode response.body else Error (error ~category:Protocol ~code:(code kind "_content_type") ~message: "The record-author PDS returned an unexpected content type") | response when response.status = 400 -> ( match ( Camlet_adapter_http.Http_client.has_media_type response "application/json" , Camlet_atproto_protocol.Repository_read_codec.decode_xrpc_error response.body ) with | true, Some ("RecordNotFound", _) -> Error (error ~category:Not_found ~code:(code kind "_not_found") ~message: "The record-author PDS has no current record at the requested \ URI") | true, Some ("InvalidRequest", _) -> Error (error ~category:Protocol ~code:(code kind "_invalid_request") ~message:"The record-author PDS rejected the record request") | _ -> Error (error ~category:Protocol ~code:(code kind "_response_unexpected") ~message: "The record-author PDS returned an ambiguous error response")) | response when response.status = 429 -> Error (error ~category:Transport ~code:"issue.view_rate_limited" ~message:"A record-author PDS rate limited the issue observation") | response when response.status >= 500 && response.status <= 599 -> Error (error ~category:Transport ~code:"issue.view_pds_failed" ~message:"A record-author PDS could not return a required record") | _ -> Error (error ~category:Protocol ~code:(code kind "_response_unexpected") ~message:"The record-author PDS returned an unexpected response") let decode_issue_response requested = decode_response ~kind:Issue ~decode: (Camlet_atproto_protocol.Issue_view_codec.decode_issue_record ~requested) let decode_state_response = decode_response ~kind:State ~decode:Camlet_atproto_protocol.Issue_view_codec.decode_state_record let decode_comment_response = decode_response ~kind:Comment ~decode:Camlet_atproto_protocol.Issue_view_codec.decode_comment_record let record_query uri = match (At_uri.collection uri, At_uri.record_key uri) with | Some collection, Some record_key -> Ok [ ("repo", At_uri.authority uri) ; ("collection", collection) ; ("rkey", Record_key.to_string record_key) ] | _ -> Error (error ~category:Internal ~code:"issue.view_record_locator_invalid" ~message:"The validated record URI cannot be decomposed") let read client origin uri ~kind ~decode = match record_query uri with | Error error -> Error error | Ok query -> ( match Camlet_adapter_http.Http_client.get client ~origin ~path:"/xrpc/com.atproto.repo.getRecord" ~query ~accept:"application/json" ~max_body_bytes:(2 * 1024 * 1024) with | Error error -> Error error | Ok response -> decode_response ~kind ~decode response) let read_issue client origin uri = read client origin uri ~kind:Issue ~decode:(fun body -> Camlet_atproto_protocol.Issue_view_codec.decode_issue_record ~requested:uri body) let read_state client origin uri = read client origin uri ~kind:State ~decode:Camlet_atproto_protocol.Issue_view_codec.decode_state_record let read_comment client origin uri = read client origin uri ~kind:Comment ~decode:Camlet_atproto_protocol.Issue_view_codec.decode_comment_record