function makeAPI(root) { //Protobuf types const AttributeType = 'models.proto.cofd.api.Attribute'; const Attribute = root.lookupType(AttributeType); const UpdateSkillValueRequestType = 'models.proto.cofd.api.UpdateSkillValueRequest'; const UpdateSkillValueRequest = root.lookupType(UpdateSkillValueRequestType); const SkillSpecializationUpdateType = 'models.proto.cofd.api.SkillSpecializationsUpdate'; const SkillSpecializationsUpdate = root.lookupType(SkillSpecializationUpdateType); const protobufContentType = (messageType) => ({ 'Content-Type': 'application/x-protobuf; messageType="' + messageType + '"' }); const attributesResource = (username, characterID) => '/api/cofd/' + username + '/' + characterID + '/attributes'; const skillResource = (username, characterID, skillName) => '/api/cofd/' + username + '/' + characterID + '/skills'; function verifyAndCreate(protobufType, payload) { let err = protobufType.verify(payload); if (err) throw err; return protobufType.create(payload); } async function updateAttribute(params) { const { username, characterID, attribute, newValue } = params; let req = verifyAndCreate(Attribute, { name: attribute, value: parseInt(newValue) }); const resource = attributesResource(username, characterID); let resp = await fetch(resource, { method: 'PATCH', headers: { ... protobufContentType(AttributeType) }, body: Attribute.encode(req).finish() }).then(async resp => { console.log("resp is", await resp.text()); }).catch(async err => { console.log("err is", err.text()); }); } async function updateSkillValue(params) { const { username, characterID, skillName, newValue } = params; let req = verifyAndCreate(UpdateSkillValueRequest, { characterUsername: username, characterId: parseInt(characterID), skillName: skillName, skillValue: parseInt(newValue) }); let resp = await fetch('/api/rpc/cofd/update_skill_value', { method: 'POST', headers: { ... protobufContentType(UpdateSkillValueRequestType) }, body: UpdateSkillValueRequest.encode(req).finish() }).then(async resp => { console.log("resp is", await resp.text()); }).catch(async err => { console.log("err is", err.text()); }); } return { updateAttribute, updateSkillValue }; }