gmailsalesforceapexsalesforce-lightninglightning

How to make a Lightning Web Components?


I need to make a Lightning Web Components with this Apex Class but I don't know how to pass the data to the JS and the HTML and how to create that lwc. I want to make a lwc that shows every email received from a certain email every time that lwc components is used/called. Maybe is better to do an Aura Component? I don't know. Here's the code

public with sharing class Gmail {

public with sharing class GMResponseMessages {
    public Integer resultSizeEstimate;
    public GMThread[] messages;
}
      
public with sharing class GMThread {
    public String id;
    public String threadId;
}
      
public with sharing class GMMessage {
    public String id;
    public String threadId;
    public String[] labelIds;
    public String snippet;
    public String historyId;
    public String internalDate;
    public GMMessagePart payload;
    public String sizeEstimate;
    public String raw;  
}
      
public with sharing class GMMessagePart {
    public String partId;
    public String mimeType;
    public String filename;
    public Header[] headers;
    public GMMessagePartBody[] body;
    public GMMessagePart[] parts;  
}
      
public with sharing class Header {
    public String name;
    public String value;
}
      
public with sharing class GMMessagePartBody {
    public String attachmentId;
    public String superinteger;
    public String data;
}



@AuraEnabled
public static void getGmail_API() {

      //GET https://gmail.googleapis.com/gmail/v1/users/{userId}/messages/{id}

    Http http = new Http();
    HTTPResponse response;
    HttpRequest request;
    String userEmail = 'myemail@gmail.com';
      
    request = new HttpRequest();
    request.setMethod('GET');
    request.setEndpoint('callout:Gmail_API/gmail/v1/users/myemail@gmail.com/messages?q=from:othermail@mail.it');
      
    response = http.send(request); 
    System.debug('START');
    System.debug(response.getStatusCode());
      
    if(response.getStatusCode() == 200) {
    JSONParser parser = JSON.createParser(response.getBody());
    System.debug(parser);
    GMResponseMessages jsonResponse = (GMResponseMessages)JSON.deserialize(response.getBody(), GMResponseMessages.class);
    System.debug(jsonResponse); 
      
    for(GMThread thread: jsonResponse.messages){
        System.debug('THREAD FOR');
        System.debug(thread);
        Http httpMsg = new Http();
        HTTPResponse responseMsg;
        HttpRequest requestMsg;
        requestMsg = new HttpRequest();
        requestMsg.setMethod('GET');
        requestMsg.setEndpoint('callout:Gmail_API/gmail/v1/users/myemail@gmail.com/messages/' + thread.id);
        responseMsg = httpMsg.send(requestMsg); 
        if(responseMsg.getStatusCode() == 200) {
            GMMessage jsonResponseMsg = (GMMessage)JSON.deserialize(responseMsg.getBody(), GMMessage.class);
            System.debug(jsonResponseMsg);
      
        }
    }
      }

}

}


Solution

  • You'll need to follow the documented pathways to call an Apex method in a Lightning Web Component.

    You can do this by using an imperative call or a wire method. Since your Apex method takes no parameters, an imperative call is likely the way to go.