001/* 002 * This file is part of the Keycloak Moodle authenticator 003 * Copyright (C) 2024 Michael N. Lipp 004 * 005 * This program is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU Lesser General Public License as published 007 * by the Free Software Foundation; either version 3 of the License, or 008 * (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, but 011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 013 * License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public License along 016 * with this program; if not, see <http://www.gnu.org/licenses/>. 017 */ 018 019package org.jdrupes.keycloak.moodleauth.moodle; 020 021import java.io.IOException; 022import java.net.URI; 023import java.util.Map; 024import org.jdrupes.keycloak.moodleauth.moodle.model.MoodleSiteInfo; 025import org.jdrupes.keycloak.moodleauth.moodle.model.MoodleUser; 026import org.jdrupes.keycloak.moodleauth.moodle.service.MoodleClient; 027 028/** 029 * Represents an open connection to a moodle instance. 030 */ 031public class MoodleClientConnection implements MoodleClient { 032 033 private final RestClient restClient; 034 private final MoodleUser moodleUser; 035 private final MoodleSiteInfo siteInfo; 036 037 /** 038 * Instantiates a new moodle client connection. 039 * 040 * @param restClient the rest client 041 */ 042 public MoodleClientConnection(URI siteUri, RestClient restClient, 043 MoodleUser moodleUser, MoodleSiteInfo siteInfo) { 044 this.restClient = restClient; 045 this.moodleUser = moodleUser; 046 this.siteInfo = siteInfo; 047 } 048 049 @Override 050 public Object invoke(String wsfunction, Map<String, Object> params) 051 throws IOException { 052 return restClient.invoke(Object.class, Map.of("wsfunction", wsfunction), 053 params); 054 } 055 056 @Override 057 public MoodleUser moodleUser() { 058 return moodleUser; 059 } 060 061 @Override 062 public MoodleSiteInfo siteInfo() { 063 return siteInfo; 064 } 065 066 @Override 067 @SuppressWarnings({ "PMD.AvoidCatchingGenericException", 068 "PMD.EmptyCatchBlock" }) 069 public void close() { 070 try { 071 restClient.close(); 072 } catch (Exception e) { 073 // Only trying to be nice 074 } 075 } 076 077}